LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-24-2009, 08:23 AM   #1
FLX
Member
 
Registered: Apr 2005
Distribution: Fedora Core 5, SuSE 10.1
Posts: 40

Rep: Reputation: 15
[bash] re-use choices made


I created the following script:
Code:
  echo -e "\033[1mChoose something?\033[0m"
  while true
  do
  read updatechoicenv
  case "$updatechoicenv" in
    "1")
        echo choice 1!
        break
    ;;
    "2")
        echo choice 2
        break
    ;;
    "3")
        echo choice 3!
        break
    ;;
    * )
    echo "No valid option received. Try again."
    ;;
  esac
  done
  echo gonna do some other stuff, kthxbai!
  echo i shall now return to updatechoicenv
  echo
  read updatechoicenv
  case "$updatechoicenv" in
    "1")
        echo option 1 has been chosen earlier, doing some stuff based on this!
        break
    ;;
    "2")
        echo option 2 has been chosen earlier, doing some stuff based on this!
        break
    ;;
    "3")
        echo option 3 has been chosen earlier, doing some stuff based on this!
        break
    ;;
  esac
I would like to use the choice that has been made earlier to do some stuff further down the script, how can I do this?

Thanks,

Dennis
 
Old 09-24-2009, 08:49 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by FLX View Post
I would like to use the choice that has been made earlier to do some stuff further down the script, how can I do this?
Just keep it in a different variable name:
Code:
  echo -e "\033[1mChoose something?\033[0m"
  while true
  do
  read updatechoicenv1
  case "$updatechoicenv1" in
    "1")
        echo choice 1!
        break
    ;;
    <more cases>
    * )
    echo "No valid option received. Try again."
    ;;
  esac
  done
  echo gonna do some other stuff, kthxbai!
  echo i shall now return to the source
  echo
  read updatechoicenv2
  case "$updatechoicenv2" in
    "1")
        if [[ "$updatechoicenv2" -eq "$updatechoicenv1" ]]; then
            echo option 1 has been chosen earlier, doing some stuff based on this!
        else
            <whatever>
        fi
        break
        ;;
    <more cases>
  esac
But those breaks are going to break the script out of the while loop before it gets to the second read except when the first read returns an invalid choice! The ;; breaks out of the case statement.
 
  


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
How to get tab auto complete in bash to scroll through all possible choices joeldick Linux - Software 5 04-06-2011 04:27 PM
Hi never made a User application with (bash) carl0ski Programming 3 01-30-2006 05:30 AM
RHEL 4 Installer Choices: I want to see the packages associated with these choices. tacoshell Red Hat 2 05-05-2005 04:34 PM
Text Mode Editor: What Choices? BASH, VI, EMACS? LinxAI Linux - Newbie 12 03-28-2004 11:17 PM
KDE 3.1.1 made terminals use bash as default? red1busta Linux - Newbie 2 03-28-2003 11:39 PM

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

All times are GMT -5. The time now is 07:39 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