Oscar Sayin Oscar Sayin - 1 month ago 13
Bash Question

Execute a shell script with input parameter

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;

Answer

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

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

and

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