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

Introduction to Shell Scripting - Session 1

Uploaded by

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

Introduction to Shell Scripting - Session 1

Uploaded by

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

Shell Scripting

Session # 1

1
Why Shell?
 Shell is the term used to mark the command
interpreter in unix.
 Different shells are there in unix such as csh, ksh
and bash
 For Linux, the Bash is the default
 Why Shell?
 For routing jobs, such as system administration, without
writing programs
 Examples: Backing up a data, updating software on group
of machines..

2
Writing a Script
 Use text editor to create first shell script.
 Open a file first.sh
 Type the below code

#!/bin/sh #Before you add anything


else to your script, you need to
alert the system that a shell
script is being started.
To execute,

% chmod 777 first.sh #chmod 777


foldername will give read, write,
and execute permissions for everyone
%./first.sh 3
Syntax
 Variables
 Conditions
 Control
 Lists
 Functions
 Shell Commands
 Result
 Document

4
Variables
 Variables needed to be declared, note it is case-sensitive
(e.g. foo, FOO, Foo)
 Add ‘$’ for storing values
% salutation=“yes dear”
% salutation=Hello
% echo $salutation
% echo $salutation
yes dear
% read salutation
Hello
Hola!
% salutation=7+5
% echo $salutation
% echo $salutation 5

Hola!
Quoting
 Edit a “vartest.sh” file
#!/bin/sh

myvar=“Hi there”

echo $myvar Hi There


echo “$myvar” Hi There

echo `$myvar`
echo \$myvar $myvar

echo Enter some text


read myvar
echo `$myvar` now equals $myvar
exit 0 6
Shell script to accept two numbers as input and
find the sum of them and display the result
#!/bin/sh
echo enter number1
read n1
echo enter number2
read n2
n3=`expr $n1 + $n2` // backtick or back quote
echo sum of the two numbers are $n3

a=10
b=20

sum=$(( $a + $b ))

echo "Sum is: $sum" 7


 Shell script to accept two numbers as input and
find the product of them and display the result

n3=`expr $n1 \* $n2`

8
 Write a shell script to generate a bill. The items
purchased are 10 pens and 15 pencils. Each
pen costs Rs. 10/- and each pencil costs Rs. 5/-.
Generate the bill and display the total amount
to be paid

9
Environment Variables
 $HOME home directory
 $PATH path
 $$ process id of the script
 $# number of input parameters
 $0 name of the script file

10
Condition need space !

 test or ‘ [ ‘
if test –f fred.c If [ -f if [ -f fred.c ];then
then fred.c ] ...
... then fi
...
fi
fi
expression1 –eq expression2 -d file if directory
expression1 –ne expression2 -e file if exist
expression1 –gt expression2 -f file if file
expression1 –ge expression2 -g file if set-group-id
expression1 -lt expression2 -r file if readable
expression1 –le expression2 -s file if size >0
!expression -u file if set-user-id
-w file if writable
String1 = string2
-x file if executable
String1 != string 2
-n string (if not empty 11
string)
-z string (if empty string)
Control Structure
Syntax #!/bin/sh
if condition echo “Is it morning? Please answer yes or no”
read timeofday
then
if [ $timeofday = “yes” ]; then
statement
echo “Good morning”
else
else
statement echo “Good afternoon”
fi fi
exit 0

Is it morning? Please answer yes or no


yes
Good morning
12
Condition Structure
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ]; then
echo “Good morning”
elif [ $timeofday = “no” ]; then
echo “Good afternoon”
else
echo “Sorry, $timeofday not recongnized. Enter yes or no”
exit 1
fi
exit 0

13
Condition Structure
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ “$timeofday” = “yes” ]; then
echo “Good morning”
elif [ $timeofday = “no” ]; then
echo “Good afternoon”
else
echo “Sorry, $timeofday not recongnized. Enter yes or no”
exit 1
fi
exit 0

If input “enter” still returns Good morning


14
1. Write a shell script to read 3 numbers and
find the greatest of the 3 numbers.

15
2. Write a shell script to read the marks of a student
in OS course and assign the grade
O – 85-100, A – 75-84, B – 60-74, C – 50-59, F – 0-
49

3. Write a shell script to calculate the discount and


final bill amount. If bill amount is less than 100, 2%
discount, if bill is less than 500, 5% discount and if
bill amount is greater than 500, 10% discount.

16
4. Write a shell script to implement a simple
calculator which has the four basic operations:
Addition, Subtraction, Multiplication and Division.

5. Write a shell script to calculate the time


difference in seconds when two time values are
provided in hh:mm:ss format.

17

You might also like