Docsity

Shell Programming: Quoting and Special Characters in Unix Operating System Fundamentals, Study notes of Computer Science

Toggle sidebar
bg1
1
CS
CS-
-344
344 -
-Unix Operating
Unix Operating
System Fundamentals
System Fundamentals
Lecture 6
Shell Programming
Based on slides created by
Dr. Bangalore for the
Spring 2005 offering of
the course
7/15/2005 3
Department of Com
puter and Informati
on Sciences
UAB
Shell: Turn Interpretation Off
Shell: Turn Interpretation Off
Shell interprets characters entered at the
command prompt
To turn off this interpretation we must use
Backslash – \
Double quotes – ” ”
Single quotes – ’ ’
What happens when the following commands are
used
echo ’$HOME’ ”$HOME” \$HOME
echo My files are: ”*”
echo ”My files are: *”
echo ’My files are: *’
pf3
pf4
pf5
pf8

Partial preview of the text

Download Shell Programming: Quoting and Special Characters in Unix Operating System Fundamentals and more Study notes Computer Science in PDF only on Docsity!

CS CS-- 344344 - - Unix OperatingUnix Operating

System FundamentalsSystem Fundamentals

Lecture 6

Shell Programming

Based on slides created by

Dr. Bangalore for the

Spring 2005 offering of

the course

7/15/2005 3 Department of Com

puter and Informati

on Sciences

UAB

Shell: Turn Interpretation OffShell: Turn Interpretation Off

 Shell interprets characters entered at the

command prompt

 To turn off this interpretation we must use

 Backslash –  Double quotes – ” ”  Single quotes – ’ ’

 What happens when the following commands are

used

 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

  • NO NO NO

Character Inside ” ” Inside ’ ’ After \

7/15/2005 5 Department of Com

puter and Informati

on Sciences

UAB

SummarySummary

 For the bash shell

 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)

 For the C shell

 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

UAB

MiscellaneousMiscellaneous

 Passing Special Characters to Utilities

 grep treats $ character as end of line (e.g., grep ';$' MyClass.javagrep ';$' MyClass.java – look for lines ending with ;)

 Single and double quotes can be mixed in

commands

 echo ’ ”$USER” ’  echo ” ’$USER’ ”  echo ’$USER”?’ $USER ’ ”$USER”’  echo ’$USER \’ ”Date is \” date \” $USER *”

 Interpreting special characters in variable names

$ 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

UAB

Simple ArithmeticSimple Arithmetic

 Using “bcbc”

a=8; echoa=8; echo ””$a * 9$a * 9”” | bc| bc (result is 8*9 = 72)

 Using “letlet” (assign values to variables and

to perform arithmetic calculations)

a=8a=

echo $aecho $a

let a=$a9let a=$a9 [could also use ((a=$a*9))]

echo $aecho $a [could use echo $((a=$a*9)) instead]

7/15/2005 12 Department of Com

puter and Informati

on Sciences

UAB

Obtaining Exit Status of ProcessesObtaining Exit Status of Processes

 When we type a command and press ENTER

 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)

 To obtain this exit status, type “echo $?” after the

command is entered

 An exist code “0” indicates program completed

successfully, otherwise something went wrong

7/15/2005 13 Department of Com

puter and Informati

on Sciences

““testtest”” oror ““[[”” commandcommand

 “testtest” or “[[” command used for making decision

[ 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

$ [ $USER ]

$ echo $? 0

7/15/2005 14 Department of Com

puter and Informati

on Sciences

UAB

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

UAB

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

An example script with various shell constructs

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 ;;

  1. ypcat passwd | grep $USER ;;
  2. who | sort | awk '{print $1}' | uniq -c ;;_ esac a=$? if [ $a -eq 0 ] then echo Command executed successfully else echo Command returned with the exit code $a fi

7/15/2005 20 Department of Com

puter and Informati

on Sciences

UAB

Defining and Using FunctionsDefining and Using Functions

 Similar to functions in other programming

languages we can instruct the shell to define

functions

 This function is not a file, it is not a script, it is

defined in memory

 You can also save these functions to a file

$ 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

UAB

““getoptsgetopts””^ functionfunction

 Used to retrieve options and option-arguments

from a list of parameters

Usage: getopts options variablegetopts options variable Example: while getopts abc optionwhile getopts abc option

 getoptsgetopts function examines the argument list for

options following a dash and then assigns the

first option that matches to the variable specified

 If one of the options requires an argument, it

must be followed by a colon

(e.g., getopts a:b:c: optiongetopts a:b:c: option)

 If no matching option is found, the variable

specified will be set to ?? character

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

UAB

Final Notes:Final Notes:

 Infinite loop:

while :while : dodo …… exit 0exit 0 …… donedone

 Selections:

case

  1. … ;;
  2. … ;; … 7|8|9) … ;; *) … ;; ecase