標籤:
$# 是傳給指令碼的參數個數 $ 0 是指令碼本身的名字 $ 1 是傳遞給該shell指令碼的第一個參數 $ 2 是傳遞給該shell指令碼的第二個參數 [email protected] 是傳給指令碼的所有參數的列表 $* 是以一個單字串顯示所有向指令碼傳遞的參數,與位置變數不同,參數可超過 9 個 $$ 是指令碼啟動並執行當前進程ID號 $? 是顯示最後命令的退出狀態, 0 表示沒有錯誤,其他表示有錯誤 |
區別:@*
- 相同點:都是引用所有參數
- 不同點:只有在雙引號中體現出來。假設在指令碼運行時寫了三個參數(分別儲存在12 3)則"*" 等價於 “12 3"(傳遞了一個參數);而“@" 等價於 "1""2" "$3"(傳遞了三個參數)
一個守護指令碼 demon:
#################### VERSION=0.0.4####################!/bin/bash###################################Global parameters#########################################set -x #echo onCUR_DIR=`pwd`VA_LOG_LEVEL="err"VA_RUN_LEVEL="demon"####for record file deleteDEFAULT_DAYS=15RECORD_FILE_PATH=/var/video_recordDEFAULT_FILE_CHECK=1##### unit : GDEFAULT_MINI_SPACE=15PROCESS_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 ;; esacdone}######################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 [email protected]if [ $? != 0 ]then echo get_opt unknow options! exit 1fivideo_monitor_cleanfor (( i = 0 ; $i < PROCESS_NUM; i++ ));do ran_videomon_monitor $idonevideomon_maintain
參考http://www.cnblogs.com/kaituorensheng/p/4002697.html
shell參數 傳遞