Shell scripts are computer programs that are specially made to be run by the UNIX shell. Typically, shell scripts are used for manipulation, program execution and printing text. They enable the user to communicate with the OS, and vice versa.
Most of what we will cover in this article has a history in the UNIX/Linux world, right from the days of AT&T, Bell Labs and BSD (Berkeley Software Division), when variants of UNIX flavours were created and the shell started evolving. Today, we have many variants of shelland a lot of options to choose from.
Users need to understand their exact requirements before choosing the shell that suits the situation. Some of the well-known and widely used shells are:
What is a shell?
A shell is a process which is, or can be, spawned. It is also an interpreter for computer OSs. Shells take the input from the user and pass it on to the OS and vice versa. All shell commands can be executed and used to carry out various executions and output generation, using scripting.
What is a spawn?
A spawn is a combination of exec() and wait() system calls/library functions, which uses external commands to run and provide the necessary output.
Note: A program in execution is called a process.
Note 2: The command lsb_release –a can be used to view the present version of the Linux variant. Throughout this document, I have used Ubuntu 17.04, also called ‘zesty’. The command uname –a can be used to see the Linux kernel version, as well as more details.
What are the various shell types?
The following are the various types of shells, explained in brief.
sh: The Bourne shell (sh) or command line interpreter is for computer operating systems. This was the default shell for Version, in earlier variants of UNIX 7.
In Ubuntu, this is also called ‘dash’. This is a POSIX-compliant implementation of /bin/shthat aims to be as small as possible. ‘dash’ is a direct descendant of the NetBSD version of ‘ash’ (the Almquist SHell), ported to Linux in early 1997. It was renamed dash in 2002. The following command will take the reader to the detailed man page for sh in Ubuntu:
Note: ‘sh_shell_screen_shot’ will give a detailed view of the output of this command.
Bash: Bash is a UNIX shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell.
csh: The C shell is a UNIX shell created by Bill Joy, while he was a graduate student at the University of California, Berkeley in the late 1970s. This shell was widely used by the C-programming community earlier.
Ksh: KornShell is a UNIX shell and was developed by David Korn at Bell Labs in the early 1980s. It was based on the Bourne shell source code. This shell was the default in many proprietary UNIX flavours like AIX, HP-UX, etc.
Note: In some of the Ubuntu flavours, this will not be available, by default. We need to install it using the sudo apt get install ksh command.
Pdksh: The Public Domain Korn Shell, also known as pdksh, is open source and is used predominantly in UNIX/Linux like systems.
Tcsh: This is an enhanced but completely compatible version of the Berkeley UNIX C shell, csh(1). It is a command language interpreter usable both as an interactive login shell and a shell script command processor.
The following are the well-known and popular user detail based commands.
1) whoami: This command is used to print an effective user ID.
2) uname: This command is used to print system information. There are many options, but ‘-a’ is most popular and will display all the information.
3) who: This shows who has logged in with terminal information.
There are seven to eight directories present across all the UNIX/Linux flavoured OSs.
Some of the following are popular and it is best if users get familiar with them.
a) bin: All the binary executables are present in this directory.
b) lib: All the library files are present here.
c) dev: All the device files (character, block, socket, etc types) are here.
d) etc: This is for all systems admin related commands.
e) usr and /usr/bin: This is the home for users and additional executables.
Creating and viewing files, and copying/removing commands
There is a long list of commands used for creating, viewing, copying and removing files and file operations. Here, I have listed the most popular ones.
a) touch: This command is used to create new files and to change timestamps.
b) cat: This concatenates files and prints them on the console. The cat command comes up with a lot of options, including re-directions (using operators like <, >, <<, >>, etc).
c) cp: This command is used to copy files and directories.
d) rm: This command is used to remove files and directories. To remove directories one can use the –r option and –I will help with being interactive.
e) mv: This command is used to move files and directories across and within the file system.
Listing files and permissions, and masking file permissions
To make things easy, I have created the following tables.
a) A list of the types of files. In any of the directories (/root is the best example), if you execute the ‘ls –l’ command, it will display the output as shown below. The first permission character depicts the type of file.
Note: 1) Other possible ‘chmod’ options are given below:
2) For masking the file permissions, umask is used (the default value of umask is ‘0022’).
Removing files forcibly
The command ‘rm –f’ is used to forcibly remove files.
The pwd, rmdir and mkdir commands
The command ‘pwd’ is used to know more about the present working directory. The commands ‘mkdir’ and ‘rmdir’ are used for creating and removing directories.
Note: The command ‘rmdir –p <Dir-Name>’is used for removing the leaf directory. We have to include the complete path.
The commands ‘df’ (disk space) and ‘du’ (disk utilisation) are mainly used to find out the disk usage.
The most used option is ‘df –iv’ which gives information about the free and used blocks.
Listed below are commands that are popularly used while writing shell scripts.
1. File operation related commands such as cat, wc, touch, file, which, sort, cut, grep, head & tail, more, etc
2. IO redirection commands/options (>, <, <<, >>, etc)
5. Process-oriented commands like ps, nohup, kill, nice, time, top, batch, etc
Some samples of small shell scripts using sh are listed below.
Each shell will support a ‘shell prompt’ and we can set this prompt using the variable ‘PS1’.
For example, you can execute this command on any shell prompt $PS1=‘‘What_prompt#”.
Now the prompt will be set to ‘What_prompt#’.
Watch out for more on shell scripting in future issues of OSFY.