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

标签 linux bash shell

我有以下销售代码

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

相关文章:

linux - 如何从文件中提取最后一个 block

mysql - 如何在bash脚本中执行带有参数的SQL命令

linux - 为什么从服务器不能在 PVM(并行虚拟机)中工作

windows - 阻止在匿名管道上读写

linux - 如何以编程方式即时管理 iptables 规则?

linux - 不要杀死由 ps 创建的进程 - linux

linux - Bash 脚本 : always show menu after loop execution

Linux - 检查字符串是否在列表中

linux - 根据第一列拆分第二列

linux - opencv编译出错