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

Substitution Operators

Test for the existence of variables, and allow substitutions

Pattern-matching Operators

Delete patterns from a variable's contents

Regular Expressions

Regular expressions extend the string-matching power of the Korn shell beyond *, ?, and [].

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:


More Control-Flow Commands

case

Similar to the switch in C and case in Pascal

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

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.

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