Bash Error: Unary Operator Expected
Bash Error: Unary Operator Expected
我正在尝试编写一个简单的 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.
如有任何建议或提示,我们将不胜感激。
试试这个:read return_value
没有 $
符号。
选择选项后不会调用您的命令,因为 $whoami
被视为变量。你可能想要 $(whoami)
。其他命令也一样。
这里有多个问题。
首先,您显然是想 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
的值将为空,这将再次破坏条件。所以你应该在 if
和 elif
语句中双引号。
像这样:
#!/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
我认为这还不清楚,可以使用更多规范来明确您要键入的内容。如果您希望用户键入“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
我正在尝试编写一个简单的 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.
如有任何建议或提示,我们将不胜感激。
试试这个:read return_value
没有 $
符号。
选择选项后不会调用您的命令,因为 $whoami
被视为变量。你可能想要 $(whoami)
。其他命令也一样。
这里有多个问题。
首先,您显然是想 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
的值将为空,这将再次破坏条件。所以你应该在 if
和 elif
语句中双引号。
像这样:
#!/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
我认为这还不清楚,可以使用更多规范来明确您要键入的内容。如果您希望用户键入“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