SHELL SCRIPTING
With the thousands of commands available to the command line user, how can we remember them all? The answer is, we don’t. The real power of the computer is its ability to do the work for us. To get it to do that, we use the power of the shell to automate things. We write shell scripts.
Imagine you’re tasked with setting up new workstations and user accounts regularly at your job. Instead of manually creating each folder and user account, a simple shell script serves as your efficient digital helper. By automating the creation of multiple directories and user accounts with just a few lines of code, it saves you considerable time and effort, allowing you to concentrate on more critical aspects of your work.
Shell scripting is the process of writing and executing a series of instructions in a shell to automate tasks. A shell script is essentially a script or program written in a shell language, such as Bash, sh, zsh, or PowerShell ?
A basic shell script that will create multiple folders and create multiple Linux users at once would look like this.
#!/bin/bash
# Create directories
mkdir Folder1
mkdir Folder2
mkdir Folder3
# Create users
sudo useradd user1
sudo useradd user2
sudo useradd user3
You should have something like this
# ls -latr
total 12
-rw-r--r-- 1 root root 149 Feb 11 14:35 my_first_shell_script.sh
drwxr-xr-x 1 root root 4096 Feb 11 14:36 ..
drwxr-xr-x 2 root root 4096 Feb 11 14:36 .
Something you should notice about the permissions of the newly created file is this -rw-r–r– which means;
However, no one has the execute (x) permission, hence the script cannot be executed.
To execute the script, you would typically do something like this.
./my_first_shell_script.sh
When you hit enter, you should get a response like
bash: ./my_first_shell_script.sh: Permission denied
Notice that we now have a Permission denied error which can easily be resolved by giving the file the necessary permission it requires.
But, did you also notice the mention of “bash” at the beginning of the error message? It indicates that the error message is coming from the Bash shell itself. Bash is the command interpreter or shell that you’re using in the terminal to execute commands. We will talk more about this in the next section.
Hint:
# id user1
uid=1000(user1) gid=1000(user1) groups=1000(user1)
Notice that, at the beginning of the shell script, we have ‘#!/bin/bash’ written there. This is what is called a shebang. It is a special notation used in Unix-like operating systems like Linux, to specify the interpreter that should be used to execute the script. In this case, #!/bin/bash specifically indicates that the Bash shell should be used to interpret and execute the script.
You can explore the /bin folder and see the different programs in there. Bash is one of them which is used as the interpreter in that script. If we wanted to use another shell like sh, the shebang would be updated to #!/bin/sh
/bin/bash: This is the absolute path to the Bash shell executable. It tells the system to use the Bash interpreter located at /bin/bash to run the script.
Without a shebang line, the system may not know how to interpret and execute the script, and you may need to explicitly specify the interpreter when running the script.
In programming generally, not just shell scripting, variables are essential for creating dynamic and flexible programs.
Variables can store data of various types such as numbers, strings, and arrays. You can assign values to variables using the = operator, and access their values using the variable name preceded by a $ sign.
Lets explore a few examples:
Example: Assigning value to a variable:
name="John"
From the example above, “John” was assigned to the variable “name”.
Now that the variable is assigned, you may ask, how then can I use it?
After assigning a value to a variable, as shown in the previous example where we assigned “John” to the variable name, you can utilize this variable in various ways in your script or program. One of the most straightforward methods to use or retrieve the value stored in a variable is by echoing it back to the console. This is done using the echo command in shell scripting.
echo $name
This command instructs the shell to print the value of name to your screen, which, in our case, would output John
echo is a command used to print a text, variables or values. In this example, echo is used to print a variable which stores a value.
In the next Shell Scripting project, we will see how powerful we can automate tasks using control flows.