我正在尝试将两个单词(或更多)读入变量$ans
。如果我使用单个单词作为答案,它可以正常工作,但否则会失败:
echo "hello what is your name"
read ans
if [ $ans = "zarkon kootz" ]; then
echo "hello zarkon kootz"
elif [ $ans = "zorlac kootz" ]; then
echo "hello zorlac kootz"
else
echo "Hey $ans get off my computer"
fi
有关如何将多个单词读入变量$ans
?
答案 0 :(得分:4)
你太近了 你唯一要做的就是将变量括在(双)引号
中这是你写它的方式,我只是添加引号并且它有效:
echo "hello what is your name"
read ans
if [ "$ans" = "zarkon kootz" ]; then
echo "hello zarkon kootz"
elif [ "$ans" = "zorlac kootz" ]; then
echo "hello zorlac kootz"
else
echo "Hey $ans get off my computer"
fi
答案 1 :(得分:0)
如果输入中有多个单词,则可以将每个单词分配给另一个变量。
即。做
read lastName firstName
而不是
read ans