[英]Execute code in the context of another *.sh file
I am a newbie in these matters and feel kind of lost. 我在这些事情上是新手,感到有点迷茫。
My System is Windows 7. 我的系统是Windows 7。
I have an external initialization file init.sh : 我有一个外部初始化文件init.sh :
# Some code...
exec "${BASH}" --login -i
Now I am writing my own file myFile.sh which starts by calling init.sh and which should execute further code in the bash context of init.sh . 现在,我正在编写自己的文件myFile.sh ,该文件首先调用init.sh ,并且应在init.sh的bash上下文中执行更多代码。 I tried:
我试过了:
exec "init.sh"
# Further code here
Which is not working. 这不起作用。 I would be grateful for any help.
我将不胜感激。
exec
instantly replaces a process with whatever was exec
-ed. exec
立即用exec
-ed替换进程。 You can't run code after an exec
, because the code that was running before the exec
is gone , replaced by the new process. 您不能在
exec
之后运行代码,因为在exec
消失之前运行的代码已被新进程替换。 Normally, replacing exec "init.sh"
with . "init.sh"
通常,将
exec "init.sh"
替换为. "init.sh"
. "init.sh"
(or source "init.sh"
) would allow you to run the contents of init.sh
in the current shell without replacing the shell, but because init.sh
itself includes an exec
, that won't work. . "init.sh"
(或source "init.sh"
)将允许您在当前shell中运行init.sh
的内容,而无需替换该shell,但是由于init.sh
本身包含exec
,因此无法正常工作。
Given that init.sh
is running in interactive mode, it doesn't make sense to execute further code after that in any event; 鉴于
init.sh
在交互模式下运行, init.sh
在此之后再执行更多代码是没有意义的。 a shebang ( #!init.sh
) line could be used to make init.sh
the interpreter for the remaining code, but since it's interactive, that doesn't make sense. 可以使用shebang(
#!init.sh
)行来使init.sh
成为其余代码的解释器,但是由于它是交互式的,因此没有任何意义。
It sounds an awful lot like you're trying to reinvent .bashrc
/ .bash_profile
/ .bash_login
files here; 这听起来很可怕,就像您要在此处重新创建
.bashrc
/ .bash_profile
/ .bash_login
文件一样; you might want to look at those instead of trying to simulate them, badly. 您可能想要看那些而不是尝试模拟它们,这很糟糕。
You can use the source
command: 您可以使用
source
命令:
source "init.sh"
This will run the commands in the referenced script ("init.sh") as if its contents were in this script. 这将在引用的脚本(“ init.sh”)中运行命令,就像其内容在此脚本中一样。
. "init.sh"
is a short-hand. 是简写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.