How to log if an environment variable is unset?
NickName:xyz Ask DateTime:2011-10-19T14:15:28

How to log if an environment variable is unset?

How can I write a shell script that checks for an environment variable and writes to a log file if the variable is unset?

Copyright Notice:Content Author:「xyz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7817267/how-to-log-if-an-environment-variable-is-unset

Answers
flolo 2011-10-19T06:23:01

The command to write to the log is logger.\nAnd you test if a variable is set with test -v, so in your script you must have the lines:\n\nif test ! -v VARNAME; then logger Variable VARNAME is unset; fi\n\n\nEDIT: In case you mean with log just an arbitrary log file and not the system log, you can of course replace the logger with echo bla bla > log.file.",


Jonathan Leffler 2011-10-19T06:23:45

If you only want a message when it is unset, then:\n\nif [ -z \"${CHOSEN_ENV_VAR}\" ]\nthen echo \"CHOSEN_ENV_VAR was not set but should have been\" >> log.file\nfi\n\n\nIf you simply want the script to stop and report on stderr, then:\n\n: ${CHOSEN_ENV_VAR:?'was not set but should have been'}\n\n\n(You can test that in an interactive shell, but the interactive shell won't exit. Put it in a script and the script is exited.)",


More about “How to log if an environment variable is unset?” related questions

How to log if an environment variable is unset?

How can I write a shell script that checks for an environment variable and writes to a log file if the variable is unset?

Show Detail

How to unset environment variable in Jenkins

I have a Jenkinsfile and the JAVA_HOME environment variable is already set when the process starts up. Unfortunately, this environment variable is being passed into my container when I run a script...

Show Detail

How to unset an environment variable using php?

I know I can set an environment variable using putenv("ENV_FOO=SOMETHING"); and get the value via: getenv("ENV_FOO"); If the variable isn't set, getenv("ENV_FOO") will return false.&#x

Show Detail

Node.js unset environment variable

How do you unset an environment variable in Node.js? I've tried: process.env.MYVAR = undefined But that doesn't unset it, it appears as follows: console.log('xx' + process.env.MYVAR + '

Show Detail

How to unset an environment variable from docker?

I want to unset the environment variable through entry point scripts. I am sourcing the cleanup.sh in entry point script. But the unsetting of the environment variable is not happening. There ar...

Show Detail

Unset Environment Variable in grunt-env

Is there a way to unset environment variables with grunt-env? I know I can set them from the module, and there is even a way to set them to a value only if it doesn't exist. However, the project's ...

Show Detail

How to unset an environment variable in Eclipse run configuration?

I have an external run configuration for docker-compose, and need to disable TLS verification because my VPN messes with my connection and I connect through a forwarded port because of that. The o...

Show Detail

Unset environment variable in Jupyter Notebook

How do I unset an environmental variable in Jupyter Notebook? I set the variables using a .env file that is loaded by: from dotenv import load_dotenv load_dotenv('./.env') After changing the .env...

Show Detail

Unset an environment variable for a single command

In Bash, we can set an environment variable for a single command this way: FOO=bar somecommand What if we want to unset a variable for a single command?

Show Detail

Unset environment variable when Eclipse application is launched?

I've got a situation where a user has the 'JAVA_TOOL_OPTIONS' environment variable set on their workstation ... but the option is causing my Eclipse application to crash. Is there a way, in the ec...

Show Detail