使用输入参数执行shell脚本

时间:2016-10-17 15:53:40

标签: linux shell

下面的shell脚本会询问用户的小时和分钟。当您输入这两个输入时,它会安排成像。当我尝试以root身份运行此脚本时: bash -x /run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging 13 56。我收到以下错误:typset:'13': not a valid identifier and typset:'56': not a valid identifier.

我的问题是如何在一行中使用其参数执行此脚本。我也尝试了./run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging << "16 16"sh /run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging 16 50,但是他们没有工作我得到了同样的错误。

#!/bin/bash
    DEBUG="";
    #DEBUG="set -x";
    eval $DEBUG;
    typeset inputHour=2;
    typeset inputMinute=0;
    typeset crontabsFile="/etc/crontab";
    typeset tmp_crontabsFile="/tmp/crontab.tmp";
    typeset imagingsh=$BIN_DIR/autoimaging.sh;
    function validateNumber
    {
      eval $DEBUG;
      typeset -i vInput=$1;
      if [[ $? != 0 ]]
      then
        return 1;
      fi
      typeset -i vBase=$2;
      if [[ $? != 0 ]]
      then
        return 1;
      fi

      if (( vInput < 0 || vInput >= vBase ))
      then
        return 1;
      fi
      return 0;
    }
    function updatecrontabs
    {
      eval $DEBUG;
      typeset hour=$1;
      typeset minute=$2;

      #remove any entries that already have the autoimaging.sh
      /bin/grep -v "${imagingsh}" $crontabsFile > ${tmp_crontabsFile};   

      cat <<- EOF >> $tmp_crontabsFile
    $inputMinute $inputHour * * * $imagingsh
    EOF
      crontab ${tmp_crontabsFile}  
      if [[ $? != 0 ]]
      then
        return 1;
      fi
      cp ${tmp_crontabsFile} ${crontabsFile}; 
    }
    # If this script is invoked with two parameters, parse the two parameters as Hour and Minute
    # Otherwise, get input from user.
    if [[ $# != 2 ]]
    then
      echo " Configure Auto Imaging ";
      while ( true )
      do
        echo -n "Please input the hour when the Auto Imaging is to be executed: ";
        read inputHour
        if [[ -n $inputHour ]]
        then
          validateNumber $inputHour 24;
          if [[ $? == 0 ]]
          then
            break;
          else
            echo "You must input the number between 0 and 24";
        echo " ";
          fi
        else
          echo "You must input the number between 0 and 24";
          echo " ";
         fi 
      done
      while ( true )
      do
        echo -n "Please input the minute when the Auto Imaging is to be executed: ";
        read inputMinute
        if [[ -n $inputMinute ]]
        then
          validateNumber $inputMinute 60;
          if [[ $? == 0 ]]
          then
            break;
          else
            echo "You must input the number between 0 and 60";
        echo " ";
          fi
        else
          echo "You must input the number between 0 and 60";
          echo " ";
        fi  
      done
    else
      inputHour=$1;
      inputMinute=$2;
      typeset vResult=validateNumber $inputHour 24;
      if [[ $vResult != 0 ]]
      then
        echo "The number for hour must be between 0 and 24";
        exit 1;
      fi
      vResult= validateNumber $inputMinute 60;
      if [[ $vResult != 0 ]]
      then
        echo "The number for minute must be between 0 and 60";
        exit 1;
      fi 
    fi
    # Begin update the entry: auto imaging in crontabs
    updatecrontabs $inputHour $inputMinute;
    if [[ $? == 0 ]]
    then
      echo "update crontabs successfully! The auto imaging will be executed at: " $inputHour ":" $inputMinute;
      echo " ";
    else
      echo "Errors happen during update crontabs";
      echo " "
      exit 1;
    fi
    rm ${tmp_crontabsFile} > /dev/null;
    exit 0;

2 个答案:

答案 0 :(得分:1)

这似乎很可疑:

typeset vResult=validateNumber $inputHour 24;

它没有调用该函数,您需要使用$(validateNumber $inputHour 24)或反引号。

同样,以下行可能是错误的:

vResult= validateNumber $inputMinute 60;

清除$vResult并以validateNumber$inputMinute作为参数调用60

如果我使用数字作为第一个参数调用typeset,我可以得到相同的错误,但我在代码中看不到这种用法。您确定粘贴了导致问题的版本吗?

a=12 ; typeset -i $a=b
bash: typeset: `12=b': not a valid identifier

答案 1 :(得分:0)

我已经编辑了我的代码部分,如下所示:它现在正在运行:

validateNumber $inputHour 24;
      if [[ $? != 0 ]]

validateNumber $inputMinute 60;
      if [[ $? != 0 ]]