LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-22-2014, 10:26 PM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 884

Rep: Reputation: 45
bash menu issues


I am trying to build a menu to select a cell phone carrier for sending text messages via email from the command line.

I have the menu built but am having an issue getting out of the menu after a choice is made. I am not a programmer. So copy paste from examples is how I got where I am.

The part of the code that is not working right is

Code:
while [ 1 ]
do
        showMenu
        read CHOICE
        case "${CHOICE}" in
                "1")
                        echo " You chose att"
                        CCARRIER="att"
                        break 0
                        ;;
# 15 entries
         esac
the error I get is
Code:
./carrierTest: line 60: break: 0: loop count out of range
I have tried replacing break with return, exit, and done. It will return clean with exit but that drops me out of the script. If I don't have the "0" after break the menu gets stuck in a loop. Same with return.

Google has not helped me.

Thanks
 
Old 09-23-2014, 12:47 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,585

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
Maybe have a look at 'select' command as building menus and dealing with the choices from them is its forte
 
Old 09-23-2014, 08:37 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,887

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
I think 'read CHOICE' is not valid. You need to post the full script or at least leave in the later parts which close the while loop.
 
Old 09-23-2014, 08:09 PM   #4
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 884

Original Poster
Rep: Reputation: 45
Thanks gnashley

I have opted to try what grail said above. What posted before came from a web site. I no longer have that page so I can't post a link, but what I posted is pretty much what there was.

grail thanks, I have looked at select and it seams to be more friendly. But as per normal I am having issues. What I have came from http://www.softpanorama.org/Scriptin...atements.shtml

As I stated before copy/paste is me. I did change the menu items from terminals to cell providers and extend it but that is all.


The menu script.
Code:
#!/bin/bash

print 'Select your Cell Service:'
PS3='Provider? '
select term in \
    'AT&T' \
    'Verizon' \
    'Sprint' \
    'Alltell' \
    'Cingular' \
    'Nextel' \
    'SunCom' \
    'T-Mobile' \
    'Voice Stream' \
    'US Cellullar' \
    'Cricket' \
    'Virgin' \
    'Boost' \
    'Boost Mobile' \
    'Einsteingcs' \
    'Einstien' \
    'Other'
    do
    case $REPLY in
        1 ) TERM=att ;;
        2 ) TERM=verizon ;;
        3 ) TERM=sprint ;;
        4 ) TERM=alltel ;;
        5 ) TERM=cingular ;;
        6 ) TERM=nextel ;;
        7 ) TERM=sunCom ;;
        8 ) TERM=tmobile ;;
        9 ) TERM=voicestream ;;
        10 ) TERM=uscellular ;;
        11 ) TERM=cricket ;;
        12 ) TERM=virgin ;;
        13 ) TERM='boostmobile|boost' ;;
        14 ) TERM='boostmobile|boost' ;;
        15 ) TERM='einsteinpcs|einstein' ;;
        16 ) TERM='einsteinpcs|einstein' ;;
        * ) print 'invalid.' ;;
    esac
    if [[ -n $term ]]; then
        print TERM is $TERM
        break
    fi
  CCARRIER=${TERM}
echo "${CCARRIER}"
  done
And the errors
Code:
:~/BDay/Dev$ ./carrierTest 
Error: no such file ":Select your Cell Service:"
1) AT&T            6) Nextel        11) Cricket       16) Einstien
2) Verizon         7) SunCom        12) Virgin        17) Other
3) Sprint          8) T-Mobile      13) Boost
4) Alltell         9) Voice Stream  14) Boost Mobile
5) Cingular       10) US Cellullar  15) Einsteingcs
Provider? 4
Error: no such file "TERM"
Error: no such file "is"
Error: no such file "alltel"
:~/BDay/Dev$
 
Old 09-23-2014, 08:42 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129
use "echo", not "print"

Also, bash is case-sensitive, so if you set a variable TERM, you need to reference it as TERM from then on. Your if statement checks "term".
 
Old 09-23-2014, 09:44 PM   #6
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 884

Original Poster
Rep: Reputation: 45
Thanks suicidaleggroll

The echo is only for debugging to know that the script is filling CCARRIER correctly. After I get the menu working right I will incorporate it into the larger script that generates a csv data file.

As for the term TERM thing, I know the case sensitive thing. My understanding from reading the web site
Quote:
When the body of the select loop is entered, $term equals one of the four strings (or is null if the user made an invalid choice), while the built-in variable REPLY contains the number the user selects. We need a case statement to assign the correct value to TERM; we use the value of REPLY as the case selector.
I don't know what this is supposed to do other than break out of the menu loop.
Code:
if [[ -n $term ]]; then
        print TERM is $TERM
        break
I only need to have the chosen value in the case-esac placed in $CCARRIER
 
Old 09-24-2014, 10:01 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,585

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
You do not need to over-complicate things. Try something like:
Code:
#!/usr/bin/env bash

carriers=( 'AT&T'
	 'Verizon'
	 'Sprint'
	 'Alltell'
	 'Cingular'
	 'Nextel'
	 'SunCom'
	 'T-Mobile'
	 'Voice Stream'
	 'US Cellullar'
	 'Cricket'
	 'Virgin'
	 'Boost'
	 'Boost Mobile'
	 'Einsteingcs'
	 'Einstien'
	 'Other'
	)

PS3="Provider? "
echo "Select your Cell Service:"

select item in "${carriers[@]}"
do
	if [[ -n "$item" ]]
	then
		carrier="$item"
		break
	fi

done

echo "$carrier"
 
Old 09-24-2014, 11:01 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129Reputation: 2129
Quote:
Originally Posted by rbees View Post
The echo is only for debugging to know that the script is filling CCARRIER correctly.
I don't think you understood me. There are two places where you're using "print" to print out some text. This is not how you print text in bash, and is the cause of pretty much all of your errors when you try to run it. Replace all of your calls to "print" with "echo". I don't even know what "print" is...I've never used it or even heard of it, but apparently your system has it installed and it's trying to treat the argument as a file name, which is clearly not your intention.


Quote:
Originally Posted by rbees View Post
As for the term TERM thing, I know the case sensitive thing.
If you know it's case sensitive, then why are you filling "TERM" and then checking "term" in an if-statement? You need to be consistent.

Also, TERM is an actual environment variable that contains the name of the terminal emulator you're using. You shouldn't use it as a random variable name in your script. For that matter, you shouldn't use all caps for variable names in scripts at all, since all caps is generally reserved for environment variables.

Last edited by suicidaleggroll; 09-24-2014 at 11:04 AM.
 
Old 09-24-2014, 04:54 PM   #9
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 884

Original Poster
Rep: Reputation: 45
Thank grail and suicidaleggroll

This is where I got this morning before I had to go to work.
Code:
#!/bin/bash

echo 'Select your Cell Service:'
PS3='Provider? '
select carrier in \
    'AT&T' \
    'Verizon' \
    'Sprint' \
    'Alltell' \
    'Cingular' \
    'Nextel' \
    'SunCom' \
    'T-Mobile' \
    'Voice Stream' \
    'US Cellullar' \
    'Cricket' \
    'Virgin' \
    'Boost' \
    'Boost Mobile' \
    'Einsteingcs' \
    'Einstien' \
    'Unknown'
    do
    case $REPLY in
        1 ) CCARRIER='att' ;;
        2 ) CCARRIER='verizon' ;;
        3 ) CCARRIER='sprint' ;;
        4 ) CCARRIER='alltel' ;;
        5 ) CCARRIER='cingular' ;;
        6 ) CCARRIER='nextel' ;;
        7 ) CCARRIER='sunCom' ;;
        8 ) CCARRIER='tmobile' ;;
        9 ) CCARRIER='voicestream' ;;
        10 ) CCARRIER='uscellular' ;;
        11 ) CCARRIER='cricket' ;;
        12 ) CCARRIER='virgin' ;;
        13 ) CCARRIER='boostmobile|boost' ;;
        14 ) CCARRIER='boostmobile|boost' ;;
        15 ) CCARRIER='einsteinpcs|einstein' ;;
        16 ) CCARRIER='einsteinpcs|einstein' ;;
        17 ) CCARRIER='unknown' ;;
        * ) print 'invalid.' ;;
    esac
    if [[ -n $carrier ]]; then
        echo "CCARRIER is $CCARRIER"
        break
    fi
  
done

echo "${CCARRIER}"
I made a few changes since reading your posts this afternoon.

grail, I like what you did. But I need the extra step for "eye-candy". Most people will recognize AT&T as a cell phone carrier but may not att which may look like a typo. But in a different part of the over all script(s) having a & or | or other special character will cause problems, so.....

I still have some other variable naming issues to get fixed before I can add it to the over all script. As suicidaleggroll pointed out there were name problems. I was not aware that TERM is a built-in variable. But it does make sense.

I do not know why the site I got the basics from had print in there for an echo. I just copied it to a new file and started changing things to get it to do what I wanted. I tried adding ' and " to the print statements and in that case it does actually print to the screen but I didn't get it to work until I changed them to echo's.

I don't know how or why it works as it is, I only know that it does. I suppose I should figure it out but I always have a time to do it issue. It works move onto something else.

Thanks
 
Old 09-24-2014, 08:56 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,585

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
As long as you quote variables, which is a good practice, you should not have issues with special characters.

Also, there is no reason to have both a 'case' and an 'if', ie. once REPLY is set, carrier will also be set.

Lastly, you still have a 'print' statement in the case which will error.
 
Old 09-26-2014, 02:10 PM   #11
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 681

Rep: Reputation: 105Reputation: 105
To understand their workings, it helps to make a very simple script, so you know what your variables actually contain.

Code:
select dinges in \
        "hello" \
        "goodbye" 
do
        echo "${REPLY} is ${dinges}"
        if [[ "${dinges}" == "goodbye" ]]
        then
                printf "\nYou said '%s', so... farewall cruel master!\n" "${dinges}"
                exit
        fi
done
then you see that "${REPLY}" contains the choice number, whereas "${dinges}" contains the string value of said choice;
It's (I think) good practice to create a means to leave the loop.

From the code posted earlier, both ${TERM} and ${term} were set, but for the human eye this is confusing. Keep your variables either uppercase or lowercase or camelcase, but try to avoid having the same name for a variable with only their case being different. You will only confuse yourself. Try to avoid (unless necessary ofc) special variables (TERM is used as a 'tty descriptor' (do I word that correct?))

If you need different case-settings because you already used that variable, then obviously you've used the wrong variable name. If they're different in purpose, their name should be different.

Last edited by Ramurd; 09-26-2014 at 02:14 PM.
 
Old 09-28-2014, 09:07 AM   #12
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 884

Original Poster
Rep: Reputation: 45
Thanks to all who helped.

Marking solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash script menu with sub menu sbrock Programming 3 01-19-2014 05:01 AM
Making menu's within menu's (function) in bash genderbender Programming 1 03-19-2008 11:12 AM
Having issues with my Fluxbox menu??? Southpaw76 Slackware 10 11-23-2006 12:07 PM
fluxbox menu issues.... detpenguin Linux - Software 2 03-17-2004 11:11 PM
Mandrake 9.1 - Menu issues pete_bogg Linux - Distributions 5 08-18-2003 11:54 PM

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

All times are GMT -5. The time now is 01:38 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration