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
- Create a folder on an ubuntu server and name it shell-scripting
- Using the vim editor, create a file called my_first_shell_script.sh
- Put the shell script code above into the new file
- Save the file
- use cd command to change into the shell-scripting directory
- Use ls -latr command to confirm that the file is indeed created.
# 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;
- The owner of the file has read (r) and write (w) permissions.
- Members of the file's group have read (r) permission.
- Others also have read (r) permission.
./my_first_shell_script.sh
- . / This prefix to the file indicates that the command should look for the file in the current directory.
- The dot (.) represents the current directory,
- and the slash (/) is a directory separator
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.
- Add the execute permission for the owner to be able to execute the shell script
- Run the shell script.
- Evaluate and ensure that 3 folders are created
- Evaluate and ensure that 3 users are created on the linux server
- Use the chmod command to update the permission
- Use ls to test that the folders are created
- Use id command to test that the users are created. You should see something like this:
# 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.
Example: Assigning value to a **variable**:
name="John"
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
