39. 1. 内部命令 shift 的作用 当位置变量个数超出 9 时,就不能直接引用位置大于 9 的位置变量了,必须用 shift 命令存取。每执行一次 shift 命令,删除$ 1 位置变量,并使其他的所有位置变量向左移动一个位置。 例: $ 0 $ 1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7 $ 8 $ 9 myprog a b c d e f g h i j shift b c d e f g h i j shift c d e f g h i j shift d e f g h i j
40. 2. 用 set 命令进行强制性赋值 位置变量可以使用 set 命令进行强制性赋值。 例: set Sun Mon Tue Wed Thu Fri Sat 结果$ 1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7 注意 : $ 0 是不能用这种方法赋值的。
41. 9.5.4 环境变量 shell 执行环境由一系列环境变量组成,这些变量是由 shell 维护和管理的。所有这些变量都可被用户重新设置,变量名由大写字母或数字组成。 CDPATH 执行 cd 命令时使用的搜索路径; HOME 用户的 home 目录; PATH 寻找命令或可执行文件的搜索路径; PS1 主命令提示符,默认为“$”; PS2 从命令提示符,默认为“ >” ; TERM 使用的终端类型。
42. 9.5.5 预定义的特殊变量 在 shell 中有一组特殊的变量,其变量名和变量值只有 shell 本身才可以设置。 “ $ #” 记录传递给 shell 的自变量个数。 例 1 : myprog a b c 则 $ # 的值为 3 例 2 : if test $ # -lt 2 then echo ″ two or more args required ″ exit fi
44. 9.5.6 变量替换 shell 在遇到未设置的变量时,将其值作为空串处理。而在实际应用中,对于未设置的变量,用户可以根据需要采用不同的处理方式,这可通过变量替换来实现。变量替换提供了三种功能:允许替换未设置变量的隐含值;允许对未设置变量赋值;在访问未设置变量时,提示出错信息。格式为, $ {var:-word} 例 1 : 假设$ PARM 未设置 $ echo ″ The value of PARM is $ {PARM:-undefined}″ The value of PARM is undefined $ echo $ PARM -
45. 例 2 :假设$ PARM 未设置 $ arg= $ {PARM:- ″ not defined ″} ( 注意 : 双引号 ) $ echo ′ $ arg : ′ $ arg $ arg : not defined 例 3 :对未设置变量赋值 $ cat use.d sel= $ 1 : $ {sel∶=main}( 注意 : 此处的“ :” 为空命令 ) echo ″ Your selection is $ sel ″ $ use.d programming Your selection is programming
46. 注意 : 变量替换的这种形式不允许使用位置变量,若要使用位置变量,则必须先将位置变量赋值给一中间变量,然后再对中间变量进行这种形式的替换。 例 4 :将例 3 中的空命令用中间变量替换。 $ cat use.d sel= $ 1 my-sel= $ {sel∶=main} echo ″ Your selection is $ my-sel ″ $ use.d programming Your selection is programming
47. 例 5 : 测试环境变量 TERM 是否设置。 $ cat check.env : $ {TERM:? ″ the TERM varible should be set ″ } 注意 : 出错信息以一行长为限,且输出到标准错误输出上。
81. 例: # The test codes for function definition GetYesOrNo() { while echo ″ $ *(Y/N)? \ c ″ > &2 do read reply RestData case ″ $ reply ″ in [ yY ] ) return 0 ;; [ nN ] )return 1 ;; *)echo ″ Please enter Y or N ! ″ >&2 ;; esac done 执行$ GetYesOrNo ″ Do you wish to continue ″ || exit 显示 Do you wish to continue(Y/ N)?
99. # 第一条语句先指定本 shell 程序采用哪个 shell 执行。 #! /bin/sh # 以 # 开始的语句是注释行。 # /etc/rc.d/rc.sysinit - run once at boot time # # Taken in part from Miquel van Smoorenburg′s bcheckrc. # # 下面语句是定义变量 PATH ,并把 PATH 变量声明为全局变量。 # Set the path PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH
100. # 下面命令读取配置数据,这里用到一个简单的 IF 语句。 # 判断 /etc/sysconfig/network 文件存在否,如果该文件存在,就执行它。 # 否则,给 NETWORKING 变量赋值,给 HOSTNAME 变量赋值。 # Read in config data. if [ -f /etc/sysconfig/network ] ; then . /etc/sysconfig/network else NETWORKING=no HOSTNAME=localhost fi
101. # 下面语句的功能是启用交换,执行 shell 命令 swapon 。 # Start up swapping. echo ″Activating swap partitions″ swapon a # 以下语句设置主机名。 # 下面语句引用 hostname 变量,执行 hostname 命令。 # Set the hostname. hostname $ {HOSTNAME} echo hostname: `hostname`
102. # 下面语句设置 NIS 域名,其中引用$ NISDOMAIN 变量,执行 shell 命令 domainname 。 # Set the NIS domain name if [ -n ″ $ NISDOMAIN″ ] ; then domainname $ NISDOMAIN else domainname ″″ fi # 下面是一段简单的 if 语句,判断 /fsckoptions 文件是否存在, # 并定义 fsckoptions 变量。 if [ -f /fsckoptions ] ; then fsckoptions=`cat /fsckoptions` else fsckoptions=′′ fi
103. # 下面是一段简单的 if 语句,判断 /fastboot 文件是否不存在。 # 其中,引用 fsckoptions 变量,执行 shell 语句 fsck 。 # 并引用系统变量$ ? ,定义变量 rc ,获取上一个 shell 语句的执行结果。 if [ ! -f /fastboot ] ; then echo ″Checking root filesystems.″ fsck -V -a $ fsckoptions / rc= $ ? # 以下这段语句是检查文件系统的执行结果, # 如果$ rc 大于 1 ,说明文件系统有严重问题,并显示信息。 # 其中,给环境变量 PS1 赋值,执行 shell 命令 sulogin , # 执行 shell 命令 umount a ,执行 shell 命令 reboot 。 # A return of 2 or higher means there were serious problems. if [$ rc -gt 1 ] ; then echo echo echo ″*** An error occurred during the file system check.″ echo ″*** Dropping you to a shell; the system will reboot″ echo ″*** when you leave the shell.″
104. PS1=″(Repair filesystem) #″; export PS1 sulogin echo ″Unmounting file systems″ umount a mount -n -o remount, ro/ echo ″Automatic reboot in progress.″ reboot # 下面语句是嵌套的复合条件 if 语句,执行 shell 命令 quotacheck elif [″$ rc″=″1″ -a -x/sbin/quotacheck ] ; then echo ″Checking root filesystem quotas″ /sbin/quotacheck -v/ fi fi
105. # 下面语句对根系统实行磁盘空间限定,执行 shell 命令 quotaon 。 if [ -x/sbin/quotaon ] ; then echo ″Turning on user and group quotas for root filesystem″ /sbin/quotaon/ fi # 下面命令是设置 PNP ,首先检查参数,执行 shell 命令 mount 。 # 在 if 语句中采用输出重定向,定义变量 PNP 。 # check for arguments mount -t proc/proc/proc if grep -i nopnp/proc/cmdline>/dev/null; then PNP= else PNP=yes fi
106. # 以下语句设置 PNP, 先使用一个简单 if 语句 , 复合条件,并引用变量 PNP 。 # 执行 shell 命令 isapnp 。 # set up PNP if [ -x /sbin/isapnp -a -f /etc/isapnp.conf ] ; then if [ -n ″ $ PNP″ ] ; then echo ″Setting up ISA PNP devices″ /sbin/isapnp/etc/isapnp.conf else echo ″Skipping ISA PNP configuration at users request″ fi fi
107. # 下面命令重新 mount 根文件系统,执行 shell 命令 mount # 如果 /etc/HOSTNAME 不存在,则把$ {HOSTNAME} 变量的值输出到 /etc/HOSTNAME 文件中。 # Remount the root filesystem read-write. echo ″Remounting root filesystem in read-write mode.″ mount -n -o remount,rw / if [ ! -f /etc/HOSTNAME ] ; then echo $ {HOSTNAME} > /etc/HOSTNAME fi # 清空 / etc/mtab 文件 # clear mtab >/etc/mtab
108. # 执行 shell 命令,把 / 和 /proc 文件系统加载上。 # Enter root and /proc into mtab. mount -f / mount -f /proc # 如果 /proc/ksyms 文件存在 , 定义变量 USEMODULES 为 y 。 if [ -f /proc/ksyms ] ; then USEMODULES=y else USEMODULES= fi
109. # 下面命令是获取要执行的模块。 # 先执行 shell 命令 rm 删除文件 , 引用变量 USEMODULES , # 并用 set 给位置变量强制赋值,引用系统变量$ # ,引用位置变量$ 1, # 在 if 语句中使用管道命令 , 使用输出重定向。 # Get the modules ready to go -- we use awk here as cut is in /usr/bin rm -f /lib/modules/preferred if [ -n $ USEMODULES ] ; then set `cat /proc/cmdline` while [$ # -gt 0 ] ; do if echo $ 1|grep ′^BOOT-IMAGE=′ > /dev/null ; then # # # 中间略去部分语句 # # 下面语句初始化串口 , 执行 shell 命令 rc.serial 。 # Initialize the serial ports. if [ -f /etc/rc.d/rc.serial ] ; then . /etc/rc.d/rc.serial fi
110. # 下面语句加载模块 , 执行 shell 命令 rc.modules 。 # Load modules (for backward compatibility with VARs) if [ -f /etc/rc.d/rc.modules ] ; then /etc/rc.d/rc.modules fi # 下面语句是检查 SCSI 磁带设备。 # If a SCSI tape has been detected, load the st module unconditionally # since many SCSI tapes don′t deal well with st being loaded and unloaded # 这里用到带复合条件的 if 语句,并使用管道命令和重定向命令。 if [ -f /proc/scsi/scsi ] && cat /proc/scsi/scsi|grep -q ′Type:Sequential-Access′ 2>/dev/null ; then if cat /proc/devices|grep -qv ′ 9 st′ ; then if [ -n ″ $ USEMODULES″ ] ; then # Try to load the module. If it fails, ignore it... modprobe st 2>/dev/null fi fi fi
111. # 下面执行 shell 命令 dmesg ,显示系统配置信息, # 并把输出结果重定向到 /var/log/dmesg 中。 # Now that we have all of our basic modules loaded and the kernel going, # let′s dump the syslog ring somewhere so we can find it later dmesg > /var/log/dmesg # 执行 shell 命令 random start 。 # Feed entropy into the entropy pool /etc/rc.d/init.d/random start