1 单引号和双引号的区别
单引号严格一些。它可以防止任何变量扩展。双引号可以防止通配符扩展但允许变量扩展。
2 Use case to select OS type
# Moved shell code from '.machtype' into here so that one less process
# is executed.
PLATFORM="unknown"
if [ -f /usr/5bin/uname ] ; then
OS_TYPE=`/usr/5bin/uname -r`
PLATFORM=`/usr/5bin/uname -m`
else
if [ -f /usr/bin/uname ] ; then
OS_TYPE=`/usr/bin/uname -r`
PLATFORM=`/usr/bin/uname -m`
else
if [ -f /bin/uname ] ; then
OS_TYPE=`/bin/uname -r`
PLATFORM=`/bin/uname -m`
fi
fi
# Note that SunOS 5.0 can run SunOS 4.x binaries, but not vice-versa.
# Note that SysV88 R4x can run SysV88 R32 binaries, but not vice-versa.
case $PLATFORM in
#############################################
# We are a Sun-4 running ...
#############################################
sun4*)
case $OS_TYPE in
4*)
PLATFORM=sun4 ;; # ... SunOS 4.x. We can't run ELF binaries.
5*)
PLATFORM=sun44 ;; # ... SunOS 5.x. We run both SunOS 4.x
# binaries and SunOS 5.x ELF binaries.
esac ;;
#############################################
# We're a Sun-3.
#############################################
sun|sun3*)
PLATFORM=sun3 ;;
#############################################
# We're running Solaris on x86
#############################################
i86pc)
PLATFORM=pc ;;
#############################################
# We're a 68K Moto VME.
#############################################
M68030)
PLATFORM=m68k ;;
#############################################
# We're an 88K MCG VME running R32.
# We can not run R4x ELF binaries.
#############################################
M88100)
PLATFORM=m88k ;;
#############################################
# We're an 88K MCG VME running R4x.
# We run both R32 COFF binaries as well as R4x ELF binaries.
#############################################
m88k)
PLATFORM=m88k4 ;;
#############################################
# We're an HP 9000 workstation.
#############################################
9000/7*)
PLATFORM=hp97 ;;
9000/*)
PLATFORM=hp9 ;;
esac
# End of .machtype code
3 联合逻辑运算
if [ "x$PLATFORM" != "x" -a ! -h /tmp/gsmtt/bin ]; then
! Unary negation operator. -a Binary and operator.
4 重定向(屏蔽)输入输出
rm -rf /tmp/tt >/dev/null 2>&1
5 exec
调用exec后面的命令将不会被执行(进程覆盖)
6 关于赋值时的空格--不要空格!
对:
a='$0'
错:
a = '$0'
a= '$0'
a = '$0'
Reference:
shell编程(1)
shell编程(2)
shell编程(3)
shell编程学习笔记(1)
shell编程学习笔记(2)
Bourne Shell及shell编程(1)