SlideShare a Scribd company logo
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022 (Computer)
Experiment # 5
Title:
• BASH Shell Scripting – Part II
Objective:
• To learn how write and run shell scripts.
Information:
While statement syntax: Until statement syntax:
while condition_is_true
do
execute commands
done
example
while [ $num -gt 100 ]
do
sleep 5
done
until false
do
execute commands
done
COMMANDS:
1. Numbers in reverse order
This Command is used to write script to print numbers as 5,4,3,2,1 using while loop.
#!/bin/bash
# Linux Shell Scripting Tutorial
#
i=5
while test $i != 0
do
echo "$i"
let i=i-1
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022 (Computer)
Output:
2. Numbers in order
This command is used to write script to print numbers as 0 … 9 using while loop.
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
Output:
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022 (Computer)
3. Numbers in reverse order
This command is used to write script to print numbers as 20 … 10 using until loop.
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Output:
4. Check is a file exists or not
This command is used to determine whether given file exist or not, file name is
supplied as command line argument, also check for sufficient number of command line
argument.
#!/bin/bash
# Linux Shell Scripting
if [ $# -ne 1 ]
then
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022 (Computer)
echo "Usage - $0 file-name"
exit 1
fi
if [ -f $1 ]
then
echo "$1 file exists"
else
echo "Sorry, $1 file does not exist"
fi
Output:
5. Reading user input with read
This command is used to write a shell script to read the name of the user from keyboard
and print it back to screen. Use read command.
#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
Output:
EE-332 Operating System
Department of Electrical Engineering | The University of Faisalabad
Zeeshan Iqbal BEE-FA18-022 (Computer)
6. Reading user input with read
This command is used to write a shell script to read the name (first and last names) of
the user from keyboard and print it back to screen. Use read command.
#!/bin/bash
echo Please, enter your firstname and lastname
read FN LN
echo "Hi! $LN, $FN !"
Output:

More Related Content

PDF
Php 5.5
PDF
Erlang and Elixir
PDF
Bootstrap |> Elixir - Easy fun for busy developers
PDF
Damien seguy php 5.6
PDF
Shell Programming_Module2_Part2.pptx.pdf
PDF
PPT
390aLecture05_12sp.ppt
PDF
BASH Shell Scripting – Part III &IV
Php 5.5
Erlang and Elixir
Bootstrap |> Elixir - Easy fun for busy developers
Damien seguy php 5.6
Shell Programming_Module2_Part2.pptx.pdf
390aLecture05_12sp.ppt
BASH Shell Scripting – Part III &IV

Similar to BASH Shell Scripting – Part II (20)

PPTX
Linux Shell Scripting
PPT
Shell Scripting
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
PDF
Course 102: Lecture 8: Composite Commands
PPT
34-shell-programming asda asda asd asd.ppt
PPTX
Shell programming 1.ppt
PPT
34-shell-programming.ppt
PPTX
BWU_BTA_22_508_cSd how os are implemented.pptx
PDF
Operating_System_Lab_ClassOperating_System_2.pdf
PPT
Shell programming
PPTX
Licão 05 scripts exemple
PDF
PPT
Shell scripting - By Vu Duy Tu from eXo Platform SEA
PPTX
OSYMP_(2)[1] micro project - Read-Only.pptx
PPTX
Bash Shell Scripting
PPT
PPTX
Easiest way to start with Shell scripting
PDF
Types of Linux Shells
PPT
Bash shell
Linux Shell Scripting
Shell Scripting
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
Course 102: Lecture 8: Composite Commands
34-shell-programming asda asda asd asd.ppt
Shell programming 1.ppt
34-shell-programming.ppt
BWU_BTA_22_508_cSd how os are implemented.pptx
Operating_System_Lab_ClassOperating_System_2.pdf
Shell programming
Licão 05 scripts exemple
Shell scripting - By Vu Duy Tu from eXo Platform SEA
OSYMP_(2)[1] micro project - Read-Only.pptx
Bash Shell Scripting
Easiest way to start with Shell scripting
Types of Linux Shells
Bash shell
Ad

More from Zeeshan Iqbal (11)

PDF
Bankers Algorithm in Operating System
PDF
Deadlock Prevention in Operating System
PDF
Round Robin Algorithm in Operating System
PDF
Shortest job first scheduling
PDF
Introduction to shortest job first scheduling
PDF
Programming Under Unix and Linux
PDF
Introduction to base shell scripting
PDF
Working with files and directories in Linux
PDF
Introduction to command line interface.
PDF
Introduction of Linux
PDF
Pakistan study notes
Bankers Algorithm in Operating System
Deadlock Prevention in Operating System
Round Robin Algorithm in Operating System
Shortest job first scheduling
Introduction to shortest job first scheduling
Programming Under Unix and Linux
Introduction to base shell scripting
Working with files and directories in Linux
Introduction to command line interface.
Introduction of Linux
Pakistan study notes
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Business Ethics Teaching Materials for college
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
RMMM.pdf make it easy to upload and study
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
Business Ethics Teaching Materials for college
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
RMMM.pdf make it easy to upload and study
Week 4 Term 3 Study Techniques revisited.pptx
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial disease of the cardiovascular and lymphatic systems
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program

BASH Shell Scripting – Part II

  • 1. EE-332 Operating System Department of Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) Experiment # 5 Title: • BASH Shell Scripting – Part II Objective: • To learn how write and run shell scripts. Information: While statement syntax: Until statement syntax: while condition_is_true do execute commands done example while [ $num -gt 100 ] do sleep 5 done until false do execute commands done COMMANDS: 1. Numbers in reverse order This Command is used to write script to print numbers as 5,4,3,2,1 using while loop. #!/bin/bash # Linux Shell Scripting Tutorial # i=5 while test $i != 0 do echo "$i" let i=i-1
  • 2. EE-332 Operating System Department of Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) Output: 2. Numbers in order This command is used to write script to print numbers as 0 … 9 using while loop. #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done Output:
  • 3. EE-332 Operating System Department of Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) 3. Numbers in reverse order This command is used to write script to print numbers as 20 … 10 using until loop. #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done Output: 4. Check is a file exists or not This command is used to determine whether given file exist or not, file name is supplied as command line argument, also check for sufficient number of command line argument. #!/bin/bash # Linux Shell Scripting if [ $# -ne 1 ] then
  • 4. EE-332 Operating System Department of Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) echo "Usage - $0 file-name" exit 1 fi if [ -f $1 ] then echo "$1 file exists" else echo "Sorry, $1 file does not exist" fi Output: 5. Reading user input with read This command is used to write a shell script to read the name of the user from keyboard and print it back to screen. Use read command. #!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!" Output:
  • 5. EE-332 Operating System Department of Electrical Engineering | The University of Faisalabad Zeeshan Iqbal BEE-FA18-022 (Computer) 6. Reading user input with read This command is used to write a shell script to read the name (first and last names) of the user from keyboard and print it back to screen. Use read command. #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !" Output: