LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2013, 12:14 PM   #1
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Rep: Reputation: Disabled
Multiple Options in BASH


Simple

Scripts starts off with prompting the user to answer Yes or no. When they answer Yes or No, a command is run within the script and then I need to prompt them with another series of questions.

If you worked in BASIC back in the day, there was the GOTO statement and we could feed it to another line. But not in BASH. I understand. But what would be the equal?

I have a multiple menu set-up already. But when I add more interactive prompts, it fails.
 
Old 03-02-2013, 12:25 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Nope, no goto's here. You generally use functions to set up code that can be called on arbitrarily, and loops to iterate commands multiple times.


I recommend the Bash Guide for a good general rundown on all the basic shell scripting concepts.


And here are a few more useful scripting references:
http://wiki.bash-hackers.org/start
http://www.linuxcommand.org/index.php
http://wiki.bash-hackers.org/scripting/newbie_traps
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://ss64.com/bash/



PS: If you post the code you have, and explain how you want it to behave, we can certainly help guide you through the proper way to do it.

Last edited by David the H.; 03-02-2013 at 12:29 PM.
 
Old 03-02-2013, 01:22 PM   #3
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes great links many thanks.

But to run a series of loops example:

You have 4 options to choose from:
1 - Walk
2 - Run
3 - Climb
4 - Swim

Then the user answers with say "swim". Then more questions.

You have 4 options to choose from:
1 - Do you want to head back to land?
2 - Do you want to board the boat in front of you?
3 - Do you want to go to the bottom of the ocean?
4 - Do you want to fight the shark?

And the routine runs again. This was the concept of an old adventure game done in BASIC. I understand that the BASIC statements are not the same. I truly respect that. What would be similar?

This is what I have:

Code:
#!/bin/bash
clear
PS3='Please enter your choice: '
options=("Run 1" "Walk 2" "Jog 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
	    echo "you chose choice 1"
             ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac

done
But when the user chooses option one, how do I make it prompt for more? Just add in the options again? I tried I get errors.
 
Old 03-02-2013, 09:34 PM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by bigbankclub View Post
Simple

Scripts starts off with prompting the user to answer Yes or no. When they answer Yes or No, a command is run within the script and then I need to prompt them with another series of questions.

If you worked in BASIC back in the day, there was the GOTO statement and we could feed it to another line. But not in BASH. I understand. But what would be the equal?

I have a multiple menu set-up already. But when I add more interactive prompts, it fails.
I will probably be tarred and feathered for suggesting this but a quick workaround would be to set the first line of your script as:
Code:
#!/usr/bin/blassic
then write the rest of your script in traditional BASIC. The "SHELL" instruction in blassic is used to execute simpler bash scripts where you don't have to spend hours figuring out how to make a conditional statement work properly.

Of course you have to have blassic installed on your computer. Blassic was designed to run older style programs written in BASIC (including line numbers) but it also has politically correct conditional structures if you wish to use them.
 
Old 03-02-2013, 09:44 PM   #5
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Oh I just rolled off into the snowbank when I read that. Then I looked it up. Wow so Vic-20! But in reality, I don't want to use Basic, but that does open some more ideas on what other interpreters are available. Tcl would be kool to try out again.

Will work on loops tomorrow. But the Basic idea was good. Saw some videos on YouTube just now. So retro brought me back to the 80's!
 
Old 03-02-2013, 09:57 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I wasn't sure what your priority was. If it is to learn script programming then you will have to do it the hard way. If you just wanted to whip up something quickly then use what you know best.

BTW I think that the value of basic as a prototyping tool is underestimated. If you just want to test a piece of code fragment without all the hassles of compiling/linking then I often find this the quickest way to start.

Last edited by psionl0; 03-02-2013 at 10:02 PM.
 
Old 03-02-2013, 10:00 PM   #7
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes I got the blassic-0.10.2.tgz file - But challenged with the install. Will make it work - But I will tackle loops next.
 
Old 03-02-2013, 10:12 PM   #8
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Got it working. That was easy. But not what I wanted. Works in Linux and Windows - but so old school!
 
Old 03-02-2013, 10:33 PM   #9
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by bigbankclub View Post
Got it working. That was easy. But not what I wanted. Works in Linux and Windows - but so old school!
Yep! It has its place but for anything more serious you need a more appropriate platform (and the time taken to make what you want work in that platform ).
 
Old 03-02-2013, 11:37 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your use of the select loop is already on the right track, since it forms a continuous loop to keep prompting you. All you need to do is set up an appropriate function for each of the chosen options. Functions act like sub-scripts inside the main script, which can be called on whenever needed, like any other command.

Code:
#!/bin/bash
clear

opt1(){
	echo "this is option one, with arguments: ${@:-none}"
	:<other code for option one>
}

opt2(){
	echo "this is option two, with arguments: ${@:-none}"
	:<other code for option two>
}

op3(){
	echo "this is option three, with arguments: ${@:-none}"
	:<other code for option three>
}

PS3='Please enter your choice: '
options=("Run 1" "Walk 2" "Jog 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Run 1")
	    echo "you chose choice 1"
	    opt1 arg1 arg2
             ;;
        "Walk 2")
            echo "you chose choice 2"
	    opt2 arg3
            ;;
        "Jog 3")
            echo "you chose choice 3"
	    opt3
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac

done
Whenever a function terminates you'll still be inside the select loop and ready for another run. Of course for minor actions you don't even need a function, just include them directly in the case statement.

Functions can take $1..$n input arguments, just like any other script. Just don't confuse them with the main script's parameters; these are local to the function. I also recommend keeping your user variables locally scoped whenever possible (with the declare or local keywords).

The only other main requirement is that functions need to be set up before they are used, so you'll usually find their declarations at the top of the script.

BTW, from the code posted so far, you seem to already have a pretty good handle on basic scripting practice. For once I see no major issues to criticize.

Edit: Actually I just noticed that there's an error in your select loop. $opt holds the text string of the option selected, so you'd need to test for "Run 1", "Walk 2", in the case statement. Or you can also use $REPLY instead, which contains the number of current selection. I've modified the code above to reflect this.

Edit2: I just realized that this post may not have clearly answered the posted question. Creating a sub-menu is as simple as putting another menu inside the appropriate function, which can of course call other functions, ad infinitum. Each function is essentially a self-contained module that can be accessed whenever you need it.

Last edited by David the H.; 03-03-2013 at 01:14 AM. Reason: as stated
 
Old 03-03-2013, 03:59 PM   #11
bigbankclub
LQ Newbie
 
Registered: Mar 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
excellent - I will play and grow with this. I will also add more loops.

Thank you GUYS!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 01:52 PM
Bash quesion - scp on multiple hosts to multiple directories in each one mierea.ciprian Programming 2 06-22-2012 12:27 PM
Bash development - move chmod options to bash jhwilliams Programming 9 06-26-2007 12:13 PM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 11:43 AM
Multiple booting options srivastava Linux - Software 2 05-19-2005 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration