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.
- 10-02-2012 #1Just 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
If I run this withCode: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
I getCode:export BUILD_TYPE=dev
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.Code:[: dev: unexpected operator [: dev: unexpected operator
I also tried running
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.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
This script seems pretty simple to me, so I have honestly no idea why this is failing. Any input would be greatly appreciated.
Thanks!
- 10-02-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,673
Hi,
what is the +x for in this statement?
Originally Posted by JLucci
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:
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:#!/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'"
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:#[ $# -ne 1 ] && echo "Usage: $0 {dev|prod}" && exit 1 #BUILD_TYPE=$1
Code:./script.sh dev ./script.sh prod
Last edited by atreyu; 10-02-2012 at 11:57 PM. Reason: usage
- 10-04-2012 #3Linux Newbie
- Join Date
- Jun 2012
- Location
- SF Bay area
- Posts
- 101


Reply With Quote

