0

The shell script below asks the user for hour and minute. When you enter these two inputs it schedules for imaging. When I try to run this script as root like: bash -x /run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging 13 56. I get the following error: typset:'13': not a valid identifier and typset:'56': not a valid identifier.

My question is how can I execute this script with its parameter in one line. I also tried ./run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging << "16 16" and sh /run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging 16 50 but they didn't work I got same error.

#!/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;
1
  • learn to use shell debug/trace features, i.e. set -vx; PS4='${LINENO} #>' To turn off, use set +vx;unset PS4. Good luck. Commented Oct 17, 2016 at 16:14

2 Answers 2

1

This seems suspicious:

typeset vResult=validateNumber $inputHour 24;

It doesn't call the function, you need to use $(validateNumber $inputHour 24) or backticks.

Similarly, the following line is probably wrong:

vResult= validateNumber $inputMinute 60;

It clears $vResult and calls validateNumber with $inputMinute and 60 as parameters.

I am able to get the same error if I call typeset with a number as the first argument, but I don't see such usage in your code. Are you sure you pasted the version that caused the problem?

a=12 ; typeset -i $a=b
bash: typeset: `12=b': not a valid identifier
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I am sure, When I execute it without parameters like: bash -x /run/opt/corp/cmtg/2.0.0/sesm/admin/scripts/configure_gwcautoimaging, it asks me Please input the hour when the GWC Auto Imaging is to be executed: and after I entered for hour it asks for minute that I want to enter.
@OscarSayin: Please, reduce the script as per Short, Self Contained, Correct Example. I'm not going to run code that touches crontab files.
0

I have edited my code parts as below and it is working now:

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

and

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.