Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash shell脚本*操作提供的参数太多_Bash_Shell - Fatal编程技术网

Bash shell脚本*操作提供的参数太多

Bash shell脚本*操作提供的参数太多,bash,shell,Bash,Shell,我正在做一个简单的计算器,可以选择是否继续。但当我尝试乘法操作时,控制台中出现错误: calculator.sh: line 17: [: too many arguments calculator.sh: line 22: [: too many arguments calculator.sh: line 27: [: too many arguments calculator.sh: line 32: [: too many arguments 这基本上意味着我所有的操作都有这个错误,但当

我正在做一个简单的计算器,可以选择是否继续。但当我尝试乘法操作时,控制台中出现错误:

calculator.sh: line 17: [: too many arguments
calculator.sh: line 22: [: too many arguments
calculator.sh: line 27: [: too many arguments
calculator.sh: line 32: [: too many arguments
这基本上意味着我所有的操作都有这个错误,但当我使用它们时,它们的行为是正常的。 我在stack overflow中搜索相似之处,但示例不同。我用斜杠对*进行了转义,但我认为它在与字符*进行比较时会显示错误,以便到达if语句的主体。 代码如下:

#!/bin/bash
choice="Y"
while [ $choice == "Y" ]
do
echo -n "Enter first value:"
read firstvar
echo -n "Enter second value:"
read secondvar
echo -n "Enter last value:"
read compvar
echo -n "Enter operation:"
read ops
counter=0
result=0
while [ $result != $compvar ]
do
if [ $ops == "+" ]
then result=$((firstvar+secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "-" ]
then result=$((firstvar-secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "*" ]
then result=$((firstvar\*secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
elif [ $ops == "/" ]
then result=$((firstvar/secondvar))
echo "Do you want to continue ? Y/N"
read choice
break
else
echo "Input valid operation !!!"
echo "Do you want to continue ? Y/N"
read choice
break
fi
counter=$((counter+1))
done
done

问题可能不是因为脚本中的*,而是因为$ops变量中的星号

您应该双引号的变量,以避免全局应用于他们;像这样重写测试:

elif [ "$ops" = "*" ]
这里有一个非常有用的例子

首先,请参阅Charles Duffy的注释WRT==vs=以了解字符串测试。 将$ops的出现次数更改为${ops}。 在result=$firstvar*secondvar中删除转义。 我冒昧地重新格式化了一下脚本。 希望这有帮助


通过shellcheck.net.BTW运行您的代码,一行代码就可以解决这个问题。ops='*';如果[$ops==*];然后echo把这理解为一个乘法;fi是让其他人重现您的问题所需要的一切,并且比代码试图从终端读取时更可靠,因此取决于用户行为。您不需要转义*内引号。对。从原始问题代码复制过来。==不保证在[]内受支持。Use=相反,请跳过[$ops=…测试,并在…${ops}中执行case$ops在OPS上没有任何可靠性/健壮性的好处-如果你打算重构,我会考虑切换到case语句。还应该在需要保证的情况下添加额外的引号,看看结果,比如指出,CopVar……实际上,SHILCHECK建议的引用比实际世界中的一个好主意要少。因为假设choice=Y的全部意义是作为阅读选择的替代品——但一旦你这样做了,$choice也需要被引用。
#!/bin/bash
choice="Y"
while [ $choice = "Y" ]
do
  echo -n "Enter first value:"
  read firstvar
  echo -n "Enter second value:"
  read secondvar
  echo -n "Enter last value:"
  read compvar
  echo -n "Enter operation:"
  read ops
  counter=0
  result=0
  while [ $result != $compvar ]
  do
    if [ "${ops}" = "+" ]; then
      result=$((firstvar+secondvar))
      echo "Do you want to continue ? Y/N"
      read choice
      break
    elif [ "${ops}" = "-" ]; then
      result=$((firstvar-secondvar))
      echo "Do you want to continue ? Y/N"
      read choice
      break
    elif [ "${ops}" = "*" ]; then
      result=$((firstvar*secondvar))
      echo "Do you want to continue ? Y/N"
      read choice
      break
    elif [ "${ops}" = "/" ]; then
      result=$((firstvar/secondvar))
      echo "Do you want to continue ? Y/N"
      read choice
      break
    else
      echo "Input valid operation !!!"
      echo "Do you want to continue ? Y/N"
      read choice
      break
    fi
    counter=$((counter+1))
  done
done