$# 是传给脚本的参数个数 $ 0 是脚本本身的名字 $ 1 是传递给该shell脚本的第一个参数 $ 2 是传递给该shell脚本的第二个参数 [email protected] 是传给脚本的所有参数的列表 $* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过 9 个 $$ 是脚本运行的当前进程ID号 $? 是显示最后命令的退出状态, 0 表示没有错误,其他表示有错误 |
Difference:@*
- Same point: All parameters are referenced
- Different points: only in double quotes. Suppose that three parameters were written when the script was run (stored in123) then " * "Equivalent to"123"(Mass transferHandedThe one Ginseng number ) ;" @ "equivalent to" 1""2 "" $ $ "(three parameters passed)
A daemon script demon:
#################### VERSION=0.0.4####################!/bin/bash################################## #Global parameters######################################## #set-X #EchoOncur_dir=`pwd' Va_log_level="Err"Va_run_level="Demon"#### forRecordfiledeletedefault_days= theRecord_file_path=/var/Video_recorddefault_file_check=1##### Unit:gdefault_mini_space= theProcess_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 The N days ago, default is ~ 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 () { whileGetopts":? l:m:c:n:d:"optname Do Case "$optname" inch "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 Exit0 ;; *) # should not occurEcho "maintain.sh:Unknown Error while processing options"Exit0 ;; Esac Done}##################### #Video_Monitor clean################### #video_monitor_clean () { for((i =0; $i < Process_num; i++ )); Do Echo$i; Pid_videomon=`PSAux|grep-Vgrep|grep "videomon$i"|grep "$"|sed-N'1P'|awk '{print $}'` if[$pid _videomon] Then Killall-9videomon$ifi Done}##################### #ran_videomon_monitor ################### #ran_videomon_monitor () {Echo$1CD $CUR _dir/bin$1 if[$VA _run_level = ="Demon" ] Thennohup./videomon$1-L $VA _log_level-p pid-d &Elsenohup./videomon$1-L $VA _log_level-p PID &fi EchoStart Videomon success!}##################### #recordfilecheck################### #record_file_check () {CD $CUR _dirif[-F Record_file_delete.SH ] Then chmod+x Record_file_delete.SH# # Pid_record_sh=`PSAux|grep-Vgrep|grep "record_file_delete.sh"|grep "$"|sed-N'1P'|awk '{print $}'` if[ -z $pid _record_sh] Thennohup./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"return0 fi}##################### #videomon_maintain ################### #videomon_maintain () { while true Do Sleep 2# # # #Check whether need to delete record filesif[$DEFAULT _file_check-eq1 ] ThenRecord_file_checkfi for((i =0; $i < Process_num; i++ )); Do Echo$i; PID=`PSAux|grep-Vgrep|grep "videomon$i-l $VA _log_level"|grep "$"|sed-N'1P'|awk '{print $}'` if[ -Z $pid] Thenran_videomon_monitor $ifi Done Done}################ #start process############## #get_opt [email protected]if[ $? !=0 ] Then EchoGet_opt Unknow options!Exit1fiVideo_monitor_clean for((i =0; $i < Process_num; i++ )); Doran_videomon_monitor $i DoneVideomon_maintain
Reference http://www.cnblogs.com/kaituorensheng/p/4002697.html
Shell parameter passing