How to Program with Shell Scripts: A Tutorial

3
7280

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 shell and 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:

  • sh
  • bash
  • csh
  • ksh
  • pdksh
  • tcsh

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.

ubuntu@ubuntu:~$ lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description: Ubuntu 17.04

Release: 17.04

Codename: zesty

ubuntu@ubuntu:~$

ubuntu@ubuntu:~$ uname -a

Linux ubuntu 4.10.0-19-generic #21-Ubuntu SMP Thu Apr 6 17:04:57 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

ubuntu@ubuntu:~$

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/sh that 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:

ubuntu@ubuntu:~$ man sh

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.

ubuntu@ubuntu:~$ man bash

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.

User details

The following are the well-known and popular user detail based commands.

1) whoami: This command is used to print an effective user ID.

ubuntu@ubuntu:~$ whoami

ubuntu

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.

ubuntu@ubuntu:~$ uname -a

Linux ubuntu 4.10.0-19-generic #21-Ubuntu SMP Thu Apr 6 17:04:57 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

3) who: This shows who has logged in with terminal information.

shashi-ubuntu@ubuntu:~$ who

ubuntu tty7 2018-04-09 09:09 (:0)

Directories

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.

ubuntu@ubuntu:~$ touch 123

ubuntu@ubuntu:~$ ls -l

total 44

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:36 123

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Desktop

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Documents

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Downloads

-rw-r--r-- 1 ubuntu ubuntu 8980 Mar 24 09:42 examples.desktop

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Music

drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 6 04:44 Pictures

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Public

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Templates

drwxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Videos

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.

ubuntu@ubuntu:~$ cp 123 321

ubuntu@ubuntu:~$ ls -l

total 44

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:36 123

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:41 321

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.

root@ubuntu:/# cd dev

root@ubuntu:/dev# ls -l |more

total 0

crw------- 1 root root 10, 175 Apr 9 09:07 agpgart

crw------- 1 root root 10, 235 Apr 9 09:07 autofs

drwxr-xr-x 2 root root 260 Apr 9 09:07 block

drwxr-xr-x 2 root root 80 Apr 9 09:07 bsg

crw------- 1 root root 10, 234 Apr 9 09:07 btrfs-

control

drwxr-xr-x 3 root root 60 Apr 9 09:07 bus

lrwxrwxrwx 1 root root 3 Apr 9 09:07 cdrom -> sr0

lrwxrwxrwx 1 root root 3 Apr 9 09:07 cdrw -> sr0

drwxr-xr-x 2 root root 3620 Apr 9 09:08 char

crw------- 1 root root 5, 1 Apr 9 09:07 console

lrwxrwxrwx 1 root root 11 Apr 9 09:07 core -> /proc/kcore

crw------- 1 root root 10, 59 Apr 9 09:07 cpu_dma_latency

crw------- 1 root root 10, 203 Apr 9 09:07 cuse

File permissions: - {(Octal number) Read π 4 :: Write π 2 :: Execute π 1 }

ubuntu@ubuntu:~$ ls -la

total 108

drwxr-xr-x 1 ubuntu ubuntu 4096 Apr 11 08:47 .

drwxr-xr-x 3 root root 4096 Mar 24 09:42 ..

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:36 123

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:41 321

ubuntu@ubuntu:~$ chmod 0700 123

ubuntu@ubuntu:~$ ls -la

total 108

drwxr-xr-x 14 ubuntu ubuntu 4096 Apr 11 08:47 .

drwxr-xr-x 3 root root 4096 Mar 24 09:42 ..

-rwx------ 1 ubuntu ubuntu 0 Apr 9 09:36 123

ubuntu@ubuntu:~$ chmod 0777 123

ubuntu@ubuntu:~$ ls -la

total 108

drwxr-xr-x 14 ubuntu ubuntu 4096 Apr 11 08:47 .

drwxr-xr-x 3 root root 4096 Mar 24 09:42 ..

-rwxrwxrwx 1 ubuntu ubuntu 0 Apr 9 09:36 123

Note: 1) Other possible ‘chmod’ options are given below:

chmod[who] [+/-/I][permissions] <file name>

chmod go+r go-w <file name>

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.

Disk space

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.

ubuntu@ubuntu:~$ df -iv

Filesystem Inodes IUsed IFree IUse% Mounted on

udev 118658 437 118221 1% /dev

tmpfs 124359 904 123455 1% /run

/dev/sda1 1966080 186081 1779999 10% /

tmpfs 124359 4 124355 1% /dev/shm

tmpfs 124359 6 124353 1% /run/lock

tmpfs 124359 16 124343 1% /sys/fs/cgroup

tmpfs 124359 71 124288 1% /run/user/1000

/dev/sr0 0 0 0 - /media/shashi-ubuntu/MobileWiFi

Other commands

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)

3. who

4. tee

5. Process-oriented commands like ps, nohup, kill, nice, time, top, batch, etc

Shell programming

Some samples of small shell scripts using sh are listed below.

###Sample-1(simple script)###

#!/bin/sh

# This is a comment!

echo Hello World # This is a comment section

###Sample-2(taking decisions)###

#!/bin/bash

if cp $source $target

then

echo “File copied successfully”

fi

###Sample-3(simple for loop)###

#!/bin/bash

for number in 1 2 3 4 5

do

echo $number

done

exit 0

# this prints the numbers

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.

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here