2017-04-21 321 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"