0% found this document useful (0 votes)
15 views

Control Instructions in Shell-9

Uploaded by

nitesh.codeace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Control Instructions in Shell-9

Uploaded by

nitesh.codeace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Control Instructions in Shell

There are four types of control instructions in shell-

1. Sequence Control Instructions


2. Selections and Decision Control Instruction
3. Repetition and loop control Instruction
4. Case Control Instruction
It is essential to have control over the order of execution of commands.The BASH offers 4
decision making instructions-
 If-then-fi statement
 If-then-else-fi statement
 If-then-elif-else-fi statement
 Case-Esac Statement

Simple if Example-
#ss13
echo enter source and target file names
read source target
If cp $source $target
then
echo File copied successfully
fi

Simple If-then-else-if example-


#ss14
echo enter source and target file names
read source target
If cp $source $target
then
echo File copied successfully
else
echo Failed to copy the file
fi

Exercise Question-

 In a company an employee is paid as under-


If his basic salary is less than Rs.1500,then HRA=10% of basic salary, and DA=90% of basic
salary.If his salary is either equal to or above Rs 1500, then HRA=Rs 500 and Da=98%of Basic
Salary.If the employers salary is input through the keyboard ,Write a program to find his gross .
salary.

Operators-(When we want to compare numbers)


-gt greater than
-ltless than
-gegreater than equal to
-leless than equal to
-nenot equal to
-eqequal to

File Tests-
We have a few operators that can be used to test various properties associated with a Unix file.Assume
a variable file holds an existing file name "test" the size of which is 100 bytes and has read, write and
execute permission on –

-e file exists

-a file exists This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.

-f file is a regular file (not a directory or device file)

-s file is not zero size

-dfile is a directory

-bfile is a block device

-cfile is a character device

-rif file exists and have read permission on it.

-w if file exists and have write permission on it.

-x if file exists and have execute permission on it.

-kIf file exists and sticky bit is set on it.

File Test in shell script


Q--Shell Script:- To check whether a file exist or not

echo Enter Filename


Read fname
If [ -f $fname ]
Then
Echo file exists
Else
Echo file does not exist
Fi

Q2—Shell script- To check whether the user has write permission to the file.If Yes then append
something into the file.

echo Enter Filename


Read fname
If [ -w $fname ]
Then
Echo file exists Happy Unix > f1.txt
Else
Echo No write permisiion
Fi

String Test

Here is the program to put these string test into action:-

str1="Good"
str2="Bad"
str3=
[$str1 = $str2 ]
echo $?
[$str1 != $str2 ]
echo $?
[ -n $str1 ]
echo $?
[-z "$str3" ]
echo $?
[-z $str3 ]
echo $?
["$str3"]
echo $?

Shell Script- Predict the output of the program?

Str1=”Good Morning”

Str2=”Good Bye”

[“$str1”=”$str2”]

Echo $?

(Since str1 and str2 are having two words , so we need to enclose them in double quotes)

Use of Logical Operators


1. AND(-a)
2. OR(-o)
3. NOT(!)

Shell script regarding the same-

echo "enter marks in five subjects \c"


read m1 m2 m3 m4 m5
add=`expr $m1 + $m2 + $m3 + $m4 + $m5`
per=`expr $add / 5`
echo percentage is $per
if [ $per -ge 60 ]
then
echo First Division
fi
if [ $per -ge 50 -a $per -le 60 ]
then
echo second division
fi
if [ $per -ge 40 -a $per -lt 50 ]
then
echo Third Division
fi
if [ $per -lt 40 ]
then
echo Fail
fi

Shell Script Examples-


echo "Enter filename"
read fname
if [ -w $fname -a -r $fname -a -x $fname ]
then
echo File has all permission !!! Happy Unix !!!
else
echo File has no permission
fi

Use of Nested else if with the elif clause


echo "Enter the adapter Name"
read adapter
if [ "$adapter" = "MA" ]
then
echo monochrome adapter
elif [ "$adapter" = "CGA" ]
then
echo colur graphic adapter
elif [ "$adapter" = "EGA" ]
then
echo enhanced graphic adapter
elif [ "$adapter" = "VGA" ]
then
echo Video graphic adapter
else
echo super video graphic adapter
fi

Case Control Structure


Another way of controlling the sequence of execution is using the case control structure.

Syntax with Example—

echo "Enter the number from 1 to 3:"


read num
case $num in
1)echo You entered 1 ;;
2)echo you entered 2 ;;
3) echo you entered 3 ;;
*)echo i said 1 to 3! ;;
esac

Tips and tricks -:

1. You can put the cases in any order you like.


2. The value portion of case statement can be a shell variable ,a shell script argument or output of
a command.
For example- case `who am I |cut –f1 ` in
Or
Case $1 in
3. If we want two choices options we can combine the options using the or (|) operator.
For example-
Case $1 in
Cat|dog)echo you supplied the name of animals ;;
Parrot|crow) echo You supplied the name of bird ;;
Whale|shark)echo You supplied the name of fishes ;;
*) echo you supplied an incorrect argument ;;
esac
4.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy