Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a problem in my shell script. I try to control an installation. During the installation there are a few questions which have to be answered. Like: "Enter your Choice [1-3]: "

I want do something like this:

--> It's my first script <--
#!/bin/bash


echo "--> Install packages <--"
sudo apt-get install git

cd $HOME/

#mkdir mspgcc_download
#cd mspgcc_download

echo "--> Download mspgcc <--" 

git clone git://mspgcc4.git.sourceforge.net/gitroot/mspgcc4/mspgcc4
cd mspgcc4

echo "--> Build mspgcc <--" 

echo "y" | echo "n" | echo "" | echo "n" | echo "2" | echo "1" | echo "1" | echo "1" | ./buildgcc.sh
share|improve this question
1  
there's an edit function for your question, please learn to use it instead of adding 'answers' to add more information –  Ivo Flipse Jan 19 '11 at 16:19
add comment

4 Answers

up vote 1 down vote accepted

You can often do that with a here doc:

cat <<EOF | ./buildgcc.sh
y
n

n
2
1
1
1
EOF

or

./buildgcc.sh <<EOF
y
n

n
2
1
1
1
EOF
share|improve this answer
add comment

echo will print to the standard output, that is, what you see in the terminal. You have to pipe the data into the standard input of your application, usually with |.

echo 1 | your-prog-here

Perhaps you should be using expect, but it is new programming language you will have to learn.

share|improve this answer
add comment
$ read -p 'Gimme a B: ' b
bee
$ echo $b
bee
share|improve this answer
add comment

If i've well understood your query, I often use this kind of code :

_answer=''
while [ -z ${_answer} ]
do
    # I check if Bash or Ksh is used
    if [ "x${BASH_VERSION}" != "x" -a "x${KSH_VERSION}" = "x" ]; then
        # Bash
        read -n 1 -p "Your choice [1-3] : " _answer
    else
        # KSH
        read  _answer?"Your choice [1-3] : "
    fi
    clear
    case "${_answer}" in
        1) function1 ;;
        2) function2 ;;
        3) function3 ;;
        *) _answer='' ;;
    esac
done

The difference in the read syntax, let's you only type the character on Bash, but you must type an enter at the end with KSH. For bash, it's the -n 1 that say to read to only wait for 1 character.

Don't forget to declare your function before.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.