Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, I'm using bash version 4.2.24(1)-release on ubuntu 12.04 Whenever I try to run the following code, I get some strange output Code: if [ -n "${BUILD_TYPE+x}" ]; then ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    3

    Environment Variables not comparing to set Strings properly


    Hi all,
    I'm using bash version 4.2.24(1)-release on ubuntu 12.04

    Whenever I try to run the following code, I get some strange output

    Code:
    if [ -n "${BUILD_TYPE+x}" ]; then
    	if [ "${BUILD_TYPE}" == "dev" ]; then
                  #set pkg version accordingly
    	fi
            if [ "${BUILD_TYPE}" == "prod" ]; then
                   #set pkg version accordingly
    	fi
    else
            #Do some stuff
    fi
    
    dpkg --build
    If I run this with
    Code:
    export BUILD_TYPE=dev
    I get
    Code:
    [: dev: unexpected operator
    [: dev: unexpected operator
    And then dpkg will build a debian package with the last successful version. However, if I run this code on Mac OS x 10.6.8 with bash version 3.2.48(1)-release (minus the dpkg step) I don't get these errors and receive the expected values.

    I also tried running
    Code:
    #!/bin/sh
    
    BUILD_TYPE=prod
    
    if [ -z ${BUILD_TYPE} ]; then
      echo "BUILD_TYPE not set"
      exit 1
    fi
    
    if [ "${BUILD_TYPE}"="prod" ]; then
      echo "BUILD_TYPE is prod"
    elif [ "${BUILD_TYPE}"="dev" ]; then
      echo "BUILD_TYPE is dev"
    else
      echo "BUILD_TYPE is neither prod nor dev"
      exit 1
    fi
    
    exit 0
    On my mac, it behaves as expected. On Ubuntu, it won't print out any "unexpected operator" messages, but it will print out "BUILD_TYPE is prod" if $BUILD_TYPE is set, no matter what it's set to.

    This script seems pretty simple to me, so I have honestly no idea why this is failing. Any input would be greatly appreciated.
    Thanks!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    Quote Originally Posted by JLucci
    Code:
    if [ -n "${BUILD_TYPE+x}" ]; then
    what is the +x for in this statement?

    are you just trying to evaluate the BUILD_TYPE variable, and then act accordingly if one of "dev" or "prod"? If so, then you could do something like this:
    Code:
    #!/bin/bash
    
    # get mode from command line arguments 
    [ $# -ne 1 ] && echo "Usage: $0 {dev|prod}" && exit 1
    BUILD_TYPE=$1
    
    # see what argument was given
    case $BUILD_TYPE in
      dev)
        echo 'Doing stuff for "dev"'
        ;;
      prod)
        echo 'Doing stuff for "prod"'
        ;;
      *)
        [ $# -ne 0 ] && echo "BUILD_TYPE \`$BUILD_TYPE' is invalid"
        echo "Usage: $0 {dev|prod}"
        exit 1
    esac
    
    echo "Now doing dpkg stuff with BUILD_TYPE set to '$BUILD_TYPE'"
    Edit: Just an aside, you don't have to use a command line arg, you can still use the BUILD_TYPE env var. just comment out the first two lines that grab the argument, e.g.:

    Code:
    #[ $# -ne 1 ] && echo "Usage: $0 {dev|prod}" && exit 1
    #BUILD_TYPE=$1
    Edit2: also, i guess i should have made it clear, you'd call the above code (assuming you saved it to a script named "script.sh" and made it executable) like this:
    Code:
    ./script.sh dev
    ./script.sh prod
    Last edited by atreyu; 10-02-2012 at 11:57 PM. Reason: usage

  3. #3
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    174
    Quote Originally Posted by JLucci View Post
    Hi all,
    I'm using bash version 4.2.24(1)-release on ubuntu 12.04

    Whenever I try to run the following code, I get some strange output

    Code:
    if [ -n "${BUILD_TYPE+x}" ]; then
    	if [ "${BUILD_TYPE}" == "dev" ]; then
                  #set pkg version accordingly
    	fi
            if [ "${BUILD_TYPE}" == "prod" ]; then
                   #set pkg version accordingly
    	fi
    else
            #Do some stuff
    fi
    
    dpkg --build
    If I run this with
    Code:
    export BUILD_TYPE=dev
    I get
    Code:
    [: dev: unexpected operator
    [: dev: unexpected operator
    And then dpkg will build a debian package with the last successful version. However, if I run this code on Mac OS x 10.6.8 with bash version 3.2.48(1)-release (minus the dpkg step) I don't get these errors and receive the expected values.

    I also tried running
    Code:
    #!/bin/sh
    
    BUILD_TYPE=prod
    
    if [ -z ${BUILD_TYPE} ]; then
      echo "BUILD_TYPE not set"
      exit 1
    fi
    
    if [ "${BUILD_TYPE}"="prod" ]; then
      echo "BUILD_TYPE is prod"
    elif [ "${BUILD_TYPE}"="dev" ]; then
      echo "BUILD_TYPE is dev"
    else
      echo "BUILD_TYPE is neither prod nor dev"
      exit 1
    fi
    
    exit 0
    On my mac, it behaves as expected. On Ubuntu, it won't print out any "unexpected operator" messages, but it will print out "BUILD_TYPE is prod" if $BUILD_TYPE is set, no matter what it's set to.

    This script seems pretty simple to me, so I have honestly no idea why this is failing. Any input would be greatly appreciated.
    Thanks!
    The "+x" isn't something I recognize in the first script, so I can't help with that one.

    But the second version of the script needs spaces around the "=" signs comparing the BUILD_TYPE variable. Without spaces it always results in a "true".

Posting Permissions

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