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

Linux-shell_scripting

The document provides an overview of basic shell scripting, including the structure of shell scripts, execution methods, and various types of variables such as numeric, string, and arrays. It also covers control statements like if, while, and for loops, along with command substitution and exporting variables. Additionally, it includes examples and resources for further learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Linux-shell_scripting

The document provides an overview of basic shell scripting, including the structure of shell scripts, execution methods, and various types of variables such as numeric, string, and arrays. It also covers control statements like if, while, and for loops, along with command substitution and exporting variables. Additionally, it includes examples and resources for further learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Basic shell scripting

CS 2204
Class meeting 7

*Notes by Doug Bowman and other members of the


CS faculty at Virginia Tech. Copyright 2001-2003.
Shell script/program
 A series of shell commands placed in
an ASCII text file
 Commands include
 Anything you can type on the command
line
 Shell variables
 Control statements (if, while, for)

(C) Doug Bowman, Virginia Tech,


2001 2
Resources
 Review UIAN chapter 4
 Online
 Advanced bash-scripting guide
http://www.tldp.org/LDP/abs/html/index.html
 Bash Reference Manual
http://www.gnu.org/manual/bash-2.05a/bashref.h
tml
 ksh Reference Manual
http://www.bolthole.com/solaris/ksh.html

(C) Doug Bowman, Virginia Tech,


2001 3
Script execution
 Provide script as an argument to the shell
program (e.g. bash my_script)
 Or specify which shell to use within the script
 First line of script is #!/bin/bash
 Make the script executable using chmod
 Make sure the PATH includes the current directory
 Run directly from the command line
 No compilation is necessary!

(C) Doug Bowman, Virginia Tech,


2001 4
Simple example script
#!/bin/bash

echo “Hello world!”


cd ~
pwd Output:
Hello world!
/home/grads/sgifford

(C) Doug Bowman, Virginia Tech,


2001 5
Quoting
 Quoting is necessary to use special characters
in a variable’s value or string
 ”” - shell only interprets $ and ‘‘ and \
 $ - variable substitution
 ` - Command substitution
 \” - Literal double quote
 \ is used to escape characters
 echo ‘`date +%D`‘ will print: 10/06/03
 ’’ - shell doesn’t interpret special characters
 echo ‘`date +%D`‘ will print: `date +%D`
(C) Doug Bowman, Virginia Tech,
2001 6
Shell variables
 Numeric  Command line arguments
 Strings  Functions
 Arrays  Read only
 var refers to the name, $var to the value
 t = 100 #Sets var t to value 100
 echo ”\$t = $t” #will print: $t = 100
 Remove a variable with unset var
 Names begin with alpha characters and include alpha,
numeric, or underscore

(C) Doug Bowman, Virginia Tech,


2001 7
Numeric variables
 Integer variables are the only pure numeric
variables that can be used in bash
 Declaration and setting value:
declare -i var=100
 Expressions in the style of C:
 (( expression ))
 e.g. (( var+=1 ))
 +, -, *, /, %, &, |, ~, <, >, <=, >=, ==, !=, &&, ||

(C) Doug Bowman, Virginia Tech,


2001 8
String variables
 If you do not use the declare
keyword with option –i when using a
variable for the first time, it will be a
string
 var=100 makes var the string ‘100’.
 However, (( var=100 )) will treat var
as an integer even though it is a string

(C) Doug Bowman, Virginia Tech,


2001 9
Array variables
 Array is a list of values
 Don’t have to declare size
 Reference a value by ${name[index]}
 ${a[3]}
 $a (same as ${a[0]})
 Use the declare -a command to declare an
array
 declare –a sports
 sports=(ball frisbee puck)
 sports[3]=bat
(C) Doug Bowman, Virginia Tech,
2001 10
Arrays
 Array initialization
 sports=(football basketball)
 moresports=(${sports[*]} tennis)
 ${array[@]} or ${array[*]}
refers to the entire array contents
 echo ${moresports[*]} produces

football basketball tennis


(C) Doug Bowman, Virginia Tech,
2001 11
Command line arguments
 If arguments are passed to a script,
they can be referenced by $1, $2, $3,

 $0 refers to the name of the script
 $@ - array filled with arguments
excluding $0
 $# - number of arguments
(C) Doug Bowman, Virginia Tech,
2001 12
Exporting variables
 The export command, when used with
a variable name, allows child processes
of the shell to access the variable

(C) Doug Bowman, Virginia Tech,


2001 13
Output
 We have already seen echo
 More common in other shells including
ksh is print (does not exist in bash)
 echo –n does not print newline after
output

(C) Doug Bowman, Virginia Tech,


2001 14
Return values
 Scripts can return an integer value
 Use exit N
 The variable $? will contain the return
value of the last command run
 Can be used to test conditions

(C) Doug Bowman, Virginia Tech,


2001 15
Conditions
 If using integers: (( condition ))
 If using strings: [[ condition ]]
 Examples:
 (( a == 10 ))
 (( b >= 3 ))
 [[ $1 = -n ]]
 [[ ($v != fun) && ($v != games) ]]
 Special conditions for file existence, file
permissions, ownership, file type, etc.
(C) Doug Bowman, Virginia Tech,
2001 16
Conditions (continued…)
 [[ -e $file]] – File exists?
 [[ -f $file]] – Regular file?
 [[ -d $file]] – Directory?
 [[ -L $file]] – Symbolic link?
 [[ -r $file]] – File has read permission?
 [[ -w $file]] – File has write permission?
 [[ -x $file]] – File has execute perm?
 [[ -p $file]] – File is a pipe?
(C) Doug Bowman, Virginia Tech,
2001 17
If statements
 Syntax:
if condition
then
statements
elif condition
then
statements optional
else
statements
fi
(C) Doug Bowman, Virginia Tech,
2001 18
If statement
 Example
if [[ -r $fname ]]
then
echo ’$fname is readable’
elif [[ -w $fname && -x $fname ]]
then
echo ’$fname is writeable and
executable’
fi
(C) Doug Bowman, Virginia Tech,
2001 19
For loops
 Syntax:
for var [in list]
do
statements
done
 If list is omitted, $@ is assumed
 Otherwise ${list[*]}
 where list is an array variable.
(C) Doug Bowman, Virginia Tech,
2001 20
For loop example
for colors in Red Blue Green
Yellow Orange Black Gray White
do
echo $colors
done
echo

(C) Doug Bowman, Virginia Tech,


2001 21
While loops
 Syntax:
while condition
do
statements
done
 The keywords break, continue, and
return have the same meaning as in
C/C++
(C) Doug Bowman, Virginia Tech,
2001 22
Case statements
 Syntax:
case expression in
pattern1)
statements ;;
pattern2)
statements ;;

*)
statements ;;
esac
(C) Doug Bowman, Virginia Tech,
2001 23
Case statement example
case $1 in
-a)
statements related to option a ;;
-b)
statements related to option b ;;
*)
all other options ;;
esac

(C) Doug Bowman, Virginia Tech,


2001 24
Command substitution
 Use the output of a command in a
variable, condition, etc. by:
 `command`
 $(command)

(C) Doug Bowman, Virginia Tech,


2001 25
Examples
 Arguments printed in a for loop
 Reformatting the wc command
 Performing a depth-first search

(C) Doug Bowman, Virginia Tech,


2001 26

You might also like