Results 1 to 5 of 5

Thread: bash script: if/else question

  1. #1
    Join Date
    May 2005
    Location
    Oslo, Norway
    Beans
    467
    Distro
    Ubuntu 9.10 Karmic Koala

    bash script: if/else question

    I'm trying to work my way through a script that should be easy. I start with something like this:

    Code:
    # How would you like to molest your file today?
    DESIRED_OPTIONS=`zenity --list  --checklist  --width 800 --height 600 --separator " | " --column "Use" --column "Option" --column "Explanation" \
    TRUE "Title" "Set the title" \
    FALSE "Comment" "Make your comment" \
    TRUE "File format" "Select a file format/ extension (like avi)" \
    TRUE "Video codec" "Select video codec to use" \
    FALSE "Qmin" "Set minimum video quantiser" \
    etc... `
    Notice that two of these are FALSE. The first one (Comment) runs fine with this entry:

    Code:
    ## Set -comment.
    if echo $DESIRED_OPTIONS | grep -q "Comment"
       then COMMENT=`zenity --entry --title="File comment" --text="Do you have any comments to be written to the output file?"`
       else echo "You did not wish to set a comment"
    fi
    
    if [ "${PIPESTATUS[0]}" != "0" ]
       then
       exit 1
    fi
    But it just stops after this one:

    Code:
    if echo $DESIRED_OPTIONS | grep -q "Qmin"
       then QMIN=`zenity --entry --title="Min. video quantiser" --text="Set MINIMUM video quantiser scale. Default is 1" --entry-text="1"`
       else echo "You did not wish to use Qmin"
    fi
    
    if [ "$QMIN" = "" ];
          then
         exit 1
    fi
    If I set Qmin to TRUE (checking the box in zenity), it runs through the command.
    It also just stops if I leave out the "else echo "You did not wish to use Qmin""
    I know it runs that else command if it is set to FALSE, because that shows up in terminal.

    Can someone see what I'm doing wrong here?
    I'm trying to finish off all of my blocks as I move along, because I find that it's easier to keep track of myself by doing that.

    Thanks!

    - Ketil
    Last edited by kwaanens; August 26th, 2007 at 04:21 PM. Reason: typo
    || My blog ||

  2. #2
    Join Date
    May 2005
    Location
    Oslo, Norway
    Beans
    467
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: bash script: if/else question

    Nevermind...
    I of course forgot this:


    if [ "$QMIN" = "" ];
    then
    exit 1
    fi

    "$QMIN" = "" is true, when I did not give a value for it...

    - Ketil
    Last edited by kwaanens; August 26th, 2007 at 04:23 PM.
    || My blog ||

  3. #3
    Join Date
    Feb 2007
    Beans
    2,729

    Re: bash script: if/else question

    Use the simpler:

    Code:
    [ -z "$QMIN" ] && exit 1
    MrC
    Last edited by Mr. C.; August 27th, 2007 at 04:58 PM.

  4. #4
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: bash script: if/else question

    Quote Originally Posted by Mr. C. View Post
    Use the more simpler:

    Code:
    [ -z "$QMIN" ] && exit 1
    MrC
    What about
    Code:
    [ "$QMIN" ] && exit 1
    ?
    I have to admit I am somewhat puzzled when I learned an empty string test. Is it identical to the -z test?

  5. #5

    Re: bash script: if/else question

    Quote Originally Posted by cwaldbieser View Post
    What about
    Code:
    [ "$QMIN" ] && exit 1
    ?
    I have to admit I am somewhat puzzled when I learned an empty string test. Is it identical to the -z test?
    The following are equivalent (the test is true if the string is not the null string):

    Code:
    [ "$QMIN" ] && echo exists
    Code:
    [ -n "$QMIN" ] && echo exists
    The following are equivalent and are opposite of the above (the test is true if the string is null):

    Code:
    [ "$QMIN" = "" ] && echo does not exist
    Code:
    [ -z "$QMIN" ] && echo does not exist
    Code:
    [ xx = x${QMIN}x ] && echo does not exist

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
  •