LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2017, 11:24 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Question Introduce working "break" to terminate script (bash)


The script is as follows:
Code:
#!/bin/bash
#The script below came from:
#http://stackoverflow.com/questions/14350556/creating-a-calculator-script

PS3="what's the operation? "
declare -A op=([add]='+' [subtract]='-' [multiply]='*' [divide]='/')

while true; do
    read -p "what's the first number? " n1
    read -p "what's the second number? " n2
    select ans in "${!op[@]}"; do
        for key in "${!op[@]}"; do
            [[ $REPLY == $key ]] && ans=$REPLY
            [[ $ans == $key ]] && break 2
        done
        echo "invalid response"
    done
    formula="$n1 ${op[$ans]} $n2"
    printf "%s = %s\n\n" "$formula" "$(bc -l <<< "$formula")"
done
In the past, I have been able to introduce a conditional (testing for x as input) that exits the script and returns to prompt. Since downloading the above from the page mentioned in line 3 about four weeks ago, that didn't work so I removed it.

I'd like to write an "if/then" back in that will work.

Carver
Attached Files
File Type: txt bashcalc.txt (668 Bytes, 14 views)

Last edited by L_Carver; 07-02-2017 at 11:28 AM. Reason: Fixed bad phrasing.
 
Old 07-02-2017, 12:23 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,984

Rep: Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678Reputation: 5678
Looks like you forgot the $
Code:
if [ $n1 == "x" ]; then exit 0; fi
You can always add set -xv to aid in debugging...

Last edited by michaelk; 07-02-2017 at 12:45 PM.
 
Old 07-02-2017, 01:22 PM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
You might add an initial echo $0 to verify that it's running under bash. Things like debian have a default shell of dash these days. So if you run the script by $(sh script.sh) then that's dash, not bash. But if you run it by $(./script.sh) it will observe the shebang statement #!/bin/bash and run it under bash.

edit: or echo $SHELL as $0 will likely return the script name. YMMV

Last edited by Shadow_7; 07-02-2017 at 01:23 PM.
 
  


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
Automate "touch" bash script not working. L_Carver Linux - Newbie 16 05-31-2017 03:05 PM
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 11:04 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
command " cd .. " not working in bash script? Kilam orez Linux - Newbie 7 03-12-2010 05:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:20 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
Open Source Consulting | Domain Registration