Checking if an Environment Variable is Set in UNIX Shell
We had a task to write a script to check if certain environment variables, which should not be set, were set. This task was straightforward with C shell:
if $?GARBAGEENV then
echo "Unset GARBAGEENV on `hostname`"
endif
With K shell, we did not find anything built-in. So we used something innovative.
if test "isenvset${GARBAGEENV}" != "isenvset"; then
echo "Unset GARBAGEENV on `hostname`"
fi
If GARBAGEENV was set to N, isenvset${GARBAGEENV} would be evaluated as isenvsetN (which is not equal to isenvset). If it was not set isenvset${GARBAGEENV} would be evaluated as isenvset.
if $?GARBAGEENV then
echo "Unset GARBAGEENV on `hostname`"
endif
With K shell, we did not find anything built-in. So we used something innovative.
if test "isenvset${GARBAGEENV}" != "isenvset"; then
echo "Unset GARBAGEENV on `hostname`"
fi
If GARBAGEENV was set to N, isenvset${GARBAGEENV} would be evaluated as isenvsetN (which is not equal to isenvset). If it was not set isenvset${GARBAGEENV} would be evaluated as isenvset.
Comments
Post a Comment