Download presentation
Presentation is loading. Please wait.
Published byMax Nell Modified over 9 years ago
1
CIS 240 Introduction to UNIX Instructor: Sue Sampson
2
CIS240 – Advanced Shell Programming Basic 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.
3
CIS240 – Advanced Shell Programming File Testing -d True if is a directory -f True if is a file -r True if is readable -s True if length != 0 -w True if is writable -x True if is executable
4
CIS240 – Advanced Shell Programming Test for Integers -eq True if equals -ge True if is greater than or equal to -gt True if is greater than -le True if is less than or equal to -lt True if is less than -ne True if is not equal to
5
CIS240 – Advanced Shell Programming Test for Strings True if is not empty = True if and are the same != True if and are not the same -n True if the length of is greater than zero -z True if the length of is zero
6
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
7
CIS240 – Advanced Shell Programming Path Change There are two methods to add directories to the $PATH variable: Type PATH=$PATH: 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/ /bin directory, but no directory exists… create the directory and run the scripts from there.
8
CIS240 – Advanced Shell Programming # Comment line… typically used to indicate shell #!/bin/sh if test [ -f “$1” ] then filename=“$1” set `ls –il $filename` inode=“$1” size=“$6” echo –e “Name\tInode\tSize” echo echo –e “$filename\t$inode\t$size” exit 0 fi
9
CIS240 – Advanced Shell Programming Grave 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
10
CIS240 – Advanced Shell Programming expr 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.
11
CIS240 – Advanced Shell Programming Integer Operators Comparison: = True if equals \>= True if is greater than or equal to \> True if is greater than \ True if is less than or equal to \ True if is less than != True if is not equal to \| Return first if not null, else return second \& Return first if neither null, else return 0 Arithmetic: +, -, \*, /, %Add, subtract, multiply, divide, remainder
12
CIS240 – Advanced Shell Programming Format: expr ` ` Example: #var1=10 #var1=`expr $var1 + 1` #echo $var1 # 11
13
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
14
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
15
CIS240 – Advanced Shell Programming clarg2.shx=0 for i in $1 $2 $3 $4 for ido x=`expr $x + $i` doneecho “The sum is $x”$ sh clarg2.sh 4 5 6 $ The sum is 15 $The sum is 15
16
CIS240 – Advanced Shell Programming C 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
17
CIS240 – Advanced Shell Programming C Shell Environment Variables Are reserved words Use lower case letters Some are named differently than those in Bourne Shell Bourne ShellC Shell HOMEhome PATHpath PS1prompt PS2prompt2
18
CIS240 – Advanced Shell Programming Command 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
19
CIS240 – Advanced Shell Programming Control Structures ifDecision making statement foreachCounting loop whileDecision loop switchSelection statement
20
CIS240 – Advanced Shell Programming Decision (if) Format: if ( ) then else endif if ( -f a_test_file ) then echo “file exists” else echo “file does not exist” endif
21
CIS240 – Advanced Shell Programming Counting Loop (foreach) Format: foreach end foreach students ( Phachoen Alfredo Timothy Momodou ) echo “$students” end
22
CIS240 – Advanced Shell Programming Selection (Switch) Format: switch ( ) case pattern1: breaksw case pattern2: breaksw default breaksw endsw
23
CIS240 – Advanced Shell Programming switch (“$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
24
CIS240 – Advanced Shell Programming Running 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.