How to Pro­gram with Shell Scripts: A Tu­to­rial

OpenSource For You - - Contents - By: Shashid­har Sop­pin The au­thor is a se­nior soft­ware ar­chi­tect and has 16+ years of ex­pe­ri­ence in the IT in­dus­try—in vir­tu­al­i­sa­tion, the cloud, Docker, open source soft­ware, ML, deep learn­ing and OpenS­tack. He is part of the prod­uct engi­neer­ing team at W

Shell scripts are com­puter pro­grams that are spe­cially made to be run by the UNIX shell. Typ­i­cally, shell scripts are used for ma­nip­u­la­tion, pro­gram ex­e­cu­tion and print­ing text. They en­able the user to com­mu­ni­cate with the OS, and vice versa.

Most of what we will cover in this ar­ti­cle has a history in the UNIX/Linux world, right from the days of AT&T, Bell Labs and BSD (Berke­ley Soft­ware Di­vi­sion), when vari­ants of UNIX flavours were cre­ated and the shell started evolv­ing. To­day, we have many vari­ants of shell and a lot of op­tions to choose from.

Users need to un­der­stand their ex­act re­quire­ments be­fore choos­ing the shell that suits the sit­u­a­tion. Some of the well­known and widely used shells are: ƒ sh ƒ bash ƒ csh ƒ ksh ƒ pd­ksh ƒ tcsh

What is a shell?

A shell is a process which is, or can be, spawned. It is also an in­ter­preter for com­puter OSs. Shells take the in­put from the user and pass it on to the OS and vice versa. All shell com­mands can be ex­e­cuted and used to carry out var­i­ous ex­e­cu­tions and out­put gen­er­a­tion, us­ing script­ing.

What is a spawn?

A spawn is a com­bi­na­tion of exec() and wait() sys­tem calls/ li­brary func­tions, which uses ex­ter­nal com­mands to run and pro­vide the nec­es­sary out­put.

Note: A pro­gram in ex­e­cu­tion is called a process.

Note 2: The com­mand ls­b_re­lease –a can be used to view the present ver­sion of the Linux vari­ant. Through­out this doc­u­ment, I have used Ubuntu 17.04, also called ‘zesty’. The com­mand un­ame –a can be used to see the Linux kernel ver­sion, as well as more de­tails.

ubuntu@ubuntu:~$ ls­b_re­lease -a No LSB mod­ules are avail­able. Dis­trib­u­tor ID: Ubuntu De­scrip­tion: Ubuntu 17.04 Re­lease: 17.04

Co­de­name: zesty ubuntu@ubuntu:~$

ubuntu@ubuntu:~$ un­ame -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 var­i­ous shell types?

The fol­low­ing are the var­i­ous types of shells, ex­plained in brief.

sh: The Bourne shell (sh) or com­mand line in­ter­preter is for com­puter op­er­at­ing sys­tems. This was the de­fault shell for Ver­sion, in ear­lier vari­ants of UNIX 7.

In Ubuntu, this is also called ‘dash’. This is a POSIX­com­pli­ant im­ple­men­ta­tion of /bin/sh that aims to be as small as pos­si­ble. ‘dash’ is a di­rect de­scen­dant of the NetBSD ver­sion of ‘ash’ (the Almquist SHell), ported to Linux in early 1997. It was re­named dash in 2002. The fol­low­ing com­mand will take the reader to the de­tailed man page for sh in Ubuntu:

ubuntu@ubuntu:~$ man sh

Note: ‘sh_shel­l_screen_shot’ will give a de­tailed view of the out­put of this com­mand.

Bash: Bash is a UNIX shell and com­mand lan­guage writ­ten by Brian Fox for the GNU Project as a free soft­ware re­place­ment for the Bourne shell.

ubuntu@ubuntu:~$ man bash

csh: The C shell is a UNIX shell cre­ated by Bill Joy, while he was a grad­u­ate stu­dent at the Univer­sity of Cal­i­for­nia, Berke­ley in the late 1970s. This shell was widely used by the C-pro­gram­ming com­mu­nity ear­lier.

Ksh: KornShell is a UNIX shell and was de­vel­oped by David Korn at Bell Labs in the early 1980s. It was based on the Bourne shell source code. This shell was the de­fault in many pro­pri­etary UNIX flavours like AIX, HP-UX, etc.

Note: In some of the Ubuntu flavours, this will not be avail­able, by de­fault. We need to in­stall it us­ing the sudo apt get in­stall ksh com­mand.

Pd­ksh: The Pub­lic Do­main Korn Shell, also known as pd­ksh, is open source and is used pre­dom­i­nantly in UNIX/ Linux like sys­tems.

Tcsh: This is an en­hanced but com­pletely com­pat­i­ble ver­sion of the Berke­ley UNIX C shell, csh(1). It is a com­mand lan­guage in­ter­preter us­able both as an in­ter­ac­tive lo­gin shell and a shell script com­mand pro­ces­sor.

User de­tails

The fol­low­ing are the well-known and pop­u­lar user de­tail based com­mands.

1) whoami: This com­mand is used to print an ef­fec­tive user ID.

ubuntu@ubuntu:~$ whoami ubuntu

2) un­ame: This com­mand is used to print sys­tem in­for­ma­tion. There are many op­tions, but ‘-a’ is most pop­u­lar and will dis­play all the in­for­ma­tion.

ubuntu@ubuntu:~$ un­ame -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 ter­mi­nal in­for­ma­tion.

shashi-ubuntu@ubuntu:~$ who ubuntu tty7 2018-04-09 09:09 (:0)

Di­rec­to­ries

There are seven to eight di­rec­to­ries present across all the UNIX/Linux flavoured OSs.

Some of the fol­low­ing are pop­u­lar and it is best if users get fa­mil­iar with them. a) bin: All the bi­nary ex­e­cuta­bles are present in this di­rec­tory. b) lib: All the li­brary files are present here. c) dev: All the de­vice files (char­ac­ter, block, socket, etc types) are here. d) etc: This is for all sys­tems ad­min re­lated com­mands. e) usr and /usr/bin: This is the home for users and ad­di­tional ex­e­cuta­bles.

Cre­at­ing and view­ing files, and copy­ing/ re­mov­ing com­mands

There is a long list of com­mands used for cre­at­ing, view­ing, copy­ing and re­mov­ing files and file op­er­a­tions. Here, I have listed the most pop­u­lar ones. a) touch: This com­mand is used to cre­ate new files and to

change time­stamps.

ubuntu@ubuntu:~$ touch 123 ubuntu@ubuntu:~$ ls -l to­tal 44

-rw-r--r-- 1 ubuntu ubuntu 0 Apr 9 09:36 123 dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Desk­top dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Doc­u­ments dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Down­loads -rw-r--r-- 1 ubuntu ubuntu 8980 Mar 24 09:42 ex­am­ples.desk­top dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Mu­sic dr­wxr-xr-x 2 ubuntu ubuntu 4096 Apr 6 04:44 Pic­tures dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Pub­lic dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Tem­plates dr­wxr-xr-x 2 ubuntu ubuntu 4096 Mar 24 09:52 Videos

b) cat: This con­cate­nates files and prints them on the con­sole. The cat com­mand comes up with a lot of op­tions, in­clud­ing re-di­rec­tions (us­ing op­er­a­tors like <, >, <<, >>, etc). c) cp: This com­mand is used to copy files and di­rec­to­ries. ubuntu@ubuntu:~$ cp 123 321

ubuntu@ubuntu:~$ ls -l to­tal 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 com­mand is used to re­move files and di­rec­to­ries. To re­move di­rec­to­ries one can use the –r op­tion and –I will help with be­ing in­ter­ac­tive. e) mv: This com­mand is used to move files and di­rec­to­ries

across and within the file sys­tem.

List­ing files and per­mis­sions, and mask­ing file per­mis­sions

To make things easy, I have cre­ated the fol­low­ing ta­bles. a) A list of the types of files. In any of the di­rec­to­ries (/root is the best ex­am­ple), if you ex­e­cute the ‘ls –l’ com­mand, it will dis­play the out­put as shown be­low. The first per­mis­sion char­ac­ter de­picts the type of file.

root@ubuntu:/# cd dev root@ubuntu:/dev# ls -l |more to­tal 0 crw------crw------dr­wxr-xr-x 1 root root 10, 175 Apr 9 09:07 agp­gart 1 root root 10, 235 Apr 9 09:07 aut­ofs 2 root root 260 Apr 9 09:07 block dr­wxr-xr-x 2 root root 80 Apr 9 09:07 bsg crw------con­trol 1 root root 10, 234 Apr 9 09:07 btrf­s­con­trol

dr­wxr-xr-x 3 root root 60 Apr 9 09:07 bus lr­wxr­wxrwx 1 root root 3 Apr 9 09:07 cdrom -> sr0 lr­wxr­wxrwx 1 root root 3 Apr 9 09:07 cdrw -> sr0 dr­wxr-xr-x 2 root root 3620 Apr 9 09:08 char crw------lr­wxr­wxrwx 1 root root 5, 1 Apr 9 09:07 con­sole 1 root root 11 Apr 9 09:07 core -> / proc/kcore crw------la­tency 1 root root 10, 59 Apr 9 09:07 cpu_d­ma_ crw------- 1 root root 10, 203 Apr 9 09:07 cuse File per­mis­sions: - {(Oc­tal num­ber) Read π 4 :: Write π 2 :: Ex­e­cute π 1}

ubuntu@ubuntu:~$ ls -la to­tal 108 dr­wxr-xr-x 1 ubuntu ubuntu 4096 Apr 11 08:47 . dr­wxr-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 to­tal 108 dr­wxr-xr-x 14 ubuntu ubuntu 4096 Apr 11 08:47 . dr­wxr-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 to­tal 108 dr­wxr-xr-x 14 ubuntu ubuntu 4096 Apr 11 08:47 . dr­wxr-xr-x 3 root root 4096 Mar 24 09:42 .. -rwxr­wxrwx 1 ubuntu ubuntu 0 Apr 9 09:36 123

Note: 1) Other pos­si­ble ‘chmod’ op­tions are given be­low:

chmod[who] [+/-/I][per­mis­sions] <file name> chmod go+r go-w <file name>

2) For mask­ing the file per­mis­sions, umask is used (the de­fault value of umask is ‘0022’).

Re­mov­ing files forcibly

The com­mand ‘rm –f’ is used to forcibly re­move files.

The pwd, rmdir and mkdir com­mands

The com­mand ‘pwd’ is used to know more about the present work­ing di­rec­tory. The com­mands ‘mkdir’ and ‘rmdir’ are used for cre­at­ing and re­mov­ing di­rec­to­ries.

Note: The com­mand ‘rmdir –p <Dir-Name>’is used for re­mov­ing the leaf di­rec­tory. We have to in­clude the com­plete path.

Disk space

The com­mands ‘df’ (disk space) and ‘du’ (disk util­i­sa­tion) are mainly used to find out the disk us­age.

The most used op­tion is ‘df –iv’ which gives in­for­ma­tion about the free and used blocks.

ubuntu@ubuntu:~$ df -iv

Filesys­tem In­odes 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 - /me­dia/shashiubuntu/Mo­bileWiFi

Other com­mands

Listed be­low are com­mands that are pop­u­larly used while writ­ing shell scripts.

1. File op­er­a­tion re­lated com­mands such as cat, wc, touch, file, which, sort, cut, grep, head & tail, more, etc

2. IO re­di­rect­ion com­mands/op­tions (>, <, <<, >>, etc)

3. who

4. tee

5. Process-ori­ented com­mands like ps, no­hup, kill, nice, time, top, batch, etc

Shell pro­gram­ming

Some sam­ples of small shell scripts us­ing sh are listed be­low.

###Sam­ple-1(sim­ple script)### #!/bin/sh # This is a com­ment! echo Hello World # This is a com­ment sec­tion

###Sam­ple-2(tak­ing de­ci­sions)### #!/bin/bash if cp $source $tar­get then echo “File copied suc­cess­fully” fi

###Sam­ple-3(sim­ple for loop)### #!/bin/bash for num­ber in 1 2 3 4 5

do echo $num­ber

done exit 0

# this prints the num­bers

Each shell will sup­port a ‘shell prompt’ and we can set this prompt us­ing the vari­able ‘PS1’.

For ex­am­ple, you can ex­e­cute this com­mand on any shell prompt $PS1=‘‘What_prompt#”.

Now the prompt will be set to ‘What_prompt#’.

Watch out for more on shell script­ing in fu­ture is­sues of OSFY.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.