unix shell programming / sh / csh syntax

Shell script syntax

Bourne shell (sh) syntax samples

test:
[ number -lt|-le|-eq|-ne|-ge|-gt number ]
[ string = != string ]

if [ test ]
then
   commands
elif [ test ]
   commands
else
   commands
fi

for var in item1 item2 item3
do
  commands
done

while test
do
  commands
done

case expression in
  case1)         commands ;;
  case2|case3)   commands ;;
  *)             default-commands ;;
esac


# How to read an input file into shell variables:
while read variable1 variable2
do
  ...
done < $input_file

# How to redirect stdout/stderr
echo something 1>&2
echo something 2>&1

# How to throw out stdout and stderr
some_command > /dev/null 2>&1

# Shell functions:
func () {
	echo $1 $2 $3
}
func a b c


C shell (csh) syntax samples

test:
( expression ==|!=|>>|<< expression )

if ( test ) command

if ( test ) then
   commands
else if ( test ) then
   commands
else
   commands
end if

foreach var ( list list list )
  commands
end

while condition
   commands
end

# How to throw out stdout and stderr
some_command >& /dev/null

另一個

#!/bin/sh
count=0
while [ $count -lt 5 ]
do
  count=`expr $count + 1`
  echo $count
done