我有以下销售代码
#!/bin/sh
echo "hello"
echo "enter the salutation $abc"
read -r abc
if [ "$abc" = "1" ]
then
echo "Hiiii"
elif [ "$abc" = "2" ]
then
echo "haaaaa"
fi
echo "enter name $xyz"
read -r xyz
if
if [ "$xyz" = "1" ]
then
echo "Chris"
elif [ "$xyz" = "2" ]
then
echo "Morris"
fi
echo "you had put salutation as" "$abc"
echo "you entered name as " "$xyz"
我需要最终打印出来
you had put salutation as Hii
you entered name as chris
我得到的是
you had put salutation as 1
you entered name as 1
Any help? Do I need to mention the final statement inside the if elif statement?
最佳答案
问题出在你的 echo 语句上:
echo "Hiiii"
echo "haaaaa"
echo "Chris"
echo "Morris"
您只是打印字符串,而不是将其存储在您可以显示为预期输出的变量中:
echo "you had put salutation as" "$abc"
echo "you entered name as " "$xyz"
当您输入时,存储在 abc
和 xyz
中的值将是 1
和 1
。使用变量来存储值并在需要时显示它们。比如,将 echo 替换为以下内容:
disp_sal="Hiiii"
disp_sal="haaaaa"
disp_name="克里斯"
disp_name="莫里斯"
还有,
echo "you had put salutation as" "$disp_sal"
echo "you entered name as " "$disp_name"
关于linux - 在 shell 脚本中更正 if ..elif 中的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43535380/