Introduction To UNIX: Lecture Eight: 8.1 Objectives
Introduction To UNIX: Lecture Eight: 8.1 Objectives
Introduction to UNIX:
Lecture Eight
8.1 Objectives
This chapter covers:
1 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
and then try the man command (say man pwd), the page will go
flying past without stopping. If you now say:
2 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
and you typed ls, the shell would look for /bin/ls, /usr/bin/ls
etc. Note that the PATH contains'.', i.e. the current working
directory. This allows you to create a shell script or program
and run it as a command from your current directory
without having to explicitly say "./filename".
The shell script begins with the line "#!/bin/sh" . Usually "#"
denotes the start of a comment, but #! is a special
combination that tells UNIX to use the Bourne shell (sh) to
interpret this script. The #! must be the first two characters
of the script. The arguments passed to the script can be
accessed through $1, $2, $3 etc. $* stands for all the
arguments, and $# for the number of arguments. The
process number of the shell executing the script is given by
3 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
$ ls -l simple
-rw-r--r-- 1 will finance 175 Dec 13 simple
$ chmod +x simple
$ ls -l simple
-rwxr-xr-x 1 will finance 175 Dec 13 simple
$ ./simple hello world
The number of arguments is 2
The arguments are hello world
The first is hello
My process number is 2669
Enter a number from the keyboard:
5
The number you entered was 5
$
if [ test ]
then
commands-if-test-is-true
else
commands-if-test-is-false
fi
4 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
-s file
true if file exists and is not empty
-f file
true if file is an ordinary file
-d file
true if file is a directory
-r file
true if file is readable
-w file
true if file is writable
-x file
true if file is executable
$X -eq $Y
true if X equals Y
$X -ne $Y
true if X not equal to Y
$X -lt $Y
true if X less than $Y
$X -gt $Y
true if X greater than $Y
$X -le $Y
true if X less than or equal to Y
$X -ge $Y
true if X greater than or equal to Y
"$A" = "$B"
true if string A equals string B
"$A" != "$B"
true if string A not equal to string B
$X ! -gt $Y
true if string X is not greater than Y
$E -a $F
true if expressions E and F are both true
$E -o $F
true if either expression E or expression F is true
for loops
5 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
while loops
while [ test ]
do
statements (to be executed while test is true)
done
#!/bin/sh
while [ ! -s input.txt ]
do
echo waiting...
sleep 5
done
echo input.txt is ready
You can abort a shell script at any point using the exit
statement, so the following script is equivalent:
#!/bin/sh
while true
do
if [ -s input.txt ]
echo input.txt is ready
exit
fi
echo waiting...
sleep 5
done
6 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
case statements
case variable in
pattern1)
statement (executed if variable matches
pattern1)
;;
pattern2)
statement
;;
etc.
esac
7 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
arithmetic operations
8 of 9 04/11/2019, 3:04 PM
Introduction to UNIX and Linux: Lecture 8 http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture...
With csh, to add the directory ~/bin to your PATH, you can
include the line:
set path = ( $PATH $HOME/bin )
in your .cshrc.
9 of 9 04/11/2019, 3:04 PM