1

Hi I am trying to create a bash program to call a program and input menu picks into it.

For example my program runs

Hello world:
1) Item 1
2) Item 2
Enter : 2

Item 2 Menu
1) sub Item A
2) sub item B
enter: 1

I tried doing $ 2|1|./program and $./program|2|1

But it either fails or it just loops forever and doesn't get to the second menu.

Is it possible to do this?

1
  • 3
    Possibly you could do it with expect Commented Dec 13, 2011 at 17:04

1 Answer 1

3

Presuming a program that works essentially like this one:

pmenu () {
    printf '%s) %s\n' "$1" "$2"
}

m1 () {
    pmenu 1 'Item 1'
    pmenu 2 'Item 2'
}
m2 () {
    pmenu 1 'sub Item A'
    pmenu 2 'sub Item B'
}

printf 'Hello, World:\n'

m1
read -p "enter: " m

case "$m" in
    1 ) m2 ;;
    2 ) m2 ;;
esac

read -p "enter: " m

echo "You chose: $m"

Then you can just say

printf '2\n1\n' | program

For anything less straightforward you may need to learn expect, which is designed for this sort of thing.

Sign up to request clarification or add additional context in comments.

1 Comment

Or, if one is not able/bothered to learn expect, there's autoexpect which can record a session and generate an expect script that can be used to replay the session.

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.