shell会将每个参数分配给对应的变量,变量可以分配数字,也可以分配字符串。
[注意]每个参数都是用空格分隔的,所以shell会将空格当成两个值的分隔符。要在参数值中包含空格,必须要用引号(单引号或双引号均可)。
*/1 * * * * /root/shell/checkprocess.sh "/usr/sbin/mysqld" "/root/shell/mysqldstart.sh" 。*/1 * * * * /root/shell/checkprocess.sh "/usr/sbin/httpd" "/root/shell/httpdstart.sh" 。shell = /share/cachedev1_data/.qpkg/xunleiyuanchengqnap/xunleiyuanchengqnap.sh为shell = /share/cachedev1_data/.qpkg/script/startxunleiyuanchengqnap.sh。
Hello Rich Blum, glad to meet you.
在第9个变量之后,你必须在变量数字周围加上花括号,比如${10}。
如:total=$[ ${10} * ${11} ]
可以用 $0 参数获取shell在命令行启动的脚本名。这在编写多功能工具时很方便。
"basename" 命令会返回不包含路径的脚本名。
另外,箭头键,[home],[end],[delete]键与bash shell中一样用法。yubana147:~ home$ -bash: u88:~/source/qsno_cvs home$ printenv path/bin:/sbin:/usr/bin:/sbin:/users/home/bin:/usr/local/bin -bash: -bash:: command not found。shell=/bin/bash shell变量指定了系统要使用哪个shell,这里是bash 。
The script name is: test2.sh
/* 不同功能的脚本 */
#!/bin/bash
# Testing a Multi-function script
#
name=$(basename $0)
#
if [ $name = "addem" ]
then
total=$[ $1 + $2 ]
#
elif [ $name = "multem" ]
then
total=$[ $1 * $2 ]
fi
#
echo
echo The calculated value is $total
[~/shell/4]$: cp test6.sh addem
[~/shell/4]$: chmod u+x addem
[~/shell/4]$: ln -s test6.sh multem
[~/shell/4]$: ./addem 2 5
The calculated value is 7
[~/shell/4]$: ./multem 2 5
The calculated value is 10
在使用参数前一定要检查其中是否存在数据。如,使用了-n测试来检查命令行参数$1中是否有数据。
if [ -n "$1" ]
then
echo Hello $1, glad to meet you.
else
echo "Sorry, you did not identify yourself. "
fi
特殊变量$#含有脚本运行时携带的命令行参数的个数。