determine if value is set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robbieg
    New Member
    • Jan 2008
    • 9

    determine if value is set

    Hey all,

    first post here and as im sure once i ask my question.. you will all know that i am very new the shell scripting. I spend all of time in php, not shell.

    I need to figure out a way to see if a shell varable has a value in it.

    I have
    Code:
    SHELL="/bin/sh"
    import EXT
    import HOST
    VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
    VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
    FILE_SIZE=$SIZE
    
    
    logfile "/var/log/qmail/maildrop.log"
    set -x
    log "==== BEGIN maildrop processing for $EXT@$HOST ==="
    log "Home directory is $VHOME"
    log "File size $SIZE"
    
    if ( $VHOME eq ' ' )
    {
    log "ERROR: VHOME isn't set, falling back to vdelivermail"
    log "=== EXIT === "
    to "$VPOP"
    }
    my log files show entry's for $VHOME and $FILE_SIZE and $SIZE, but yet.. my script is saying that $VHOME is empty.

    Knowing that my $VHOME has a value.. what am i doing wrong that is making it process the if statement? I have tried $VHOME = '', $VHOME =='', $VHOME eq '', $VHOME eq "".
  • bykwzpz
    New Member
    • Jan 2008
    • 12

    #2
    Originally posted by robbieg
    Hey all,

    first post here and as im sure once i ask my question.. you will all know that i am very new the shell scripting. I spend all of time in php, not shell.

    I need to figure out a way to see if a shell varable has a value in it.

    I have
    Code:
    SHELL="/bin/sh"
    import EXT
    import HOST
    VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
    VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
    FILE_SIZE=$SIZE
    
    
    logfile "/var/log/qmail/maildrop.log"
    set -x
    log "==== BEGIN maildrop processing for $EXT@$HOST ==="
    log "Home directory is $VHOME"
    log "File size $SIZE"
    
    if ( $VHOME eq ' ' )
    {
    log "ERROR: VHOME isn't set, falling back to vdelivermail"
    log "=== EXIT === "
    to "$VPOP"
    }
    my log files show entry's for $VHOME and $FILE_SIZE and $SIZE, but yet.. my script is saying that $VHOME is empty.

    Knowing that my $VHOME has a value.. what am i doing wrong that is making it process the if statement? I have tried $VHOME = '', $VHOME =='', $VHOME eq '', $VHOME eq "".
    Hi,

    You might try -eq instead of just eq.

    Hope this helped.

    Comment

    • robbieg
      New Member
      • Jan 2008
      • 9

      #3
      no. that did not fix my issue.

      [HTML]if ( $VHOME -eq "" )[/HTML]

      Comment

      • bykwzpz
        New Member
        • Jan 2008
        • 12

        #4
        Okay, now I see the problem. The clue was that you normally operate in PHP. The if statement that you have is fine in PHP, but shell has a different structure. Try this:

        [CODE]
        if [ $VMHOME = "" ]
        then
        <what you want to do if VMHOME not set>
        fi
        [\CODE]

        Spaces around the braces ( [ ] ) are required. I tried this on my own system and it works reliably.

        Comment

        • robbieg
          New Member
          • Jan 2008
          • 9

          #5
          i now get an Syntax_error_af ter_if/ in my log files.

          my code is as follows.

          Code:
          if [ $VHOME == '' ]
          log "ERROR: VHOME isn't set, falling back to vdelivermail"
          log "=== EXIT === "
          to "$VPOP"
          fi

          Comment

          • bykwzpz
            New Member
            • Jan 2008
            • 12

            #6
            Originally posted by robbieg
            i now get an Syntax_error_af ter_if/ in my log files.

            my code is as follows.

            Code:
            if [ $VHOME == '' ]
            log "ERROR: VHOME isn't set, falling back to vdelivermail"
            log "=== EXIT === "
            to "$VPOP"
            fi
            You don't have the 'then' in your if statement. It comes right after the if line:
            Code:
            if [ <expession> ]
            then
            <statements>
            fi
            May also be written as:

            [CODE]
            if [ <expression> ] ; then
            <statements>
            fi
            [\CODE]

            Also, you will probably get some errors from your log statements. Unless you have written 'log' as a procedure call somewhere else in your shell procedure, you will get an error on each of the lines starting with 'log' and for sure on the line starting with 'to'.

            In your if test expression, you are using a PHP equals. Only a single equal sign (=) is used in shell for string comparison.

            Comment

            • robbieg
              New Member
              • Jan 2008
              • 9

              #7
              this is my script.

              this is my script.

              Code:
              #! /bin/sh
              import EXT
              import HOST
              VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
              VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
              FILE_SIZE=$SIZE
              
              
              logfile "/var/log/qmail/maildrop.log"
              log "==== BEGIN maildrop processing for $EXT@$HOST ==="
              log "Home directory is $VHOME"
              log "File size $SIZE"
              
              if [$VHOME = '' "]; then
              log "ERROR: VHOME isn't set, falling back to vdelivermail"
              then
              log "=== EXIT === "
              then
              to "$VPOP"
              fi
              log is defined. in my script, im using 1 = sign, and i have placed my then after the ;

              the script is still telling me there is an error after if on line 14.

              Comment

              • bykwzpz
                New Member
                • Jan 2008
                • 12

                #8
                Originally posted by robbieg
                this is my script.

                this is my script.

                Code:
                #! /bin/sh
                import EXT
                import HOST
                VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
                VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
                FILE_SIZE=$SIZE
                
                
                logfile "/var/log/qmail/maildrop.log"
                log "==== BEGIN maildrop processing for $EXT@$HOST ==="
                log "Home directory is $VHOME"
                log "File size $SIZE"
                
                if [$VHOME = '' "]; then
                log "ERROR: VHOME isn't set, falling back to vdelivermail"
                then
                log "=== EXIT === "
                then
                to "$VPOP"
                fi
                log is defined. in my script, im using 1 = sign, and i have placed my then after the ;

                the script is still telling me there is an error after if on line 14.
                Look at the extra quote character just before the right brace. Also, what are the extra 'then' lines doing in the script on lines above and below the 'log "=== EXIT' line?

                Comment

                • robbieg
                  New Member
                  • Jan 2008
                  • 9

                  #9
                  Originally posted by bykwzpz
                  Look at the extra quote character just before the right brace. Also, what are the extra 'then' lines doing in the script on lines above and below the 'log "=== EXIT' line?
                  Code:
                  import EXT
                  import HOST
                  VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
                  VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
                  FILE_SIZE=$SIZE
                  
                  
                  logfile "/var/log/qmail/maildrop.log"
                  log "==== BEGIN maildrop processing for $EXT@$HOST ==="
                  log "Home directory is $VHOME"
                  log "File size $SIZE"
                  
                  if [$VHOME = '']; then
                  log "ERROR: VHOME isn't set, falling back to vdelivermail"
                  log "=== EXIT === "
                  to "$VPOP"
                  fi
                  same error. _Syntax_error_a fter_if/ is it worth saying that my OS is fedora 8?

                  Comment

                  • WinblowsME
                    New Member
                    • Jan 2008
                    • 58

                    #10
                    I haven't done any shell scripting in ages, but I do recall a -z string function to check if the length of the variable is zero.



                    Code:
                    if [ -z "$variable" ] ; then
                       echo "\$variable is not set."
                    fi

                    Comment

                    • bykwzpz
                      New Member
                      • Jan 2008
                      • 12

                      #11
                      Fedora's sh should execute like UNIX/LINUX sh, so I don't think that is the cause. Are you sure that you have spaces between the braces? I'm going to use underscore instead of space below to illustrate where spaces are required:

                      Code:
                      #example showing underscores instead of spaces
                      if_[_$VMHOME_=_''_]_;_then
                      
                      #example showing spaces
                      if [ $VMHOME = '' ] ; then
                      This shows how to construct the expression so that VMHOME would always have a value but will still detect if it was empty. Consider this:

                      Code:
                      if [ "X$VMHOME" = "X" ] ; then
                      <do your log stuff here>
                      fi
                      This permits checking for an empty VMHOME, but will keep the shell from complaining if the variable is not set. I also agree with WinblowsMe's post that a -z test option is available.

                      Comment

                      • WinblowsME
                        New Member
                        • Jan 2008
                        • 58

                        #12
                        Robbie, do what bykwzpz said and put spaces around the brackets and add double-quotes to the variable.

                        Code:
                        import EXT
                        import HOST
                        VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
                        VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
                        FILE_SIZE=$SIZE
                         
                        logfile "/var/log/qmail/maildrop.log"
                        log "==== BEGIN maildrop processing for $EXT@$HOST ==="
                        log "Home directory is $VHOME"
                        log "File size $SIZE"
                         
                        if [ "$VHOME" = "" ] ; then
                           log "ERROR: VHOME isn't set, falling back to vdelivermail"
                           log "=== EXIT === "
                           to "$VPOP"
                        fi
                        Or try

                        Code:
                        if [ -z "$VHOME" ] ; then
                           log "ERROR: VHOME isn't set, falling back to vdelivermail"
                           log "=== EXIT === "
                           to "$VPOP"
                        fi

                        Comment

                        • robbieg
                          New Member
                          • Jan 2008
                          • 9

                          #13
                          thanks everyone for all the help so far! but it's still not working for me...

                          Code:
                          #! /bin/sh
                          import EXT
                          import HOST
                          VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
                          VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
                          FILE_SIZE=$SIZE
                          
                          
                          logfile "/var/log/qmail/maildrop.log"
                          log "==== BEGIN maildrop processing for $EXT@$HOST ==="
                          log "Home directory is $VHOME"
                          log "File size $SIZE"
                          
                          if [ -z "$VHOME"  ] ; then
                          	log "ERROR: VHOME isn't set, falling back to vdelivermail"
                          	log "=== EXIT === "
                          	to "$VPOP"
                          fi
                          i made sure that there are spaces in my file config, and that im using the "" around my $VHOME value. log files still say ":_Syntax_error _after_if/" on line 14 which is the first time the If statement present.

                          Comment

                          • bykwzpz
                            New Member
                            • Jan 2008
                            • 12

                            #14
                            Hi Robbie,

                            Try this script in a separate file. I called mine 'mytest'. When you have created this script, use chmod +x to make it executable. Then execute it using ./mytest from your command prompt.

                            If you get the message "Error - NO VHOME", then the structure of the if statement is sound. The syntax error is somewhere else - perhaps in some of the statements within the if statement. If you don't get the message, then there may be a problem prior to the if statement that does not affect anything until the if statement is encountered.

                            Code:
                            #!/bin/sh
                            
                            VHOME=""
                            
                            if [ -z "$VHOME" ] ; then
                            
                            echo "Error - no VMHOME"
                            exit 1
                            
                            fi
                            
                            echo "ERROR - VHOME IF TEST FAILED"
                            
                            exit 0

                            Comment

                            • robbieg
                              New Member
                              • Jan 2008
                              • 9

                              #15
                              i did as u suggested.. and it said that ERROR VHOME IF TESTED FAILED.

                              Comment

                              Working...