Results 1 to 5 of 5

Thread: [SOLVED] shell script if/then

  1. #1
    Join Date
    Oct 2007
    Beans
    832
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    [SOLVED] shell script if/then

    I'm having difficulty grasping how to correctly pull the output of $? and make a correct if/then statement out of it. I always seem to end up with the same result regardless of which selection I make in the window.

    In order to keep it simple, I've just changed it as follows, so you can quickly grasp what I'm trying to accomplish.

    Code:
    zenity --question --text "Need help?"; echo $?
    
    if [ $? = 0 ] ; then
    
    echo "Let me help you then"
    
    else
    
    echo "Ok, good. You got it."
    
    fi

    Also, can anyone provide a good on-line source for different types of IF/THEN statements. All my resources are very limited and always give you the one or two most typical examples and that's it.

    thanks in advance...
    Last edited by mdpalow; December 10th, 2007 at 07:01 PM.

  2. #2
    Join Date
    May 2007
    Beans
    245
    Distro
    Ubuntu 10.04 Lucid Lynx

    Lightbulb Re: shell script if/then

    Simply remove the
    Code:
    ; echo $?
    frome the end of the first line. It "eats" the Zenity exit code and produces its own... your if statement always reads the code from echo which is always going to be the same.

  3. #3
    Join Date
    Oct 2007
    Beans
    832
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: shell script if/then

    I understand now... thanks very much for your help NatanB

  4. #4
    Join Date
    May 2007
    Beans
    39
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: shell script if/then

    By the way, you can even put directly the command in the if expression:

    Code:
     
    #!/bin/bash
    
    if zenity --question --text "Need help?"; then
    
               echo "Let me help you then"
    
    else
    
               echo "Ok, good. You got it."
    
    fi
    Well, this change doesn't give any performance benefit but it (often) makes the code to be more readable.

  5. #5
    Join Date
    Oct 2007
    Beans
    832
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [SOLVED] shell script if/then

    Thanks Trumpen...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •