Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bourne Shell scripting help

Options
  • 04-06-2009 3:52pm
    #1
    Closed Accounts Posts: 752 ✭✭✭


    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 Posts: 4,218 ✭✭✭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,334 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: 752 ✭✭✭JimmyCrackCorn!


    [ -z $A'' ]

    Works


Advertisement