Automation Using Shell Script

Emeka Afamefuna Nwosa
The Startup
Published in
5 min readNov 13, 2020
Photo by Jelleke Vanooteghem on Unsplash

I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it — Bill Gates

Being a “lazy” programmer most often does not translate to being lazy in the real sense of the world but the ability to work efficiently by automating anything and everything that can be automated. It is the ability not to repeat the same mundane task multiple times, instead focusing on tasks that are important.

This semester, I decided to take shell programming course which consists of 14 weeks of lecture and lab sessions. Four weeks into the semester, I noticed a rather boring pattern during labs. Every lab had the same sequence:- create a directory, cd into the directory, create a file, write the script header, write the given lab task, make the file executable, and execute it. Since a typical lab consist of 4–6 files, I started thinking of creating a scaffold that will allow me focus on the lab task instead of doing other boring repetitive stuffs. In this article, I will walk you through how I was able to achieve this and what I have learnt so far but first, a little about shell scripting.

Shell scripting consist of series of commands that can be executed by Unix/Linux shell. The shell accepts these human-readable commands into the system and executes them automatically. There are different flavours of shell: The Bourne Shell (sh), The C Shell (csh), The Korn Shell (ksh), and The GNU Bourne-Again Shell (bash) with each having a varying degree of syntax. For this walk through, I will be using the bash shell syntax.

Automated File Creation

For this script, my goal was to check if a given file exists in a directory, if the file does exist, it will open the file for editing using nano text editor (You can use any other text editor of your choice). However, if the file does not exist, I want to automatically create the file, write the script header, make it executable and open it up for editing with one line of command. Here is what my script looks like

createfile.sh

In line 1, I included the shebang #!/bin/bash which indicates that this script will always be run with bash, however, if you are not sure if bash is supported by your system, you can use #!/bin/sh which can be executed by whichever shell your system supports. For more details about the difference, please read here

Line 4 starts the if statement with -f flag, we check if a given file exists or if it is a normal file and not a directory (Other flags that can be used with file includes -r is readable, -w is writable, -x is executable, and -s is empty). if the filename is a normal file and does not exist in the directory yet, then we create the file using the touch command in line 7, write the script header using the echo command and the redirector > symbol which overwrites anything in the file with the script header. To make the file executable, you can use either use chmod +x $1 or chmod 777 $1command. Finally, the file is opened and ready for us to start editing.

#To run this script
./createfile.sh filename

Automated Directory creation

The goal of this script is to automate the creating of directory, copying needed files into the directory and then cd into the directory. Just like the file creation, the script starts with the shebang #!/bin/bash, however, unlike file creation, -d flag is used to check if the filename is directory and not a normal file.

I decided to copy both the createfile and createdir scripts into the new directory using the cp command. Createfile because I will have to create files in the new directory and createdir because I can create a sub-directory within the new directory.

Perhaps, the biggest lesson me here is the use of cd within a shell script. As the script is right now, if you execute the following commands, you will get undesired result

chmod +x createdir.sh
./createdir.sh test1

The above command will do everything in the script but not change the directory (cd) to the new directory. Trying to understand what was going on led me here.

Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.

There are various ways to overcome this challenge, the first solution is to add exec bash after the cd command. This solution however opens up as many subshells as the script is called. The second solution is to use alias but I decided to use the much simpler third solution instead which is to source the script.

# how to source a script
. createdir.sh test2 #notice the space after the dot
OR
source createdir.sh test3

Difference between sourcing a script and executing a script is that while commands are run in the current shell process when we use source, executing the script runs the script in a new shell process (subshell). Basically, use source if you intend to change the environment in the current running shell. Amr Abdeen wrote a more detailed article on this.

Conclusion

To put it all together, add both createfile.sh and createdir.sh in the base directory, source the createdir.sh script and then execute the createfile.sh script… you have a ready file within second.

This is probably the most basic automation using shell script but it tells a story of endless possibilities. As a developer, network engineer or system admin, being able to automate some of your daily task can be very handy. Anything and everything can be automated just like this guy here.

I’m just starting with shell scripting journey, if you find any error or bug with my script, please do call my attention to it. I hope to build more complex automation script in the near future. For now, have fun with your automation!

--

--

Emeka Afamefuna Nwosa
The Startup

Networking student | FrontEnd Developer | HCIA certified | UPM