In the last chapter, we got started with shell scripting for Unix-like OSs. We are comfortable with shell scripts now.
Continuing the momentum, we will learn a couple of useful features in Unix-like operating systems’ shell. The following is the list of topics we will learn in this chapter:
After this chapter, we will be comfortable using the above-mentioned advanced features in the shell. This is a very short chapter.
I/O Redirection
In Unix-like operating systems, standard streams are interconnected input and output channels. The three standard interconnected channels are standard input (stdin), standard output (stdout), and standard error (stderr). They are attached to file handles (or file descriptors) 0, 1, and 2, respectively. Almost all the programming and scripting environments come with provisions to handle these streams. Let us see them one by one.
stdin
This is the standard input. In most of the cases, it is the input from the keyboard. Let us see an
example. Create a file known as New.txt in the current location and add some characters to the file. We know that we can show the contents of a file with the command
cat as follows:
We can use stdin to feed data to the
cat command as follows:
It will show us the contents of the file mentioned in the command. We know that Unix associates 0 with stdin. So the preceding command can also be written as follows:
stdout
The standard
output is usually the visual display console (it could also be the remote terminal). It is represented by the file handle 1. Suppose I wish to redirect it to a file. Then I can write a command as follows:
When we execute the preceding command, it will not show the
output in the terminal. It will redirect the output to the file mentioned in the command. We can see the contents of that file with the cat command as follows:
We can also omit 1 and write the command
ls -l 1> output.log as follows:
The
> redirection operator overwrites previous data in the file mentioned in the command if the file exists already. We can append to the existing data with the
>> redirection operator as follows:
ls -l 1>> output.log
ls -l >> output.log
Both the commands perform the same function.
Stderr
We can
redirect the standard error. It is represented by the file handle
2. We can use it as follows:
It will redirect the error in the command (if any) to the file mentioned in the command. We can also redirect it and overwrite the error log as follows:
We can
redirect the output and error to the same file as follows:
We can also write it as follows:
A device file that rejects all information redirected to it is a null device. In Unix-like operating systems, it is
/dev/null. It is also known as the Unix Black Hole (informally). We usually redirect error to this file. It is used as follows:
Crontab
Cron is a time-based scheduler in Unix-like operating systems. We can run a program or a script at a set time at regular intervals. We have to use a file known as crontab
to set cron jobs. We need to make entries for our scheduled jobs to run. The format is as follows:
(minute) (hour) (day of the month) (month) (day of the week) <Program or script to run>Minute can have values from 0 to 59.
Hour can have values from 0 to 23.
Day of the month can have values from 1 to 31.
Month can have values from 1 to 12.
Day of the week can have values from 0 to 6.
Let us
see a few examples. The following entry runs the specified script every day at midnight:
0 0 * * * /home/pi/backup.sh
We can run a script or a program at the time of every reboot as follows:
@reboot /home/pi/test2.sh
We can run a program every five minutes as follows:
*/1 * * * * /home/pi/test3.sh
We can use the following command to see the current entries in the crontab:
We can use the following command to edit the entries in the crontab:
It will ask you for the choice of editor when you run it the very first time. Choose the nano editor.
Let us see an example of a simple shell script and usage of crontab. Let us create a small shell script as shown in Listing
7-1.
#!/bin/bash
logfile=reboot_hist.log
echo "System rebooted at : " >> $logfile
date >> $logfile
Listing 7-1reboot_hist.sh
Change its permissions with the following command:
Finally, add the following entry to the
crontab:
@reboot /home/pi/reboot_hist.sh
Then when we
reboot, it will write the reboot time to the specified log file. I rebooted a couple of times and then checked the log file. The contents are as follows:
pi@raspberrypi:~ $ cat reboot_hist.log
System rebooted at :
Thu Sep 10 20:04:42 IST 2020
System rebooted at :
Thu Sep 10 20:07:01 IST 2020
Summary
In this short chapter, we learned two advanced features of the shell: I/O redirection and usage of crontab. We also saw a small example of the crontab entry. Both these features are quite useful while writing shell scripts.
With this chapter, we conclude our journey of shell scripts. In the next chapter, we will learn how to program with high-level programming languages like C, C++, and Python 3 on Linux.