linux - [ : too many arguments when taking * as an input

标签 linux bash loops while-loop calc

所以我必须在 bash 中用一些简单的函数制作这个小计算器,您可以在下面的代码(调试版本)中看到这些函数。但是在执行过程中,我遇到了一个问题,即在 operation2 中输入 * 会给我 [: 参数太多,但是在 operation1 中不会出现这种情况。

我需要计算器将此输入用于 1+1*2 等输入,因为脚本需要继续运行直到用户输入“=”,因此暂时!环形。我是批处理脚本的新手,所以我不知道我必须改变什么。我从调试中知道它正在调用这个脚本所在的字典中的文件列表,所以我相信它一定是误解了命令。

代码如下,在任何一个 operation2 上读取 * 时都会发生问题。但是它适用于所有其他输入(+、-、/、=)

#bin/bash +x

begin=$(date +"%s")

echo "Please Enter Your Unix Username:"
read text

userdata=$(grep $text /etc/passwd | cut -d ":" -f1,5,6 | tr -d ',')
echo "The User: $userdata" >> calcusers.txt
date=$(date)
echo "Last Ran calculator.sh on: $date " >> calcusers.txt
echo "---------------------------------------------------------" >> calcusers.txt

name=$(grep $text /etc/passwd | cut -d ":" -f5 | cut -d\  -f1)
echo -n "Hello" $name &&  echo  ", Welcome to calculator.sh."
echo "To begin using calculator.sh to do some calculations simply type a number"
echo "below, followed by the type of sum you want to perform and the input for"
echo "a second number, third and so on. To output the equation simply type =."
set -x
echo "Please Enter a Number:"
read number1
echo "Would you like to Add(+), Subtract(-), Multiply(*) or Divide(/) that number?"
read operation1
echo "Please Enter a Number:"
read number2
total=$(echo "$number1$operation1$number2" | bc)
echo "Would you like to Add(+), Subtract(-), Multiply(*), Divide(/) or Equals(=) that equation?"
read operation2

while [ ! $operation2 = "=" ]
do
     echo "Please Enter a Number:"
     read number3
     total=$(echo "$total$operation2$number3" | bc)
     echo "Would you like to Add(+), Subtract(-), Multiply(*), Divide(/) or Equals(=) that equation?"
     read operation2
done
set +x
echo -n "The total of the equation is" $total && echo "."
termin=$(date +"%s")
difftimelps=$(($termin-$begin))
echo "Thanks for using calculator.sh!"
echo "$(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds has passed since the Script was Executed."

exit 0

最佳答案

到处引用你的变量:

while [ ! "$operation2" = "=" ]
# ........^............^

此外,您应该预料到来自用户的无效输入

while [ ! "$operation2" = "=" ]
do
    case "$operation2" in
        [*/+-])
            echo "Please Enter a Number:"
            read number3
            total=$(echo "$total$operation2$number3" | bc)
            ;;
        *)  echo "Invalid operation: '$operation2'"
            ;;
    esac
    echo "Would you like to Add(+), Subtract(-), Multiply(*), Divide(/) or Equals(=) that equation?"
    read operation2
done

关于linux - [ : too many arguments when taking * as an input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976387/

相关文章:

linux - 在 shell 脚本中复制时检查磁盘空间

linux - 如何在 Shell 脚本中生成每个主机的 UUID?

linux - 如何在同一目录中的另一个脚本中使用 bash 脚本中的函数和变量?

php同时mysql获取数组,一次更新每10个结果,

PHP循环curl请求一个一个

linux - 使用变量时 Sed 替换不起作用

linux - (Linux) 如何在没有显示环境的情况下运行gtk 程序? Gtk 警告 ** : cannot open display:

javascript - jQuery:对可变数量的元素进行分组

c++ - 无法在运行时找到共享对象。 KRPC

php - Nginx 仅在 php 脚本和子域上抛出 404 错误