Bash错误:预期一元运算符

时间:2017-11-04 18:24:46

标签: bash variables conditional

我正在尝试编写一个简单的Bash脚本,它将根据用户输入执行命令。我有一些Python的经验,但出于某种原因,这给了我一些问题。

===================

*#!/bin/bash
echo "Type one of the following:"
echo " 1 - whoami"
echo " 2 - df"
echo " 3 - date"
echo " 4 - cal"
echo "===> "
read $return_value
if [ $return_value -eq 1 ]
    then
        echo $whoami
elif [ $return_value -eq 2 ]
    then
        echo $df
elif [ $return_value -eq 3 ]
    then
        echo $date
elif [ $return_value -eq 4 ]
    then
        echo $cal
else [ $return_value != 5 ]
    echo "You made an invalid Selection. Exiting."
fi

========================================

我不断得到这些错误,我不知所措。

Type one of the following:
 1 - whoami
 2 - df
 3 - date
 4 - cal
===> 
2
./utilities1.sh: line 10: [: -eq: unary operator expected
./utilities1.sh: line 14: [: -eq: unary operator expected
./utilities1.sh: line 18: [: -eq: unary operator expected
./utilities1.sh: line 22: [: -eq: unary operator expected
./utilities1.sh: line 26: [: !=: unary operator expected
You made an invalid Selection. Exiting.

非常感谢任何建议或提示。

3 个答案:

答案 0 :(得分:0)

试试这个:two-phase commit没有read return_value符号。

选择选项后不会调用您的命令,因为$被视为变量。你可能想要$whoami。其他命令也一样。

答案 1 :(得分:0)

这里有很多问题。

首先,您显然需要read return_value而不是read $return_value,才能将用户输入读入名为return_value的变量中。 read $return_value会将用户输入读入名为$return_value的值的变量中。例如,如果return_value=x,那么它会将输入读入变量x。由于在运行脚本时未分配return_value,因此这不是您想要的。你当然希望read return_value

其次,您可能希望执行这些命令而不是echo $df和其他命令。

此外,如果用户只需按Enter键,$return_value的值将为空,这将再次打破条件。因此,您应该在ifelif语句中对其进行双重引用。

像这样:

#!/bin/bash

echo "Type one of the following:"
echo " 1 - whoami"
echo " 2 - df"
echo " 3 - date"
echo " 4 - cal"
echo "===> "
read return_value

if [ "$return_value" -eq 1 ]; then
    whoami
elif [ "$return_value" -eq 2 ]; then
    df
elif [ "$return_value" -eq 3 ]; then
    date
elif [ "$return_value" -eq 4 ]; then
    cal
else [ "$return_value" != 5 ]
    echo "You made an invalid Selection. Exiting."
fi

答案 2 :(得分:-1)

我认为这是非常不清楚的,可以使用更多关于你想要输入的内容的规范。如果您希望用户键入" 1",则说出来,而不是" 1 - whoami"。我也不清楚你的" *"在开始。这是一个有效的代码,其中有解释说明。希望这会有所帮助。

#!/bin/bash
echo "Type one of the following numbers to"
echo "execute the specified command:"
echo " 1 - whoami"
echo " 2 - df"
echo " 3 - date"
echo " 4 - cal";
echo -n "===> "    #the -n means dont create a newline.. so the input will be after the arrow
read return_value  #remove $
echo "user input = ${return_value}"   #using ${} instead of just $ is good practice unless you have reason
if [ "${return_value}" == "1" ]; then  #sometimes you need = instead, compatibility issues.  but -eq is for integers.
    echo `whoami`                       #surround a command with `command` to execute it
elif [ "${return_value}" == "2" ]; then
    echo `df`
elif [ "${return_value}" == "3" ]; then
    echo `date`
elif [ "${return_value}" == "4" ]; then #don't forget the ";" before "then"
    echo `cal`
else                                    #else doesn't need an argument like if and elif
    echo "You made an invalid Selection. Exiting."
fi

示例输出:

me$ ./script.sh 
Type one of the following numbers to
execute the specified command:
 1 - whoami
 2 - df
 3 - date
 4 - cal
===> 1
user input = 1
myusername