Download presentation
Presentation is loading. Please wait.
1
Chapter 9 Shell Programming
CSNB113 SYSTEM ADMINISTRATION College of Information Technology Universiti Tenaga Nasional (UNITEN) SN 2014
2
Objectives Discover how shell scripts are executed and the role of the interpreter line Make shell scripts interactive using read Use positional parameters to read command-line arguments Understand the significance of the exit status and the exit statement Learn comprehensive decision making with the if conditional Use a for loop to iterate with each element of a list Use a while loop to repeatedly execute a set of commands SN 2014
3
Shell Scripts Store a group of commands in a file and then execute it
It can be in any types of file; with, or without extension SN 2014
4
Creating a Simple Script
Create the shell script file using vi editor $ vi my_first_program #!/bin/bash echo "I am fine on: "`date` echo ls *.pdf echo -n "I am "; whoami SN 2014
5
Comment Use # to indicates comment
# on the first line – interpreter line - indicator for command interpreter # on the following line - starts the comment #!/bin/bash # this is my first program # written in December 2013 echo "I am fine on: "`date` echo # creates a new line ls *.pdf # list all PDFs echo # bla-bla-bla-lah! echo -n "I am "; whoami # done! SN 2014
6
Execution Basic three steps; Write command into a file
Make the file executable (‘x’); chmod Execute the file using “./fileName” Create a file $ vi my_first_program $ chmod 755 my_first_program $ ./my_first_program #!/bin/bash echo "I am fine on: "`date` echo ls *.pdf echo -n "I am "; whoami Change permission Execute SN 2014
7
Program Availability $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games SN 2014
8
Program Availability Make your new program available system-wide
This example shows the relevancy of the 'path'. The path is an environment variable, that the system administrator can set or change $ sudo cp my_first_program /usr/local/bin/ $ which my_first_program /usr/local/bin/my_first_program $ my_first_program I am fine on: Mon Jan 17 12:26:22 SGT 2011 yahoo.pdf I am surizal $ SN 2014
9
read read statement: internal tool for taking input from the user
Input is stored in the variable name Use quotes to assign multiple words to a single variable #!/bin/bash echo –n “Enter Your Name: “ read name echo “Hello $name. I hope you will enjoy the lecture” SN 2014
10
Positional Parameters
Scripts take user input from command-line arguments $ vi pp_program $ chmod 755 pp_program $ ./pp_program CSNB113 SystemAdmin SN010101 #!/bin/bash echo “You are executing program $0” echo “Hello $3.” echo “Your subject code is $1” echo “Subject name for $1 is $2” $0 $1 $2 $3 SN 2014
11
exit exit statement: Shell scripts return exit status; successful or fail $? : parameter stores the exit status of the last command; 0 (zero) if succeeds, and non-zero if fails SN 2014
12
if … then … fi if statement makes two-way decisions
Additional reference: Here Form 1 Form 2 Form 3 if condition is successful then execute commands fi else elif command is successful then … else … SN 2014
13
if … then … fi #!/bin/bash TEST=4 if [ $TEST -lt 5 ]; then
echo "less than five" fi #!/bin/bash TEST=6 if [ $TEST -lt 5 ]; then echo "less than five" else echo "five or more" fi SN 2014
14
for … do … done for variable in list do commands done
Perform a set of instruction repeatedly for variable in list do commands done #!/bin/bash for i in do echo "This is round $i" done SN 2014
15
while … do … done Perform a set of instruction repeatedly
Doesn’t work with a list Uses a control command to determine the flow of execution while condition is true do commands done #!/bin/bash TEST="no" while [ "$TEST" != "yes" ]; do read TEST done SN 2014
16
expr expr value1 operation value2 a=5 b=10 c=`expr $a + $b`
Command used to evaluate an expression – perform calculation expr value1 operation value2 a=5 b=10 c=`expr $a + $b` SN 2014
17
References Das, S. (2012). Your UNIX/LINUX The Ultimate Guide: Third Edition. McGraw-Hill Hahn, H. (2008). Harley Hahn's Guide to Unix and Linux. California: McGraw-Hill Higher Education SN 2014
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.