




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





7/15/2005 3 Department of Com
puter and Informati
on Sciences
Shell: Turn Interpretation OffShell: Turn Interpretation Off
Backslash – Double quotes – ” ” Single quotes – ’ ’
echo ’$HOME’ ”$HOME” $HOME echo My files are: ”*” echo ”My files are: *” echo ’My files are: *’
7/15/2005 4 Department of Com
puter and Informati
on Sciences
UAB Effect of Quoting on Special CharactersEffect of Quoting on Special Characters
newline NO NO NO ! YES NO NO
spaces NO NO NO
` (black quote) YES NO NO
” (double quote) - NO NO
’ (single quote) NO - NO
$ YES NO NO
; NO NO NO
> NO NO NO
| NO NO NO
< NO NO NO
{ } NO NO NO
[ ] NO NO NO
Character Inside ” ” Inside ’ ’ After \
7/15/2005 5 Department of Com
puter and Informati
on Sciences
SummarySummary
The backslash character and single quotes turns off interpretation for all special characters The double quotes turn off interpretation of all special characters except $, `, and \ (\ - only if the next character is interpreted)
The backslash character turns off interpretation for all special characters The single quotes turn off interpretation of all special characters except the! The double quotes turn off interpretation of all special characters except $, `, and!
7/15/2005 6 Department of Com
puter and Informati
on Sciences
MiscellaneousMiscellaneous
grep treats $ character as end of line (e.g., grep ';$' MyClass.javagrep ';$' MyClass.java – look for lines ending with ;)
echo ’ ”$USER” ’ echo ” ’$USER’ ” echo ’$USER”?’ $USER ’ ”$USER”’ echo ’$USER \’ ”Date is \” date \” $USER *”
$ aa=t* $ echo $aa '$aa' "$aa" t.c t1.c temp.java tmp typescript $aa t*
$ aa=’t’ $ echo $aa '$aa' "$aa" t.c t1.c temp.java tmp typescript $aa t
7/15/2005 10 Department of Com
puter and Informati
on Sciences
Example: script with complex argumentsExample: script with complex arguments
$ cat > cmdscript.sh echo 'The name of the script is: ' $ echo 'No. of command-line arguments = ' $# echo 'First three arguments are: ' $1 $2 $ echo 'Complete argument list is:' $* echo 'PID of this process = ' $$ $ chmod + cmdscript.sh $ ./ cmdscript.sh ”Hello World” ”$USER” ’$USER’ _The name of the script is: ./cmdscript.sh No. of command-line arguments = 3 First three arguments are: Hello World puri $USER Complete argument list is: Hello World puri $USER* PID of this process = 5828_ $ ./ cmdscript.sh a b c The name of the script is: ./cmdscript.sh No. of command-line arguments = 3 First three arguments are: a b c Complete argument list is: a b c PID of this process = 5831
7/15/2005 11 Department of Com
puter and Informati
on Sciences
Simple ArithmeticSimple Arithmetic
7/15/2005 12 Department of Com
puter and Informati
on Sciences
Obtaining Exit Status of ProcessesObtaining Exit Status of Processes
the shell interprets the command creates child process (or processes) completes input/output redirection and passes arguments instructs child process to execute appropriate program when program completes, shell receives the exit status code from the child process (every program returns an exit code on completion)
7/15/2005 13 Department of Com
puter and Informati
on Sciences
““testtest”” oror ““[[”” commandcommand
[ statement ][ statement ] – returns 0 to the parent process if statement is true, otherwise 1 [[^ - -f xyz ]f xyz ]^ – tests if^ xyz^ is a file, returns 0 if^ xyz^ is a file [ string ][ string ] – true if a single string is present [ x[ x – –eq y ]eq y ] – true if x is equal to y [ x[ x – –ne y ]ne y ] – true if x is not equal to y [ x[ x – –gt y ]gt y ] – true if x is greater than y [ x[ x^ – –lt y ]lt y ]^ – true if^ x^ is less than^ y [ x[ x – –ge y ]ge y ] – true if x is greater than or equal to y [ x[ x – –le y ]le y ] – true if x is less than or equal to y NOTE: space after [ and before ] required
$ [ -f /etc/passwd ] $ echo $? 0
$ [ 6 -eq 7 ] $ echo $? 1
$ [ 6 -lt 7 ] $ echo $? 0
$ echo $? 0
7/15/2005 14 Department of Com
puter and Informati
on Sciences
ifif……thenthen……elseelse
Syntax: ifif command (run command and obtain exit status) thenthen command (if exit status is 0, execute this) elseelse command (if exit status is not 0, execute this) fifi
OR
ifif^ condition^ ;^ thenthen^ action^ ;^ elseelse^ action2^ ;^ fifi
Example: if [ $1 ]if [ $1 ] thenthen if [if [ - -f $1 ]f $1 ] then echo File Existsthen echo File Exists else echo File does not existelse echo File does not exist fifi elseelse echo "Usage: $0 "echo "Usage: $0 " fifi
7/15/2005 15 Department of Com
puter and Informati
on Sciences
forfor
Syntax: forfor word inin wordlist dodo command donedone OR
forfor word [ inin wordlist ... ] ; dodo actions ; donedone
Example: for file in *.javafor file in *.java do do echo $file echo $file wc wc - -l $filel $file donedone OR for file in *.java ; do echo $file; wcfor file in *.java ; do echo $file; wc - -l $file; donel $file; done
7/15/2005 19 Department of Com
puter and Informati
on Sciences
Complete Shell ScriptComplete Shell Script
clear echo "1) grep $USER /etc/passwd" echo "2) ypcat passwd | grep $USER" echo "3) who | sort | awk '{print $1}' | uniq -c" echo "What command do you want to run?" echo Select 1, 2, or 3 read choice echo You have selected option $choice echo echo The output for the selected command is: case $choice in _1) grep $USER /etc/passwd ;;
7/15/2005 20 Department of Com
puter and Informati
on Sciences
Defining and Using FunctionsDefining and Using Functions
$ numfiles() { ls | wc $ } $ numfiles temp.java 14 33 245 temp.java $ numfiles 143 144 2004 $ ls | wc 143 144 2004
$ cat > myfunctions numfiles() { ls | wc $ } $. ./myfunctions $ typeset -F declare -f numfiles $
7/15/2005 21 Department of Com
puter and Informati
on Sciences
““getoptsgetopts””^ functionfunction
Usage: getopts options variablegetopts options variable Example: while getopts abc optionwhile getopts abc option
7/15/2005 22 Department of Com
puter and Informati
on Sciences
““getoptsgetopts”” exampleexample
$ cat getopts.sh while getopts abo: c do case $c in a | b) echo "Option selected is $c" ;; o) echo "Option selected is $c and it's arguments are $OPTARG" ;; ?) echo $USAGE; exit 2;; esac done
$ ./getopts.sh -o xxx,z,yy -b -a filename Option selected is o and it's arguments are xxx,z,yy Option selected is b Option selected is a $ ./getopts.sh -abo "xxx,z,yy" filename Option selected is a Option selected is b Option selected is o and it's arguments are xxx,z,yy
Note: You can use * as the default value in case statements
7/15/2005 23 Department of Com
puter and Informati
on Sciences
Final Notes:Final Notes:
while :while : dodo …… exit 0exit 0 …… donedone
case