CIS 240 Introduction to UNIX Instructor: Sue Sampson.

24
CIS 240 Introduction to UNIX Instructor: Sue Sampson

Transcript of CIS 240 Introduction to UNIX Instructor: Sue Sampson.

CIS 240

Introduction to UNIX Instructor: Sue Sampson

CIS240 – Advanced Shell ProgrammingBasic Rules for Writing Expressions You must have at least one space before and one

space after parens used to group expressions. You must have at least one space before and one

space after brackets that substitute for the test command

Some shells require you to enclose variable names in quotes when used in a test command

You must have no spaces between variable/value in an assignment statement.

CIS240 – Advanced Shell ProgrammingFile Testing • -d <file> True if <file> is a directory• -f <file> True if <file> is a file• -r <file> True if <file> is readable• -s <file> True if <file> length != 0• -w <file> True if <file> is writable• -x <file> True if <file> is executable

CIS240 – Advanced Shell Programming

Test for Integers

• <i1> -eq <i2> True if <i1> equals <i2>• <i1> -ge <i2> True if <i1> is greater than or equal to <i2>• <i1> -gt <i2> True if <i1> is greater than <i2>• <i1> -le <i2> True if <i1> is less than or equal to <i2>• <i1> -lt <i2> True if <i1> is less than <i2>• <i1> -ne <i2> True if <i1> is not equal to <i2>

CIS240 – Advanced Shell Programming

Test for Strings

<s> True if <s> is not empty <s1> = <s2> True if <s1> and <s2> are the same <s1> != <s2> True if <s1> and <s2> are not the same -n <s> True if the length of <s> is greater than zero -z <s> True if the length of <s> is zero

CIS240 – Advanced Shell Programming

Comments start with a pound sign (#). Shell headers are comments at the beginning of a shell script

that provide information to users and programmers. Adding a header and comments makes your shell script easier

for others to use. The #!/bin/sh you see at the beginning of script examples in

your book is more than a comment. File must be located in an executable directory; echo $PATH The current shell reads that line and uses the shell specified in the

path to run the script Script must be executable, chmod

CIS240 – Advanced Shell ProgrammingPath Change There are two methods to add directories to the $PATH

variable: Type PATH=$PATH:<the absolute path you want to add> Add the path to your PATH statement in .bash_profile in

your home directory.  The PATH statement is about midway down in the file.

Example:  You have a script in a directory called /home/Sampson.  You would enter the following command: PATH=$PATH:/home/Sampson

Note: in RH9, your path statement includes a home/<user>/bin directory, but no directory exists… create the directory and run the scripts from there.

CIS240 – Advanced Shell Programming# Comment line… typically used to indicate shell#!/bin/shif test [ -f “$1” ]then

filename=“$1”set `ls –il $filename`inode=“$1”size=“$6”echo –e “Name\tInode\tSize”echoecho –e “$filename\t$inode\t$size”exit 0

fi

CIS240 – Advanced Shell ProgrammingGrave Accents Backwards single quote (above the tab key

on your keyboard) Used for command substitution Example:

echo “The date and time is `date`.”The date and time is Tue Jan 27 09:48:45 CST 2004

CIS240 – Advanced Shell Programming

expr <args> Converts numbers in an expression

from strings to integers. By default every variable is stored as a string.

Evaluates the expression arguments, ‘args’, and sends the results to the standard output.

CIS240 – Advanced Shell ProgrammingInteger OperatorsComparison: <i1> = <i2> True if <i1> equals <i2> <i1> \>= <i2> True if <i1> is greater than or equal to <i2> <i1> \> <i2> True if <i1> is greater than <i2> <i1> \<= <i2> True if <i1> is less than or equal to <i2> <i1> \< <i2> True if <i1> is less than <i2> <i1> != <i2> True if <i1> is not equal to <i2> <i1> \| <i2> Return first if not null, else return second <i1> \& <i2> Return first if neither null, else return 0

Arithmetic:

+, -, \*, /, % Add, subtract, multiply, divide, remainder

CIS240 – Advanced Shell Programming

Format:

expr `<args>`

Example:# var1=10

# var1=`expr $var1 + 1`

# echo $var1

# 11

CIS240 – Advanced Shell Programming

Command Line Parameters

$0 the name of the script being executed $1,$2,$3 input parameters $# the number of parameters $* all the parameters in a single string $$ the process identifier $? the value returned by the last command executed $! The process identifier of the last background process invoked

CIS240 – Advanced Shell Programming

clarg1.sh

if [ $1 ]

then

echo “The value of the first argument is $1”

else

echo “There is no argument 1”

fi

$ sh clarg1.sh

$ There is no argument 1

CIS240 – Advanced Shell Programming

clarg2.sh

x=0 x=0

for i in $1 $2 $3 $4 for i

do do

x=`expr $x + $i` x=`expr $x + $i`

done done

echo “The sum is $x” echo “The sum is $x”

$ sh clarg2.sh 4 5 6 $ sh clarg2.sh 4 5 6

$ The sum is 15 $The sum is 15

CIS240 – Advanced Shell ProgrammingC Shell Variables Follow many of the same conventions as

Bourne Shell names First character is upper or lower case a-z Next characters are upper or lower case

letters, numbers, or underscores The maximum length depends on your

version of UNIX

CIS240 – Advanced Shell ProgrammingC Shell Environment Variables Are reserved words Use lower case letters Some are named differently than those in Bourne

Shell

Bourne Shell C ShellHOME homePATH path PS1 promptPS2 prompt2

CIS240 – Advanced Shell ProgrammingCommand Line Parameters C Shell recognizes the same convention as Bourne

shell ($0, $1, $2 …) C Shell also recognizes the C programming language

convention for command line input (argv) if ( $argv[1] )

then echo “The value of the first argument is $argv[1]” else echo “There is no argument 1”

endif

CIS240 – Advanced Shell Programming

Control Structures if Decision making statement foreach Counting loop while Decision loop switch Selection statement

CIS240 – Advanced Shell ProgrammingDecision (if) Format:

if (<expression>) then<1 or more statements>

else<1 or more statements>

endif

if ( -f a_test_file ) thenecho “file exists”

elseecho “file does not exist”

endif

CIS240 – Advanced Shell Programming

Counting Loop (foreach) Format:

foreach <variable> <argument list>

<1 or more statements>

end

foreach students ( Phachoen Alfredo Timothy Momodou )

echo “$students”

end

CIS240 – Advanced Shell Programming

Selection (Switch) Format:switch (<test string>)case pattern1:

<1 or more statements>breakswcase pattern2:

<1 or more statements>breakswdefault

<1 or more statements>breakswendsw

CIS240 – Advanced Shell Programmingswitch (“$guessword”)

case “fred”:

echo “guessword is fred”

breaksw

case “bill” | “bert”:

echo “guessword is either bill or bert”

breaksw

default:

echo “guessword was not matched”

breaksw

endsw

CIS240 – Advanced Shell ProgrammingRunning C Shell Scripts Create the shell script file Enter #!/bin/csh as the first line in the

file Make the file executable Enter the entire path of the file as a

command

/home/sue/bin/test