我有以下卖家代码
#!/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
有任何帮助吗?我是否需要在if elif中提及最终声明 声明?
答案 0 :(得分:1)
问题在于你的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="Chris"
disp_name="Morris"
也
echo "you had put salutation as" "$disp_sal"
echo "you entered name as " "$disp_name"
答案 1 :(得分:0)
试试这个;
#!/bin/sh
echo "hello"
echo "enter the salutation $abc"
read -r abc
if [ "$abc" = "1" ]
then
x="Hiiii"
elif [ "$abc" = "2" ]
then
x="haaaaa"
fi
echo "enter name $xyz"
read -r xyz
if [ "$xyz" = "1" ]
then
y="Chris"
elif [ "$xyz" = "2" ]
then
y="Morris"
fi
echo "you had put salutation as" "$x"
echo "you entered name as " "$y"
答案 2 :(得分:0)
我会用:
#!/bin/bash
PS3="Enter the salutation>"
select abc in Hiii Haaa; do
[[ "$abc" ]] && break
done
PS3="Enter name>"
select xyz in Chris Morris; do
[[ "$xyz" ]] && break
done
echo "you had put salutation as" "$abc"
echo "you entered name as " "$xyz"