As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

bash scripting

cmsamocmsamo Registered User regular
edited August 2010 in Help / Advice Forum
I have a process that i have to do every so often - I need to change 2 lines in a configuration file and restart some processes. I usually do this by hand, but a number of other (non-technical) people need to be able to achieve the same goal, so I thought I would try and write a bash script to do this (my software runs on linux)

There are two lines in the config file that I need to change. The content of those lines depends on whichever option a user chooses:

There are 4 options. I want my script to change the contents of the lines in the file depending on which option the user chose.

I've got as far as defining sets of variables that contain the text lines, and I've got code to capture user input (basically a number between 1 and 4)

but.... how do I do "switch" type statements in bash scripting?

My idea is to switch based on option choice, and then invoke some kind of 'sed' command to search through the file and substitute using the variables I've defined for that option.

steam_sig.png
cmsamo on

Posts

  • Options
    Jimmy KingJimmy King Registered User regular
    edited August 2010
    bash has a case statement, which is the same as switch in every other language. I'll find an example shortly.

    Jimmy King on
  • Options
    cmsamocmsamo Registered User regular
    edited August 2010
    Below is what I found through google, but I can't make it work...
    echo Please Choose Profile:
    echo 1. Yellow
    echo 2. Blue
    echo 3. Red
    echo 4. Green
    echo
    echo Choose Number:
    read PCHOICE
    
    echo "Choice: $PCHOICE"
    
    if [ $PCCHOICE ="1" ]
    then
    echo "Yellow Chosen"
    fi
    if [ $PCCHOICE ="2" ]
    then
    echo "Blue Chosen"
    fi
    
    if [ $PCCHOICE ="3" ]
    then
    echo "Red Chosen"
    fi
    
    if [ $PCCHOICE ="4" ]
    then
    echo "Green Chosen"
    fi
    

    In each 'if' block, I want to do my sed stuff, once I've got the case statement working...

    cmsamo on
    steam_sig.png
  • Options
    Jimmy KingJimmy King Registered User regular
    edited August 2010
    Be careful with your quoting around the lvalue and rvalue there. That will make it behave differently and may be your problem since I see one of them has the rvalue quoted but not the others. I actually do almost all of my small scripts like this in perl because doing I always have to figure out what minor detail I screwed up in bash.

    I can't find my code that has a case statement in bash, but this seems to cover it prtty well. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

    Jimmy King on
  • Options
    cmsamocmsamo Registered User regular
    edited August 2010
    This seems to work for me....
    case "$PCHOICE" in
            "1")
                    echo "Threshold Baseline: $Ybase"
                    echo "RDDT Curve: $Yrddt"
            ;;
    
            "2")
                    echo "Threshold Baseline: $Bbase"
                    echo "RDDT Curve: $Brddt"
            ;;
    
            "3")
                    echo "Threshold Baseline: $Rbase"
                    echo "RDDT Curve: $Rrddt"
            ;;
    
            "4")
                    echo "Threshold Baseline: $Gbase"
                    echo "RDDT Curve: $Grddt"
            ;;
    
    esac
    

    Thanks for the link :)

    This can be locked...

    cmsamo on
    steam_sig.png
  • Options
    McVikingMcViking Registered User regular
    edited August 2010
    FYI, I think you want to add one more clause to handle invalid input. Something like:
    *)
          echo "Invalid selection.  Exiting."
          exit
    ;;
    

    Otherwise you may end up with unpredictable results where PCHOICE is defined as something invalid.

    McViking on
  • Options
    cmsamocmsamo Registered User regular
    edited August 2010
    Good point... Thanks

    cmsamo on
    steam_sig.png
Sign In or Register to comment.