我有以下代码
#!/bin/sh
echo "hello"
echo "enter salutation $abc"
Read -r abc
If [ "$abc" = "1" ]
Then
Echo "hiii"
Elif [ "$abc" = "2"]
Then
Echo "no hi"
Fi
Echo "enter name $xyz"
Read -r xyz
If [ "$xyz" = "1" ]
Then
Echo "Chris"
Elif ["$xyz" = "2" ]
Then
Echo "Morris"
Fi
Echo "final data"
Echo " you entered salutation as " "$abc"
Echo "you entered name as "$xyz"
Out put as as
Hello
Enter the number
1
Hiii
Enter name
1
Chris
You entered salutation as 1
You entered name as 1
我想要的是
You entered salutation as hii
You entered name as chris
答案 0 :(得分:1)
您似乎使用编号输入来从预先安排的选项中进行选择;在这种情况下,当用户选择一个数字时,您可以使用该数字根据您想要的值分配变量。
#!/bin/sh
echo "hello"
echo "enter salutation: 1 for hiii and 2 for no hi"
read -r salutation
case $salutation in
1) salutation="hiii";;
2) salutation="no hi";;
*) echo "Only 1 or 2 can be specified"; exit 1;;
esac
echo "enter name: 1 for Chris, and 2 for Morris"
read -r name
case $name in
1) name="Chris";;
2) name="Morris";;
*) echo "Only options 1, or 2 are supported"; exit 2;;
esac
echo "final data"
echo "you entered salutation as $salutation"
echo "you entered name as $name"
在条件中,您应该将值分配给变量,而不是仅将其打印出来。然后,您可以稍后使用该变量。
答案 1 :(得分:0)
您的代码无效,因为您打印的变量$ abc和$ xyz包含1或2,而不是字符串。如果您有更多疑问,请告诉我。
#!/bin/sh
echo "Hello, enter a number for salutation:"
read -r abc
if [ $abc = 1 ]
then
Salutation="hiii"
elif [ $abc = 2 ]
then
Salutation="no hi"
fi
echo "enter number 1 or 2 for name:"
read -r xyz
if [ "$xyz" = "1" ]
then
Name="Chris"
elif ["$xyz" = "2" ]
then
Name="Morris"
fi
echo "final data"
echo "you entered salutation as $Salutation"
echo "you entered name as $Name"