1

I have the following sell code

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

I need the final printing to be like

 you had put salutation as Hii
 you entered name as chris

what I get is

 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?

3
  • What did you enter when you are prompted for salutation and name respectively? Commented Apr 21, 2017 at 6:28
  • @anthony I entered 1 for both Commented Apr 21, 2017 at 6:33
  • 1
    Your code in question is not complete. It throws syntax error: test.sh: line 24: syntax error: unexpected end of file. Won't produce the output you said you saw. Commented Apr 21, 2017 at 6:42

3 Answers 3

1

The issues are with your echo statements:

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

You are just printing the string but not storing it in variables which you can display as your expected output:

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

The values stored in abc and xyz as you entered will be 1 and 1. Use variables to store values and display them when required. Like, replace echo's with following:

disp_sal="Hiiii" disp_sal="haaaaa"

disp_name="Chris" disp_name="Morris"

also,

echo "you had put salutation as" "$disp_sal"
echo "you entered name as " "$disp_name"
Sign up to request clarification or add additional context in comments.

Comments

0

I would use:

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

Comments

0

Try this;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.