2

I am trying to write a script that will contain multiple commands which the user will be prompted to run up front, and run a dynamic set of those commands based on user input

So, lets say for example I setup functions for the commands that I need to run

    command1 () { some_command; }
    command2 () { some_command; }
    command3 () { some_command; }
    command4 () { some_command; }

Followed by a series of prompts

Do you want to run command1?
Do you want to run command2?
Do you want to run command3?
Do you want to run command4?

For this example, assume Y, N, Y, Y so I need to run command1, command3, command4 I hope that gets the point across.

Any assistance would be greatly appreciated.

4 Answers 4

2
read -p "Do you want to run command1? " c1  
read -p "Do you want to run command2? " c2  
read -p "Do you want to run command3? " c3  
read -p "Do you want to run command4? " c4

if [ "$c1" = "Y" ]; then  
    command1  
fi  

if [ "$c2" = "Y" ]; then  
    command2  
fi

if [ "$c3" = "Y" ]; then  
    command3  
fi

if [ "$c4" = "Y" ]; then  
    command4  
fi
Sign up to request clarification or add additional context in comments.

Comments

1

You might (or might not) want to consider the select built-in:

select

The select construct allows the easy generation of menus. It has almost the same syntax as the for command:

select name [in words ...]; do commands; done

The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error output stream, each preceded by a number. If the ‘in words’ is omitted, the positional parameters are printed, as if ‘in "$@"’ had been specified. The PS3 prompt is then displayed and a line is read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the select command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY.

The commands are executed after each selection until a break command is executed, at which point the select command completes.

Comments

0

The read command is just what you need http://www.vias.org/linux-knowhow/bbg_sect_08_02_01.html

short example

Applying user input to the variable 'foo'

# Just showing a nice message along with it.
echo -n "Would you like to run command1? (Y/N) "
read foo

Then you could just test the value of the foo variable

if [ "$foo" == "Y" ]; then
  command1
fi

Comments

0

If you took user input into a series of variables (using the read command as detailed in the other answer), one for each command (call them, for example, C1, C2, C3) then after you've taken user input you can write a series of if statements that looks at the value of those variables

if [ $C1 == "Y" ]; then
    command1
fi

if [ $C2 == "Y" ]; then
    command2
fi

if [ $Cn == "Y" ]; then
    commandN
fi

Does that help at all?

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.