3

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
  • 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
1

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
| improve this answer | |
3

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.

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

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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