1. Introduction
UNIX shell programs executes the user command. User can enter like a command directly or executes a set of commands from a file called as a script file or program. Shell scrips are not compiled, it is interpreted executes line by line. In this chapter we are going to see shell script basics
2. Advantages and features using Shell script
Bash is a free software means no restriction to use and develop. bash incorporates features of Korn shell and C shell.
Features of bash are command history, unlimited size, control over job, shell functions, interactive, case statements, loops, conditional statements etc., Since it is free we have freedom to copy, modify and redistribute
a. The startup files in the Linux are based on Bash, the start up files are scripts that are read and executed when the operating starts
b. the important files that are activated during the start up or at the user logon
. /etc/profile
. ~/.bashrc
. ~/.bash_profile
. ~/.bash_history
. ~/.bash_logout
3. Commonly used Shells in Linux
UNIX has lot of shell types. each one is used for different purposes, namely sh,bash,csh,tcsh,ksh,zsh
sh or Bourne shell – It is used in UNIX system, it is a basic shell and will less features
bash – Bourne again shell – It is standard GNU shell probably most used shell in Linux it is flexible. It is advanced and user friendly shell , most of the user prefer the bash shell.
csh – C shell, it is used when a programmer executes C language codes.
tcsh or TENEX C shell – It is a superset of C shell, enhanced features for user friendly also called as Turbo C Shell
ksh – Korn shell , it is a superset of Bourne shell
zsh – Extended bourne shell, it includes features of bash, ksh and tcsh
The /etc/shell shows the available shells on a Linux machine
[root@control ~]# cat /etc/shells /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash [root@control ~]
The shell is set for the user and the default shells are set in the /etc/passwd file
[root@control ~]# grep devops /etc/passwd devops:x:1000:1000:DevOps:/home/devops:/bin/bash [root@control ~]#
To check the current shell execute the below command to check the current user shell
[devops@control ~]$ echo $SHELL /bin/bash
However user can switch between the shells
[devops@control ~]$ sh sh-4.4$
4. How to execute shells
Shell script can be executed in various ways, we are going to see shell script basics
sh filename.sh
for i in cat file
; do commands; done
Shell has built in comamnds used for variuos purpose depending on the logic and scnerios
break, eval, exit, exec, export,getopsts, hash, pwd, set, test,trap, umask,unset
alias, bind, command, echo, enable, logout, printf, shopt, read typeset, ulimit
Executing commands:
A command is being executed, Success will return value 0 and other than zero means it is wrong comamnd or unsuccessful, The executed command can be validated by using echo $?
Succesfull Command:
We could see the value 0
[root@control ~]# date Sun Dec 6 15:52:14 IST 2020 [root@control ~]# echo $? 0
Unsuccessful command
We could see the value 127 which is unsuccessful
[root@control ~]# data -bash: data: command not found [root@control ~]# echo $? 127 [root@control ~]#
5. Example shell scripts
In this section we will see basic scripts that will be useful for the start up
A good script will be without errors and perform the task, Script should be resuable scnerio and avoid unnecessary write ups
use vim editor for the color difference, vim enhanced useful to verify whether it is a keyword or variable or a command
[root@control ~]# vim test.sh !/bin/bash echo "Welcome to computer carriage" [root@control ~]# sh test.sh Welcome to computer carriage [root@control ~]#
In the above example #!/bin/bash is the interpreter, usually every script file start with the interpreter, so that the OS or the interpreter will know what language it is written
in this we have used a simple echo command to display, sh filename.sh will executes the script file
Example 2
[root@control ~]# vim test.sh !/bin/bash echo "Welcome to computer carriage" echo "Todays date and time is $(date)" [root@control ~]# sh test.sh Welcome to computer carriage Todays date and time
In the second example we have used date command inside the echo command, $(command) we can execute any command inside the parenthesis
Inputting the variable at run time
We are going to give the variable NAME while executing the script
[root@control ~]# vim test.sh !/bin/bash echo "Welcome to computer carriage" echo "Todays date and time is $(date)" echo "Enter the name" read NAME echo "Hello $NAME" [root@control ~]# sh test.sh Welcome to computer carriage Todays date and time is Sun Dec 6 16:07:17 IST 2020 Enter the name Bala <--- Input at runtime Hello Bala [root@control ~]#
In the above example read is the keyword used which is used to get input variable at runtime.
Define variables in the file
We can set the standard variable in the file itself
[root@control ~]# cat test.sh !/bin/bash NAME=Bala echo "Welcome to computer carriage" echo "Todays date and time is $(date)" echo "Hello $NAME" [root@control ~]# sh test.sh Welcome to computer carriage Todays date and time is Sun Dec 6 16:15:30 IST 2020 Hello Bala [root@control ~]#
In the above example we have set the NAME value is BALA, instead of asking inputs at runtime.
6. Loops and conditions
In bash scripts we can use loops and conditions like other programming languages
Loops
The below example i have created a file called cmds, which has the set of commands that has to be executed
[root@control ~]# cat cmds date ls uptime
In the file cmds to get executed the command at a time, for loop is used, cat cmds will list the file, the variable for i is the input of the cmds file. it is executed one by one line until the end of the file.
[root@control ~]# for i in cat cmds
do
$i
done
Sun Dec 6 16:10:44 IST 2020
anaconda-ks.cfg awx splunk-8.0.6-152fb4b2bb96-linux-2.6-x86_64.rpm test.sh
ansible-2.8.0-1.el8ae.noarch.rpm cmds splunkforwarder-8.0.5-a1a6394cc5ae-linux-2.6-x86_64.rpm
16:10:44 up 42 min, 2 users, load average: 0.10, 0.04, 0.16
[root@control ~]#
Conditional statements
We are going to see simple if condition, lets consider A=10 and B=20, with simple If condition we can get which bigger using the shell script basics
[root@control ~]# cat test.sh !/bin/bash A=10 B=20 if [[ A > B ]];then echo "A is bigger" else echo "B is bigger" fi [root@control ~]# sh test.sh B is bigger [root@control ~]#
7. Conclusion
hope you have understood the shell script basics , let us know you comments on the topic shell script basics
Refer: TLDP Shell script basics – https://tldp.org/LDP/abs/html/
Also refer: CentOS Minimal install – https://computercarriage.com/2020/09/23/centos-8-minimal-install/
2 thoughts on “Shell script basics”