[英]shell script unable to set environment variable with the grepped value
In the shell script, I want to grep a value from the log file and set as the value of one environment. 在shell脚本中,我想从日志文件中grep一个值并将其设置为一个环境的值。 In the log file, there is one line like this: 在日志文件中,有如下一行:
SOME_PATH=/some/specific/path
In my shell script, I grep the SOME_PATH
keyword, and I got the above line and split it with =
, but I'm now able to set the environment variable: 在我的shell脚本中,我grep SOME_PATH
关键字,我得到了上面的代码行并用=
拆分,但是现在我可以设置环境变量了:
line=`grep "SOME_PATH" /path/to/the/log.log`
path=${line##*=}
export SOME_PATH="$path" #I cannot change the environment variable with this line.
And if I just have the script like below, the environment variable changes. 如果我只有下面的脚本,环境变量也会改变。
export SOME_PATH=/some/specific/path
Exported variables available either in processes spawned from shell (script) which export
ed a var or in another script with source
d script with export
statement. 导出变量可用于从export
了var的shell(脚本)生成的进程中,也可以在带有带有export
语句的source
d脚本的另一个脚本中使用。
1st way: 第一种方式:
$ cat exp.sh
#!/bin/bash
export MYVAR="hi there"
bash
# or bash -c "echo here: $MYVAR"
2nd way: 第二种方式:
$ cat exp.sh
#!/bin/bash
export MYVAR="hi there"
$ cat imp.sh
#!/bin/bash
. exp.sh
echo $MYVAR
Here's another explanation: advanced bash scripting guide . 这是另一种解释: 高级bash脚本编制指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.