shell参数 传递

$# 是传给脚本的参数个数

$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 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 [email protected]
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

时间: 2024-08-07 03:23:05

shell参数 传递的相关文章

Linux Shell参数替换

Linux Shell参数替换 2013-06-03 10:01 by 轩脉刃, 1816 阅读, 0 评论, 收藏, 编辑 Bash中的符号的作用是参数替换,将参数名替换为参数所代表的值.对于 来说,大括号是可选的,即A和 {A}代表同一个参数. ${}带冒号的有下面几种表达式: ${parameter:-word} 如果parameter为null或者未设置,整个参数替换表达式值为word ${parameter:=word} 如果parameter为null或者未设置,整个参数替换表达式值

Shell脚本传递带有空格的参数

在另一博文<Shell脚本实现DB2数据库表导出到文件>中实现了通过脚本实现将DB2数据库导出到文件,需要传入七个参数,最后一个是一个带有空格字符串,所以传入的时候有点问题,会自动识别空格,默认会将空格前的当作第7个参数,以下是传入的参数: MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first 100000 rows only 最后的“fe

shell如何传递外部参数给文件

shell里面如何传递参数: Js代码   sh test.sh zhang 20 那test.sh里面咋接受参数呢? Js代码   #!/usr/bin/env sh name=$1 age=$2 echo "name is ${name} & age is ${age}" 输出:name is yaochun & age is 20 $# 获取参数的个数 $*  获取全部参数 [email protected]  获取全部参数 扩展阅读: http://bbs.ch

Shell脚本传递带有空格的参数[摘录自网络]

参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 [email protected] 与$#相同,但是使用时加引号,并在引号中返回每个参数. $- 显示Shell使用的当前选项,与set命令功能相同. $? 显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误. MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/exp

set -- 传递变量到shell 参数

set -- The -- is the standard "don't treat anything following this as an option"   不把后面的string 看成选项(直接看成参数) [[email protected] ~]# cat set2.sh#!/bin/bash var="1 2 3"echo $var set -- $var #do not treat - as option ,only take $var as $1

shell 参数

位置参数 所谓位置参数 (positional parameter) ,指的是 Shell 脚本的命令行参数 (argument) ;同时也表示在 Shell 函数内的函数参数.它们的名称是以单个的整数来命名.出于历史的原因,当这个整数大于 9 时,就应该以花括号( {} )括起来 : echo first arg is $1 echo tenth arg is ${10} 特殊变量参数 $# 提供传递到 Shell 脚本或者函数的参数总数. $*,[email protected] 一次表示所

shell参数中空格

在使用shell传参数时. 解析参数时请不要用shift  action="$1" case "$action" in     status)         shift         [email protected]         exit $?     ;;     ...      *)         help         exit -1     ;; esac 因为就算有双引号,也无法再次传递. 而是应该确定的按参数追逐个传递.     stat

Linux - Shell - 参数获取

概述 参数 背景 复习一下 shell 脚本的参数获取 场景 os centos7 1. 参数: 基础 概述 简单描述 参数 1. 获取参数 获取 第一个 参数 获取参数 使用 ${num} 获取参数 ${1} 获取 第一个参数 代码 #!/bin/bash var1=${1} echo ${var1} 其他 可以尝试 ${2}, ${3} 等获取后续参数 默认情况下, 如果参数不足, echo 不会报错, 其他命令, 需要验证 2. 获取多个参数 问题1: 获取多个参数 一个一个获取参数, 如

C++ 数组长度 以及 数组名作为参数 传递给函数

在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有strlen() (需要#include <string.h>) 一个办法是用 sizeof() 一.首先定义数组 int array[] = {18,10,15,7,1}; 一开始想都没想就直接在子函数里面 int array_length(int a[]){ int len = sizeof(