2

I'd like to launch a script that sets and/or updates some environment variables. Unlike this question, I don't mind launching the script as

. /path/to/my/script/myScript.sh

to force the script to run the same process as the parent, however I don't want the script to silently fail if the parent forgets. id' like to detect the problem and generate an error message.

So, what's the best way to tell if the script has been executed inside it's parent's process?

3

Check one of the variables. If it is empty or has not been set, show the error

if [[ -z "$some_variable" ]]; then
    echo "You must 'source /path/to/my/script/myScript.sh' first" >&2
    exit 1
fi

Of course, you could just as easily source the file and continue, if you want a more seamless workflow for your users.

  • Hi Glen. Thanks for suggesting this. I was hoping to do a check inside the called script as opposed to the parent. There are a couple of different reasons for this: 1) The script may be called from the command line (so I've got no parent script to modify). 2) The script may modify variables, so checking that the variables exist wouldn't work. – Stormcloud Mar 25 '14 at 9:13
  • So you want myScript to tell the user if he forgot to source myScript? – glenn jackman Mar 25 '14 at 13:30
  • Sorry - I didn't make that clear. I'd like the called script (myScript.sh in your example) to generate the error message. choroba has added a comment that links to a bit of magic with $0 that could be used used this way. Thank you for helping – Stormcloud Mar 26 '14 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.