variables
- initialize and export a variable
export foo=value
- $* cut the arguments containing an IFS
character
"$@" does not do it (but the double quotes must be used)
#> set now "is the" time
#> for i in "$@"; do echo "($i)"; done
(now)
(is the)
(time)
#> for i in "$*"; do echo "($i)"; done
(now is the time)
#> for i in $@; do echo "($i)"; done
(now)
(is)
(the)
(time)
#> for i in $*; do echo "($i)"; done
(now)
(is)
(the)
(time)
- get the last argument
ksh allows any index
eval last=\${$#}
let index="$# - 1"
eval secondlast=\${index}
sh only allow index < 10
last=`shift \`expr $# - 1\`; echo "$1"`
secondlast=`shift \`expr $# - 2\`; echo "$1"`
test
- the syntax [...] use test
- the syntax [[ ... ]] execute the test by the shell
itself,
hence
a performance gain
arithmetics
- i=0
while
do
...
i=$(($i+1))
done
tricks
- test if a variable is numeric or not
if /bin/expr "$var" + 0 1>&- 2>&-
then
# numeric
else
# non numeric
endif
- get the status of a command at the begining of a pipe
status = `((command; echo $? 1>&3) | tee foo 3>&)
3>&1)
- filename wildcard
/usr/include/{.,*}/*.h
- avoid that Ctrl-D destroys the shell
set -o ignoreeof
- avoid to overwrite a file
set o noclobber
- to suspend a shell
suspend
to restart it, send a SIGCONT or use fg %1
- loop on the lines of a file
while read line
do
echo $line
done < $file
or
cat $file | while read line
do
echo $line
done
- retrieve the directory where is the executable
#!/bin/sh
if [ $0 = `basename $0` ]
then
myfullname=`which $0`
else
myfullname=$0
fi
mydir=`dirname $myfullname`
nawk -f $mydir/tfwSum.awk $*
- compute some result while letting the data go through the
script (i.e. it acts has a noop filter)
#!/bin/sh
exitcode=1
fifo=/tmp/myfifo$$
mkfifo $fifo
set -o monitor
tee $fifo &
result=`grep 'reportType="SubTestResult"' $fifo | sed
's/^.*result="\([^"]*\)".*$/\1/'`
[ "x$result" = "xpass" ] && exitcode=0
rm $fifo
exit $exitcode
Exemples
of scripts
Verification of the include syntax
Last update: March 9th,
2005 - Laurent