The Bash scripting language is used to create powerful, yet easy-to-use tools for creating scripting interfaces.
In this article, we’re going to walk you through how to create a simple, easy-access script to automate some tasks for your home automation system.
Read moreIn this tutorial, we’ll walk you step-by-step through the creation of a simple Bash script to help automate some common home automation tasks.
You’ll learn the basics of the scripting language, including syntax, command syntax, and how to use the environment variables.
Then, we can move onto the use of the script to run the task.
In this example, we use the simple bash script “home_admin.sh” to automate the following tasks.
It will create a directory on the hard drive called home.
You can use this directory as the base for your script.
We will use the command echo $HOME as the command to automate.
This is so that the script can find the correct command line arguments to run.
It is also useful to use a directory separator ( / ).
The default separator for the command is / and can be changed in your shell.
For example, to automate all of the tasks listed above, we could run:HomeAdmin /home/user/home/work/MyScript/Script.shThe /home/” and /home” are part of the Unix system directory.
The / is for the parent directory, and the “~” is used as the separator.
So, for this example we would say:HomeAdministrator /home~user/HomeAdministrators Script.shThis script will automate the tasks in the following order.
It uses a simple command to do the initial setup.
It does not do anything to the file that it is running from.
It just waits for a prompt.
Next, it adds a few environment variables to the script.
We use environment variables because they allow us to pass them to commands.
For the first task, we create a new file named myscript.sh.
We will be using this file for the following steps:We will add a new variable called HOME_USER to the $HOME variable.
We set it to the user name that we created in the previous step.
Next we add the following environment variables that we will use for our next task.
For each of these, we would use a new line separator that would allow us the script not to read from a file that already exists.
The last variable is HOME_WORK_HOME, which is the location on the drive of our script.
This will be the base location of the scripts that we create.
For this task, I will be adding a new command named “homeadmin.py”.
It will create the script file with the following commands:The next step is to add the variable HOME_HOME to the variable $HOME.
I would use the new variable HOME for the new command because I would like it to work from the $home directory that we added earlier.
Finally, we will add the command HOME_ACTIVITY to the command line.
The HomeAdmin script will add this to the end of its command line with the line “home administrator.py home activity.py”Home Administrator will then create a subdirectory on the root of the hard disk that will hold our script files.
The next task is to create the directory named home in the subdirectory of the file named home.
We would create the new directory with the command:The last step is for Home Administrator to create an instance of the HomeAdmin.py script.
I created an instance with the script:Home Administrator home activity, home activity home activityHomeAdmin is now creating the script from the sub directory created by Home Administrator.
I added the following variables to Home Administrator’s command line:The command “home-admin.bash” will create an executable file named “HomeAdmin.bash”.
The script will now execute in the instance created by home-admin with the environment variable HOME set to $HOME and the environment Variable HOME set as $HOME:The script has executed successfully.
Next step is adding some environment variables for the task that we have created in step #1.
The next step would be adding the variable home to the value of the variable SHELL_COMMAND in the variable command.
I will use this variable for the next step:Next, we need to add an environment variable to Home Administrators variable $PATH, which I will call HOME_PAGE_NAME.
This variable will allow us add a path for our script to be executed.
For our task, let’s add the variables for this task.
For this task I would set $HOME to $home, $SHELL_CMD to bash, and $PATH to the directory that I added in step 4:We now need to set the variable PATH to the location of our Home Admin script script, so we can execute it.
This can be done by adding the following command:We are now