bash Linux 源代码在 .sh 文件中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18233285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux source does not work in .sh file?
提问by Marc
I have a .sh (start_sim.sh
) and a .bash (sim_sources.bash
) file.
The sim_sources.bash
file is called from within the start_sim.sh
and should set an environment variable $ROBOT
to a certain value. However the ROBOT
variable never changes when I call ./start_sim.sh
. Is there a fundamental mistake in the way I am trying to do this?
我有一个 .sh ( start_sim.sh
) 和一个 .bash ( sim_sources.bash
) 文件。该sim_sources.bash
文件是从内部调用的,start_sim.sh
并且应该将环境变量$ROBOT
设置为某个值。但是,ROBOT
当我调用./start_sim.sh
. 我尝试这样做的方式是否存在根本性错误?
start_sim.sh
contains:
start_sim.sh
包含:
#!/bin/bash
echo -n "sourcing sim_sources.bash..."
source /home/.../sim_sources.bash
echo "done."
sim_sources.bash
contains:
sim_sources.bash
包含:
# set the robot id
export ROBOT=robot
EDIT: Could you also propose a way to work around this issue? I would still need to set variables from with in the .bash file.
编辑:您能否还提出一种解决此问题的方法?我仍然需要在 .bash 文件中设置变量。
EDIT2: Thanks for your replys! Finally I ended up solving it with a screen and stuffing commands to it:
EDIT2:感谢您的回复!最后我最终用一个屏幕和填充命令来解决它:
echo -n "starting screen..."
screen -dmS "sim_screen"
sleep 2
screen -S "sim_screen" -p 0 -X stuff "source /home/.../sim_sources.bash$(printf \r)"
sleep 5
screen -S "sim_screen" -p 0 -X stuff "source /home/.../start_sim.sh$(printf \r)"
回答by Brian Agnew
You're setting the ROBOT
variable inthe start_sim.sh script
, but that's notavailable to parent processes (your spawning shell/command-prompt).
你设置的ROBOT
变量中的start_sim.sh script
,但是这并不适用于父进程(您的产卵壳/命令提示符)。
Exporting a variable e.g. export ROBOT=robot
makes the variable available to the current process and child processes. When you invoke ./start_sim.sh
you're invoking a new process.
导出一个变量,例如export ROBOT=robot
使该变量可用于当前进程和子进程。当您调用时,./start_sim.sh
您正在调用一个新流程。
If you simply source start_sim.sh
in your shell, that script runs as part of your shell process and then your variable will be available.
如果您只是source start_sim.sh
在 shell 中,该脚本将作为 shell 进程的一部分运行,然后您的变量将可用。
回答by vegi
As Brian pointed out the variables are not available outside of the script.
正如布赖恩指出的那样,变量在脚本之外不可用。
Here a adapted script that shows this point:
这是一个显示这一点的改编脚本:
#!/bin/bash
echo -n "sourcing sim_sources.bash..."
. sim_sources.bash
echo $ROBOT
echo "done."
The workaround you are asking for is to start a new shell from the actual shell with the environmental values already set:
您要求的解决方法是从已设置环境值的实际 shell 启动一个新 shell:
#!/bin/bash
echo -n "sourcing sim_sources.bash..."
. sim_sources.bash
echo "done."
bash
This results in:
这导致:
bash-4.1$ printenv | grep ROBOT
ROBOT=robot
回答by sudhanshu
I am on Ubuntu 16.04
我在 Ubuntu 16.04
I used /bin/sh instead of /bin/bash and it works !
我使用 /bin/sh 而不是 /bin/bash 并且它有效!