无法在linux中获取环境变量

时间:2021-07-13 22:46:50

I've got a .sh script and it has this "if" in it:

我有一个.sh脚本,它有“if”:

    if [ -z "$SOME_LIB" ]; then
    echo "Environment variable SOME_LIB is needed!"
    exit 1
    fi

Now. When I type "printenv" I can see that "SOME_LIB" has value "/usr/local/lib/python2.6 ". That path is correct. And all is good. But when I want to run that .sh file from terminal is gives me "Environment variable SOME_LIB is needed!"

现在。当我输入“printenv”时,我可以看到“SOME_LIB”的值为“/usr/local/lib/python2.6”。那条路是正确的。一切都很好。但是当我想从终端运行那个.sh文件时,我会发现“需要环境变量SOME_LIB!”

What do i have to do to get it running properly?

我需要做些什么才能让它正常运行?

Thank's for your help!!

谢谢你的帮助!!

1 个解决方案

#1


1  

Could you add printenv just befor the command if in the script?

如果在脚本中,你能为命令添加printenv吗?

It is possible that the variable is unset in the script before.

之前可能在脚本中未设置变量。

Another option, debug the script using set -x in it or running it using bash -x to see what value contains the variable while checking.

另一个选项是,使用set -x调试脚本或使用bash -x运行它以查看检查时包含变量的值。

#1


1  

Could you add printenv just befor the command if in the script?

如果在脚本中,你能为命令添加printenv吗?

It is possible that the variable is unset in the script before.

之前可能在脚本中未设置变量。

Another option, debug the script using set -x in it or running it using bash -x to see what value contains the variable while checking.

另一个选项是,使用set -x调试脚本或使用bash -x运行它以查看检查时包含变量的值。