Shell Scripting Notes

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 17
At a glance
Powered by AI
The document discusses different ways to change process priority in Linux including using nice and renice commands. It also covers shell scripting basics like if/else, case statements, logical and file test operators.

You can set process priority when issuing a new command using the nice command or change the priority of an existing process using the renice command.

Shell scripting provides an interface to run commands and programs from scripts. Common constructs include if/else, case statements, loops, variables and more. Shells also have features like prompts and built-in commands for tasks like debugging.

Setting priority on new processes

At this point you are probably wondering how you can set
your own priority levels on processes. To change the priority
when issuing a new command you do nice -n [nice value]
[command]:
nice -n 10 apt-get upgrade
This will increment the default nice value by a positive 10
for the command, apt-get upgrade This is often useful for
times when you want to upgrade apps but dont want the
extra process burden at the given time. Remember a
positive number is gives less priority for a process.

Setting Priority on Existing Processes


Obviously at some point you are going to want to alter the
nice value of something that is already running. Just the
other day I was doing an upgrade to Ubuntu Jaunty and
Firefox started to become unusably slow. A quick renice
command rescheduled the upgrade to a lower priority and I
was back to surfing in no time.
To change the priority of an existing process just do renice
[nice value] -p [process id]:
renice 10 -p 21827
This will increment the priority of the process with an id of
21827 to 10.

Note: Only root can apply negative nice values.

Shell scripting:
The shell provides you with an interface to the UNIX
system.
It gathers input from you and executes programs
based on that input.
A shell is an environment in which we can run our
commands, programs, and shell scripts. There are
different flavors of shells.

Shell prompt:
The prompt, $, which is called command prompt, is
issued by the shell. While the prompt is displayed,
you can type a command.
For eg: $ date

Case-esac program in shell scripting:


Syntax:
case word in

pattern1)
Statement(s) to be executed if pattern1 matches ;;
pattern2)
Statement(s) to be executed if pattern2 matches;;
pattern3)
Statement(s) to be executed if pattern3 matches;;
Esac

Example 1:
echo Enter any value between 1 to 5
read num
case $num in
1)echo ONE ;;
2)echo TWO;;
3)echo THREE;;
4)echo FOUR;;
5)echo FIVE;;
*)echo INVALID VALUE;;
esac
Note: * indicates default value.

Ifelse:
Comparisons:
-eq equal to
-ne not equal to
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to

If-else program:
Syntax:
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi

Example 2:
valid_password=welcome

Echo Enter your password


read PSWD
if [ $PSWD = $valid_password ]
then
echo Valid password
else
echo Access denied
fi

Example 3:
Echo Enter the number
Read num
If test $num le 30
Then
echo Number is less than equal to 30
else
echo Number is greater than 30
fi

if then elif:

Syntax:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi

Example 4:
echo enter the value of a
read a
echo enter the value of b
read b

if [ $a = $b ]
then
echo a is equal to b
elif [ $a gt $b ]
then
echo a is greater than b
elif [ $a lt $b ]
then
echo a is less than b
else
echo INVALID VALUE
fi

While loop:
Syntax:
while command
do
Statement(s)
done

Example 5:

echo Enter the value of a


read a
while [ $a -lt 5 ]
do
a=`expr $a + 1`
echo $a
done

For loop:
Syntax:
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done

Example 6:
for((c=1;c<=5;c++))
do
echo Welcome to STC

done

Until loop:
Syntax:
until command
do
Statement(s)
Done

Example 7:
a=1
until [ $a ge 5 ]
do
echo $a .Welcome to STC
a=$(($a + 1))
done

PASSING PARAMETERS:
Example 8:
(inside a shell program)
Echo First value is $1

Echo Second value is $2


Echo Third value is $3
(outside shell program, during execution)
sh name.sh 10 20 30

Breaking out of loops:


The break statement is used to terminate the execution of
the entire loop, after completing the execution of all of the
lines of code up to the break statement. It then steps down
to the code following the end of the loop.

Syntax:
Break

Example 9:
a=0
while [ $a lt 10 ]
do
echo $a
if [ $a eq 5 ]
then
break

fi
a=`expr $a + 1`
done

The continue statement:


The continue statement is similar to the break command,
except that it causes the current iteration of the loop to
exit, rather than the entire loop.
This statement is useful when an error has occurred but
you want to try to execute the next iteration of the loop.

Syntax:
Continue

Example 10:
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"

continue
fi
echo "Found odd number"
done

LOGICAL OPERATORS:
&& -------- >

Returns True, if both condition1 and

condition2 are True (Like AND operator)


||

-------- >

Returns True, If any one condition is

True (Like OR operator)

Boolean Operators:
There are following boolean operators supported by Bourne
Shell.
Assume variable a holds 10 and variable b holds 20 then:
Operator
!

-o
-a

Description
This is logical negation.
This inverts a true
condition into false and
vice versa.
This is logical OR. If one of
the operands is true then
condition would be true.
This is logical AND. If both

Example
[ ! false ] is true.

[ $a -lt 20 -o $b -gt
100 ] is true.
[ $a -lt 20 -a $b -gt

the operands are true then


condition would be true
100 ] is false.
otherwise it would be false.

Example 11: (For || operator)


echo Enter the value of a
read a
echo Enter the value of b
read b
if (( a == 1 )) | | (( b == 8 ));then
echo Enable!!!
else
echo Disable
fi

FILE TEST OPERATORS:


The different file test operators are listed below:
a : True if the file exists.
b : True if the file exists and is a block special file.

c : True if the file exists and is a character special file.


d : True if the file exists and is a directory.
e : True if the file exists.
f : True if the file exists and is a regular file.
g : True if the file exists and its SGID bit is set.
h : True if the file exists and is a symbolic link.
k : True if the file exists and its sticky bit is set.
p : True if the file exists and is a named pipe (FIFO).
r : True if the file exists and is readable.
s : True if the file exists and has a size greater than zero.
t : True if file descriptor is open and refers to a terminal.
u : True if the file exists and its SUID (set user ID) bit is set.
w : True if the file exists and is writable.
x : True if the file exists and is executable.
O : True if the file exists and is owned by the effective user
ID.
G : True if the file exists and is owned by the effective group
ID.
L : True if the file exists and is a symbolic link.

N : True if the file exists and has been modified since it was
last read.
S : True if the file exists and is a socket.

Syntax: (mostly used in if-loop)


if [ -option filename ]
then
do something
else
do something
fi

Example 12: (bud.sh is an already existing file)


file=bud.sh
If [-r $file ]

(similarly, for write if [ -w $file ]

and for execute if [ -x $file ] )


Then
Echo Read access
Else
Echo Read permission denied
fi

Example 13:
File=directory1
If [ -d $file ]
Then
Echo Directory is present
Else
Echo Directory is not present
fi

Debugging Shell Scripts:


-x option to debug a shell script
Run a shell script with -x option.
$ Bash -x script-name
$ bash -x domains.sh

Use of set built in command


Bash shell offers debugging options which can be turn on
or off using set command.
Set -x: Display commands and their arguments as they are
executed.

Set -v: Display shell input lines as they are read.

How to execute:
Bash x filename.sh
Bash v filename.sh

You might also like