String Operators
Curly-Bracket Syntax
${varname} returns the value of variable varname, null if it is not
set.
Other string handling operators allow you to
- Ensure variables exist (i.e., are not null)
- Set default values for variables
- Catch errors resulting from variables not being set
- Remove parts of variable values matching patterns
Substitution Operators
Test for the existence of variables, and allow substitutions
- ${varname:-word} return value of varname if it exists and isn't null;
otherwise return word
- ${varname:=word} return value of varname if it exists and isn't null;
otherwise set it to word and then return its value
- ${varname:?message} return value of varname if it exists and isn't
null; otherwise print varname: message then abort the current command or
script
- ${varname:+word} return word if varname exists and isn't null; otherwise
return null
Pattern-matching Operators
Delete patterns from a variable's contents
- ${varname#pattern} if pattern matches the beginning of the value of
varname then delete the shortest part that matches and return the rest
- ${varname##pattern} if pattern matches the beginning of the value of
varname then delete the longest part that matches and return the rest
- ${varname%pattern} if pattern matches the end of the value of varname
then delete the shortest part that matches and return the rest
- ${varname%%pattern} if pattern matches the end of the value of varname
then delete the longest part that matches and return the rest
Regular Expressions
Regular expressions extend the string-matching power of the Korn shell
beyond *, ?, and [].
- *(exp) matches 0 or more occurrences of exp
- +(exp) matches 1 or more occurrences of exp
- ?(exp) matches 0 or 1 occurrences of exp
- @(exp1|exp2|…) matches exp1
or exp2 or …
- !(exp) matches anything but exp
Command Substitution
Command substitution allows you to use the standard output of a command
as if it were the value of a variable:
$(command)
Examples:
- $(ls)
- $(< filename) or $(cat filename)
- mail $(who | cut -d' ' -f1)
More Control-Flow Commands
case
Similar to the switch in C and case in Pascal
- Compares a string to a sequence of patterns
- Each pattern may actually be several patterns separated by |
- ;; breaks out of each case
- The * pattern matches all patterns, used as default
Syntax:
case expression in
pattern1 )
statements ;;
pattern2 )
statements ;;
esac
case example
for fname in $* ; do
case $fname in
*.c )
objname=${fname%.c}.o
gcc -c $fname -o $objname ;;
*.s )
objname=${fname%.s}.o
as $fname $objname ;;
*.o ) ;;
* )
print "error: $fname is wrong
type"
return 1 ;;
esac
done
select
Syntax:
select name [ in list ]
do
statements that can use $name
done
This flow-control construct
- Generates a menu of each item in list (which defaults to "$@"
if omitted)
- Prompts the user for a number using the prompt string in shell variable
PS3
- Stores the selected choice in the variable name and the selected number
in shell variable REPLY
- Executes the statements in the body
- Repeats forever (until break encountered)
select example
(from page 147 in "Learning the Korn Shell")
The code
print 'Select your terminal type: '
PS3='terminal? '
select term in gl35a t2000 s531 vt99 ; do
if [[ -n $term ]] ; then
TERM=$term
print TERM is $TERM
break
else
print 'invalid'
fi
done
will produce the following menu:
Select your terminal type:
1) gl35a
2) t2000
3) s531
4) vt99
terminal?
Here Document
Syntax:
command << label
input line 1
…
input line n
label
This allows you to redirect input to a shell script from within the
shell script itself.
- << label indicates that label marks the end of the here document
- label must appear on a line by itself to end the here document
- << 'label' prevents the shell from doing parameter and command
substitution in the here document
- <<- label deletes leading tabs (but not spaces) from the here
document
Here Document Example
(from page 189 in "Learning the Korn Shell")
pgmname=$1
for user in $(ypcat passwd | cut -f1 -d:)
do
mail $user <<- EOF
Dear user,
A new version of $pgmname has been installed
in $(whence $pgmname).
Regards,
Your friendly sysadmin
EOF
done