日期:2014-05-16  浏览次数:20677 次

shell脚本中source问题
shell脚本(test.sh)如下:

#!/bin/sh

echo "export monitor=1" >> /etc/profile
source /etc/profile

调用后(./test.sh),执行echo $monitor,没有输出任何值

手动source /etc/profile后,再次执行echo $monitor,输出了预期的值

可见脚本中的source /etc/profile没有成功,请问这个应该怎么解决?


------解决方案--------------------
test.sh执行的时候是monitor是有值的. 但是test.sh是在一个shell里执行的,test.sh结束后,shell退出了。
回到了楼主登录用shell,由于子进程的环境变量不影响父进程,所以echo 的时候是没有值的。