Shell Scripts
(Bash as an example)
Jan 24, 2025 1
What is covered here ?
When to use scripting langauges ?
When not to use scripting languages (particulary shell) ?
What are the different shells on the UNIX operating systems ?
What are the features provided by the “Bash” shell (in detail) ?
What are shell scripts and how are they useful ?
What are the files which save config information ?
Jan 24, 2025 2
“Shell” way of doing things
Lots of UNIX commands
Each of which does a specific and simple thing (like ls,
cd, cat, echo, find, wc, cut, paste, basename etc.)
However, their combination is quite powerful (almost
anything that can be done with a programming
language can be done).
Shell provides the necessary facilities for combining
the commands. (like pipes, loops, conditionals and the
most important of all: the power of scripting).
Somewhat like Component Technology !!!
Jan 24, 2025 3
“Shell” way of doing things
Control driven
Primitives are UNIX or
user-defined commands
Jan 24, 2025 4
When not to use shell scripts...
resource-intensive tasks, especially where speed is a factor
complex applications, where structured programming is a
necessity
mission-critical applications upon which you are betting the
ranch, or the future of the company
situations where security is important, where you need to
protect against hacking
project consists of subcomponents with interlocking
dependencies
Jan 24, 2025 5
When not to use shell scripts...
extensive file operations required (Bash is limited to serial
file access, and that only in a particularly clumsy and inefficient
line-by-line fashion)
need to generate or manipulate graphics or GUIs
need direct access to system hardware
need port or socket I/O
need to use libraries or interface with legacy code
Jan 24, 2025 6
Shell Features
A shell is simply a macro processor that executes commands.
A Unix shell is both a command interpreter, which provides the user
interface to the rich set of Unix utilities, and a programming language,
allowing these utilities to be combined.
The different UNIX shells are: Bourne Shell, C-Shell, Korn Shell, Bash,
Tcsh ...
Files containing commands can be created, and become commands
themselves. (Shell Script)
While executing commands is essential, most of the power (and
complexity) of shells is due to their embedded programming languages.
Like any high-level language, the shell provides variables, flow control
constructs, quoting, and functions.
Jan 24, 2025 7
The “Bash” Shell
Bash is the shell, or command language interpreter, that will appear in the
GNU operating system.
The name is an acronym for the `Bourne-Again SHell', a pun on Steve
Bourne, the author of the direct ancestor of the current Unix shell /bin/sh,
which appeared in the Seventh Edition Bell Labs Research version of Unix.
Bash is an sh-compatible shell that incorporates useful features from the
Korn shell ksh and the C shell csh. It offers functional improvements over
sh for both interactive and programming use.
It currently runs on nearly every version of Unix and a few other
operating systems -independently-supported ports exist for MS-DOS, OS/2,
Windows 95, and Windows NT.
Jan 24, 2025 8
Examples (commands & scripts)
$ ls -l
total 8
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 code
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 docs
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 test
-rwxr-xr-x 1 gsriram staff 6 Mar 22 12:27 test1.sh
$ cat test1.sh
ls -l
chmod +x test1.sh
test1.sh
total 8
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 code
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 docs
drwxr-xr-x 2 gsriram staff 512 Mar 22 12:27 test
-rwxr-xr-x 1 gsriram staff 6 Mar 22 12:27 test1.sh
Jan 24, 2025 9
Shell Features
Shell Syntax : Shell Operation, Quoting, Escape Character, Single
Quotes, Double Quotes, Comments
Shell Commands: Simple Commands, Pipelines, Lists of Commands,
Looping Constructs, Conditional Constructs, Grouping Commands
Shell Functions
Shell Parameters : Positional Parameters, Special Parameters, Shell
Expansions, Brace Expansion, Tilde Expansion, Shell Parameter
Expansion, Command Substitution, Arithmetic Expansion, Process
Substitution, Word Splitting, Filename Expansion, Pattern Matching,
Quote Removal
Jan 24, 2025 10
Shell features (contd…)
Redirections: Redirecting Input, Redirecting Output, Appending
Redirected Output, Redirecting Standard Output and Standard Error,
Here Documents, Duplicating File Descriptors, Opening File Descriptors
for Reading and Writing
Executing Commands : Simple Command Expansion, Command
Search and Execution, Command Execution Environment, Environment,
Exit Status, Signals
Shell Scripts
Jan 24, 2025 11
Shell Commands
Simple Commands
$ pwd
/home/gsriram/work/Orientation/bash_demo
List of commands
$ pwd ; ls ;
/home/gsriram/work/Orientation/bash_demo
code docs test test1.sh
Pipeline
Conditional Constructs (Features of high-level languages)
Looping Constructs (Feature of high-level languages)
Grouping Commands
Jan 24, 2025 12
Shell Commands (contd…)
Pipeline: A pipeline is a sequence of simple commands separated by`|'.
The output of each command in the pipeline is connected to the input of
the next command. That is, each command reads the previous
command's output.
find . -name *.c | wc -l
find . -name *.h | wc -l
find . -name *.s | wc -l
Jan 24, 2025 13
Grouping Commands
(a=hello; echo $a)
A listing of commands within parentheses starts a subshell.
(cat list1 list2 list3 | sort | uniq > list123)
(cat list4 list5 list6 | sort | uniq > list456)
# Merges and sorts both sets of lists
simultaneously.
wait #Don't execute the next command until subshells
finish.
diff list123 list456
-> For assignment, no blanks around = sign.
Jan 24, 2025 14
Conditional Commands
The syntax of the if command is:
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
The syntax of the case command is:
case word in
[ [(] pattern [| pattern]...) command-list ;;]...
esac
Jan 24, 2025 15
Conditional Commands (contd…)
a=4
b=5
if [ $a -ne $b ] ; then
echo "$a is not equal to $b"
echo "(arithmetic comparison)"
fi
if [ $a != $b ] ; then
echo "$a is not equal to $b."
echo "(string comparison)"
fi
Note: Be extremely careful with the syntax.
Jan 24, 2025 16
Conditional Commands (contd…)
File test operators : Unary expressions are often used
to examine the status of a file.
if [ ! -f $filename ] ; then
touch $filename;
echo "Creating file."
else
rm $filename;
echo "Cleaning out file."
fi
-> space between [ and ! Is very critical
Jan 24, 2025 17
Conditional Commands (contd…)
File test operators
• -a file True if file exists.
• -b file True if file exists and is a block special file.
• -c file True if file exists and is a character special file.
• -d file True if file exists and is a directory.
• -e file True if file exists.
• -f file True if file exists and is a regular file.
• -p file True if file exists and is a named pipe (FIFO).
• -r file True if file exists and is readable.
• -s file True if file exists and has a size greater than
zero.
Jan 24, 2025 18
Conditional Commands (contd…)
#!/bin/bash echo
echo "Hit a key, then hit return."
read Keypress
case "$Keypress" in
[a-z] ) echo "Lowercase letter";;
[A-Z] ) echo "Uppercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac
# Allows ranges of characters in [square brackets].
Jan 24, 2025 19
Looping Commands
The syntax of the while command is:
while test-commands; do
consequent-commands;
done
The syntax of the for command is:
for name [in words ...]; do
commands;
done
The syntax of the until command is:
until test-commands; do
consequent-commands;
done
Jan 24, 2025 20
Looping Commands (contd…)
function asm_file()
{
in_file=$1
if [ $big_endian == "1" ] ; then
asmsc100 -b -obe -g $in_file
else
asmsc100 -b -g $in_file
fi
}
for sl_file in *.sl; do
asm_file $sl_file
done
Jan 24, 2025 21
Looping Commands (contd…)
var0=0
while [ "$var0" -lt 10 ]
do
echo "$var0 "
var0=`expr $var0 + 1`
done
Jan 24, 2025 22
Looping Commands (contd…)
The break and continue loop control commands
correspond exactly to their counterparts in other
programming languages.
The break command terminates the loop (breaks out
of it), while continue causes a jump to the next
iteration of the loop, skipping all the remaining
commands in that particular loop cycle.
Jan 24, 2025 23
Looping Commands (contd…)
a=0
while [ $a -le 19 ] do
a=`expr $a + 1`
if [ $a -eq 3 ] || [ $a -eq 11 ] ; then
# Excludes 3 and 11
continue
fi
echo "$a "
done
Jan 24, 2025 24
Shell Functions
Shell functions are a way to group commands for later
execution using a single name for the group.
• They are executed just like a "regular" command.
• Shell functions are executed in the current shell context; no
new process is created to interpret them.
Functions are declared using this syntax:
[ function ] name () { command-list; }
This defines a shell function named “name”.
• The reserved word function is optional.
• The body of the function is the command-list between { and }.
This list is executed whenever name is specified as thename
of a command.
• The exit status of a function is the exit status of the last
command executed in the body.
Jan 24, 2025 25
Shell Functions (contd…)
When a function is executed, the arguments to the
function become the positional parameters during its
execution.
The special parameter `#' that expands to the number
ofpositional parameters is updated to reflect the
change. Positional parameter 0 is unchanged.
function asm_file()
{
in_file=$1
...
}
Jan 24, 2025 26
Shell Parameters
A parameter is an entity that stores values. It can be a
name, a number, or one of the special characters
(discussed later). For the shell's purposes, a variable is
a parameter denoted by a name.
• A parameter is set if it has been assigned a value. The
null string is a valid value. Once a variable is set, it
may be unset only by using the unset builtin
command.
A variable may be assigned to by a statement of the
form
name=[value]
• If value is not given, the variable is assigned the null
string.
Jan 24, 2025 27
Shell Parameters (contd…)
A positional parameter is a parameter denoted by one
or more digits, other than the single digit 0.
Positional parameters are assigned from the shell's
arguments when it is invoked,and may be reassigned
using the set builtin command. Positional parameter N
may be referenced as ${N} or $N.
• When a positional parameter consisting of more than a
single digit is expanded, it must be enclosed in braces.
Positional parameters may not be assigned to with
assignment statements.
Jan 24, 2025 28
Shell Parameters (contd…)
#!/bin/bash
# Variables: assignment and substitution
a=37.5
hello=$a
# No space permitted on either side of = sign when initializing
variables.
echo hello [hello]
# Not a reference.
echo $hello [37.5]
echo ${hello} #Identical to above.
echo "$hello” [37.5]
echo "${hello}" [37.5]
echo '$hello’ [$hello]
# Variable referencing disabled by single quotes,
# because $ interpreted literally.
Jan 24, 2025 29
Shell Parameters (contd…)
Some important parameters (Lots of prizes if you can
guess what they expand to !!!)
• $*
• $#
• $?
• $$
• $0
Jan 24, 2025 30
Shell Parameters (contd…)
Some important parameters (Lots of prizes if you can
guess what they expand to !!!)
• $* - All the parameters
• $0 - Name of the shell script
• $# - The number of parameters
if [ $# -ne 3 ] ; then
echo “Usage: cmd <file-name> <user-name> <num>
fi
Jan 24, 2025 31
Shell Parameters (contd…)
Some important parameters (Lots of prizes if you can
guess what they expand to !!!)
• $? - Exit status of the last command (Useful for
checking the status of a command)
some_command
OUTCOME=$?
If [ $OUTCOME == “1” ] ; then
echo “Failed” ;
else
echo “Passed” ;
fi
• $$ - Process ID of this shell (Useful for naming
temporary files)
Jan 24, 2025 32
Shell Expansions
Tilde Expansion
• If a word begins with an unquoted tilde character
(`~'), all of thecharacters up to the first unquoted
slash (or all characters,if there is no unquoted slash)
are considered a tilde-prefix.
• The characters in the tilde-prefix following the tilde are
treated as apossible login name.
• $ cd ~anindya/proj/docs
Jan 24, 2025 33
Shell Expansions (contd…)
The `$' character introduces parameter expansion,
command substitution, or arithmetic expansion.
The basic form of parameter expansion is $
{parameter}. The value of parameter is substituted.
Command substitution allows the output of a command
to replacethe command name. There are two forms:
$(command) or `command`
• textfile_listing=`ls *.txt`
textfile_listing=$(ls *.txt) # Same as above
• z=`expr $z + 3` # Same as z=$(($z+3))
• let z=z+3
Many ways of saying the same thing !!
Jan 24, 2025 34
Filename Expansion
Bash scans each word for the characters`*', `?', `(', and
`['. If one of these characters appears, then the word is
regarded as a pattern,and replaced with an
alphabetically sorted list offile names matching the
pattern.
How is pattern matching done ?
• * Matches any string, including the null string.
• ? Matches any single character.
• [...] Matches any one of the enclosed characters.
Jan 24, 2025 35
Filename Expansion (contd…)
# list all the C-files
ls *.c
scan.c printf.c test.c
# list all the G723 Project files
ls g723*
# list all the test-cases
ls test[1-9][1-9].txt
test10.txt test11.txt test12.txt test13.txt test14.txt
# list files with a single character extension
ls *.?
asm.s num.c sign.c tmp.s tmp.o
Jan 24, 2025 36
Redirections
>
• Redirect stdout to a file.
• Creates the file if not present, otherwise overwrites
it.
ls -l > listing.list
>>
• Redirect stdout to a file.
• Creates the file if not present, otherwise appends to
it.
<
• Accept input from a file.
• Companion command to ">", and often used in
combination with it.
Jan 24, 2025 37
Redirections (contd…)
Redirecting to a while loop
#!/usr/bin/bash
Filename=$1
while [ “$name” != end ] ; do
read name # Reads from $Filename, rather than stdin.
echo $name
done < $Filename # Redirects stdin to file $Filename
Jan 24, 2025 38
Bash Features (Quite Helpful)
Aliases allow a string to be substituted for a word when it
is used as the first word of a simple command.
alias g723u='cd proj/tlator_16k/tests/g.723.1/dsp16k’
$ g723u # wow
The directory stack is a list of recently-visited directories.
The pushd builtin adds directories to the stack as it
changesthe current directory, and the popd builtin
removes specified directories from the stack and changes
the current directory to the directory removed.
$ cd proj/tlator_16k/tests/g.723.1/dsp16k
$ pushd ../../tests/testcases/
...
$ popd
Jan 24, 2025 39
Bash Features (Quite Helpful)
Controlling the prompt
PS1='\u@\h:\w|>’ [gsriram@mes6:~/proj|> ]
History Features
• Use the arrow keys to retrieve the earlier commands
Job Control : Job control refers to the ability to
selectively stop (suspend) the execution of processes
and continue (resume) their execution at a later point.
• Typing the suspend character (typically `^Z', Control-
Z) while aprocess is running causes that process to be
stopped and returns control to Bash.
• fg %1 : bringing job 1 from the background into the
foreground.
Jan 24, 2025 40
Bash Files
When an interactive shell that is not a login shell is
started, Bash reads and executes commands from
`~/.bashrc', if that file exists.
When a login shell exits, Bash reads and executes
commands from the file `~/.bash_logout', if it exists.
Jan 24, 2025 41
References
Advanced Bash-Scripting HOWTO :
http://www.linuxdoc.org/HOWTO/Adv-Bash-Scr-HOWTO/special-chars.html
Bash Reference Manual, Reference Documentation for
Bash, Edition 2.2, for bash Version 2.02 :
http://www.fsf.org/manual/bash-2.02/html_chapter/bashref_toc.html
Jan 24, 2025 42