Shell Scripting: Theory Part-1
Shell Scripting: Theory Part-1
SHELL SCRIPTING
Theory Part-1
Common Shells.
C-Shell - csh : The default on teaching systems Good for interactive systems Inferior
programmable features
Bourne Shell - bsh or sh - also restricted shell - bsh : Sophisticated pattern matching and file
name substitution
Korn Shell : Backwards compatible with Bourne Shell Regular expression substitution emacs
editing mode
Thomas C-Shell - tcsh : Based on C-Shell Additional ability to use emacs to edit the command
line Word completion & spelling correction Identifying your shell.
General Things
The shbang line
The "shbang" line is the very first line of the script and lets the kernel know what shell will be
interpreting the lines in the script. The shbang line consists of a #! followed by the full pathname
to the shell, and can be followed by options to control the behavior of the shell.
EXAMPLE
#!/bin/sh
#!/bin/bash
Comments
Comments are descriptive material preceded by a # sign. They are in effect until the end of a line
and can be started anywhere on the line.
EXAMPLE
# this text is not
count=5
Positional parameters
The shell has knowledge of a special kind of variable called a positional parameter. Positional
parameters are used to refer to the parameters that were passed to a shell program on the
command line or a shell function by the shell script that invoked the function. When you run a
shell program that requires or supports a number of command-line options, each of these options
is stored into a positional parameter. The first parameter is stored into a variable named 1, the
second parameter is stored into a variable named 2, and so forth. These variable names are
reserved by the shell so that you can't use them as variables you define. To access the values
stored in these variables, you must precede the variable name with a dollar sign ($) just as you do
with variables you define.
The following shell program expects to be invoked with two parameters. The program takes the
two parameters and prints the second parameter that was typed on the command line first and the
first parameter that was typed on the command line second.
#program reverse, prints the command line parameters out in reverse #order
there hello
READ Statement :
To get the input from the user.
read x y (no need of commas between variables)
The if Statement
All three shells support nested if...then...else statements. These statements provide you with a
way of performing complicated conditional tests in your shell programs. The syntax of the if
statement is the same for bash and pdksh and is shown here:
if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi
The case Statement
The case statement enables you to compare a pattern with several other patterns and execute a
block of code if a match is found.
case string1 in
str1)
commands;;
str2)
commands;;
*)
commands;;
esac
String1 is compared to str1 and str2. If one of these strings matches string1, the commands up
until the double semicolon (;;) are executed. If neither str1 nor str2 matches string1, the
commands associated with the asterisk are executed. This is the default case condition because
the asterisk matches all strings.
The shell languages also provide several iteration or looping statements. The most commonly
used of these is the for statement.
The for Statement
The ‘for’ statement executes the commands that are contained within it a specified number of
times. The ‘bash’ and ‘pdksh’ have two variations of the for statement.
for var1 in list
do
commands
done
for i in {2..10}
do
echo "output: $i"
done
fname () {
shell commands
}
File Operations:
A file can be read like:
FILE=”test.txt”
Check if file exist?
if [ -e "$FILE" ]
#ECHO COMMAND
echo Hello World!
INTRODUCTING VARIABLES
# VARIABLES
# Uppercase by convention
# Letters, numbers, underscores
NAME="Bob"
echo "My name is $NAME"
echo "My name is ${NAME}"
# USER INPUT
read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you!"
# SIMPLE IF STATEMENT
if [ "$NAME" == "Brad" ]
then
echo "Your name is Brad"
fi
COMPARISON
NUM1=31
NUM2=5
if [ "$NUM1" -gt "$NUM2" ]
then
echo "$NUM1 is greater than $NUM2"
else
echo "$NUM1 is less than $NUM2"
fi
FILE=”test.txt”
if [ -e "$FILE" ]
#CASE STATEMENT
read -p "Are you 18 or over? Y/N " ANSWER
case "$ANSWER" in
[yY] | [yY][eE][sS])
echo "You can make your National ID Card"
;;
[nN] | [nN][oO])
echo "Sorry, you still have to survive on Form B :("
;;
*)
echo "Please enter y/yes or n/no"
;;
esac
WHILE LOOP
FUNCTION
function sayHello() {
echo "Hello World"
}
sayHello
TASK -2
Check if a particular filename (string) exist?
TASK -3
Check if a particular file exists?
TASK-4
Input the names in an array from console and greet each name separately (assume that one name
is of one word)
TASK-5
Use for loop to rename a couple of existing files in a folder,
For example existing files in a particular folder are:
file1.txt, file2.txt, file3.txt
After running your script, these should be renamed to:
new-file1.txt, new-file2.txt, new-file3.txt
TASK-6
Use for loop to rename a number of files on the basis of choices mentioned in case A or B. let’s
say append “new” in case A or “old” in case B as a prefix of file name if a file date of
modification/creation is today or before. Where, today is prefixed with “new” and whatever
before is prefixed with “old”.
TASK-7
Make a function to crawl through the subfolders of current directory and display all the available
files with .txt extension.