gpt4 book ai didi

linux - 使用输入参数执行 shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:43 25 4
gpt4 key购买 nike

下面的 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;

最佳答案

这看起来很可疑:

typeset vResult=validateNumber $inputHour 24;

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

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

vResult= validateNumber $inputMinute 60;

它清除 $vResult 并以 $inputMinute60 作为参数调用 validateNumber

如果我调用 typeset 并将数字作为第一个参数,我会得到同样的错误,但我在您的代码中没有看到这样的用法。您确定粘贴的是导致问题的版本吗?

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

关于linux - 使用输入参数执行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com