$# 是传给脚本的参数个数 $ 0 是脚本本身的名字 $ 1 是传递给该shell脚本的第一个参数 $ 2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过 9 个 $$ 是脚本运行的当前进程ID号 $? 是显示最后命令的退出状态, 0 表示没有错误,其他表示有错误 |
区别:@*
- 相同点:都是引用所有参数
- 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数(分别存储在12 3)则"*" 等价于 “12 3"(传递了一个参数);而“@" 等价于 "1""2" "$3"(传递了三个参数)
一个守护脚本 demon:
################### # VERSION=0.0.4 ################### #!/bin/bash ############################### ####Global parameters########## ############################### set -x #echo on CUR_DIR=`pwd` VA_LOG_LEVEL="err" VA_RUN_LEVEL="demon" ####for record file delete DEFAULT_DAYS=15 RECORD_FILE_PATH=/var/video_record DEFAULT_FILE_CHECK=1 ##### unit : G DEFAULT_MINI_SPACE=15 PROCESS_NUM=2 #CertMS #DarwinStreamingServer #CertMS_Server #Darwin_Server #functions ################### ##parse argument ################### usage() { echo "######################################" echo "usage of maintain.sh :" echo "options " echo "-l : videomon log level [warn , info , err] , default is err" echo "-m : videomon run mod[demon , foreground] , default is demon" echo "-c : record file delete check! [ 0:not check 1:check , default is 1]" echo "-n : [Delete the record files of the n days ago , default is 15 days]" echo "-d : [record file path , default is :/var/video_record]" echo "For example: ./maintain.sh -l info -m foreground -c 1 -n 10 -d /video_record or ./maintain.sh " echo "-? : usage info" echo "######################################" } get_opt() { while getopts ":?l:m:c:n:d:" optname do case "$optname" in "l") VA_LOG_LEVEL=$OPTARG ;; "m") VA_RUN_LEVEL=$OPTARG ;; "c") DEFAULT_FILE_CHECK=$OPTARG ;; "n") DEFAULT_DAYS=$OPTARG ;; "d") RECORD_FILE_PATH=$OPTARG ;; "?") usage exit 0 ;; *) # Should not occur echo "maintain.sh :Unknown error while processing options" exit 0 ;; esac done } #################### ##Video_Monitor clean #################### video_monitor_clean() { for (( i = 0 ; $i < PROCESS_NUM; i++ )); do echo $i; pid_videomon=`ps aux|grep -v grep|grep "videomon$i" | grep "$"|sed -n '1P'|awk '{print $2}'` if [ $pid_videomon ] then killall -9 videomon$i fi done } #################### ##ran_videomon_monitor #################### ran_videomon_monitor() { echo $1 cd $CUR_DIR/bin$1 if [ $VA_RUN_LEVEL == "demon" ] then nohup ./videomon$1 -l $VA_LOG_LEVEL -p pid -d & else nohup ./videomon$1 -l $VA_LOG_LEVEL -p pid & fi echo Start videomon Success! } #################### ##record file check #################### record_file_check() { cd $CUR_DIR if [ -f record_file_delete.sh ] then chmod +x record_file_delete.sh ### pid_record_sh=`ps aux|grep -v grep|grep "record_file_delete.sh" | grep "$"|sed -n '1P'|awk '{print $2}'` if [ -z $pid_record_sh ] then nohup ./record_file_delete.sh -n $DEFAULT_DAYS -d $RECORD_FILE_PATH -s $DEFAULT_MINI_SPACE & else echo "record_file_delete.sh is running!" fi ### else echo "Can not find record_file_delete.sh" return 0 fi } #################### ##videomon_maintain #################### videomon_maintain() { while true do sleep 2 ####Check whether need to delete record files if [ $DEFAULT_FILE_CHECK -eq 1 ] then record_file_check fi for (( i = 0 ; $i < PROCESS_NUM; i++ )); do echo $i; pid=`ps aux|grep -v grep|grep "videomon$i -l $VA_LOG_LEVEL" | grep "$"|sed -n '1P'|awk '{print $2}'` if [ -z $pid ] then ran_videomon_monitor $i fi done done } ############### ##start process ############### get_opt $@ if [ $? != 0 ] then echo get_opt unknow options! exit 1 fi video_monitor_clean for (( i = 0 ; $i < PROCESS_NUM; i++ )); do ran_videomon_monitor $i done videomon_maintain
参考http://www.cnblogs.com/kaituorensheng/p/4002697.html