Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Bourne Shell scripting help

  • 04-06-2009 03:52PM
    #1
    Closed Accounts Posts: 751 ✭✭✭


    Checking an enviroment variable is set

    What i can do simply in bash with
    if [ -z $ENV_VER ] ; then



    i have tied this on borne and cant get it to work wothout


    "sh: ENV_VER: Parameter not set."


    I have tried:(what i can remember trying)

    if [-n ${var:-x}]

    [ ! -z $var ] && echo "set" || echo "not"

    ${var:-'SomeDefault'}

    if [ "X${VAR}" != "X" ] ;then echo "hi" ;fi;


Comments

  • Registered Users, Registered Users 2 Posts: 39 Talon.ie




  • Registered Users, Registered Users 2 Posts: 4,282 ✭✭✭bullpost


    If you're still stuck I think you can use the test command to do this iirc.

    Look up the man page for test but it should be something like:

    if test $A then ......


  • Moderators, Technology & Internet Moderators Posts: 1,337 Mod ✭✭✭✭croo


    You sure
    if [ "X${VAR}" != "X" ] ;then echo "hi" ;fi;
    doesn't work?

    As is, it would only display "hi" when the VAR is set! Rather than when it is empty as you initially said. But I'm guessing that is a typo?
    if [ "X${VAR}" == "X" ] ;then echo "hi" ;fi;
    to say "hi" when VAR is not set

    A great way to test this things I find is to enabling debugging with set -x

    set -x
    if [ "X${VAR}" == "X" ] ;then echo "hi" ;fi;

    then at the prompt
    $ export VAR= (just to be sure!)
    $ ./script.sh
    ++ ''
    ++ echo hi
    hi

    $ export VAR=yes
    $ ./script.sh
    ++ ''


  • Closed Accounts Posts: 751 ✭✭✭JimmyCrackCorn!


    [ -z $A'' ]

    Works


Advertisement