gpt4 book ai didi

linux - 在 shell 脚本中更正 if ..elif 中的输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:20 25 4
gpt4 key购买 nike

我有以下销售代码

#!/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"

当您输入时,存储在 abcxyz 中的值将是 11。使用变量来存储值并在需要时显示它们。比如,将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com