2017-04-21 111 views
1

我有以下卖出代码正确的输出

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

+0

那你当你进入提示f或'salutation'和'name'分别? –

+0

@anthony我为 –

+1

输入了1您的代码不完整。它会抛出语法错误:'test.sh:第24行:语法错误:文件意外结束。不会产生你说你看到的输出。 –

回答

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" 
+0

谢谢你soo –

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" 
+0

谢谢soo :)这个作品 –

1

的问题是与你的回声声明:

echo "Hiiii" 
echo "haaaaa" 
echo "Chris" 
echo "Morris" 

你只是打印字符串,但没有将其存储在其中您可以显示为您期望的输出变量:

echo "you had put salutation as" "$abc" 
echo "you entered name as " "$xyz" 

您输入的abcxyz中存储的值将为11。使用变量来存储值并在需要时显示它们。等,与下面的代码替换呼应的:

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"