在 Ubuntu(或任何 unix 类型的环境)中执行 shell 脚本时,如何在脚本之外保留导出?
我是 Linux 新手,尤其是 Ubuntu 11,我今天才第一次尝试。我需要 Linux 进行一些需要基于 Linux 的模拟器的开发,因此我尝试编写一个 shell 脚本来设置我的开发环境。
现在,我在我的主目录中创建了一个 .scripts 文件夹,并通过将其导出到 .bashrc 中将其添加到我的路径中,因此每次启动新的终端实例时,我都会可以执行我放入其中的任何自定义脚本。
现在,其中一个(实际上是三个)脚本设置了我所有与开发相关的路径、导出以及切换到该开发的适当文件夹的 cd 命令。但是(如果您已经知道这一点,请再次原谅我......)脚本在自己的“会话”中运行(因为缺乏更好的词),因此尽管环境变量等都已设置并执行(正如所证明的那样)通过在整个过程中嵌入 echo
调用)当脚本完成并且我返回到执行脚本的终端时,其他会话不再存在,并且除了清除屏幕和回显输出之外,没有其他任何东西显示脚本曾经运行过。
现在我不确定是否可以将该脚本之外的导出变量扩展回调用“实例”,或者是否可以设置某种标志来在现有会话中执行脚本,所以我很困惑。
现在,如果这是不可能的,是否至少可以编写一个脚本或设置一个可以启动新终端窗口的图标,然后执行该脚本但保持窗口打开并初始化?
谢谢!
标记
I'm new to Linux and especially to Ubuntu 11 which I'm just trying today for the first time. I need Linux for some development which requires a Linux-based emulator, so I'm trying to write a shell script that sets up my dev environment.
Now I've created a .scripts
folder in my home dir and added it to my path by exporting it in .bashrc
so every time I start a new terminal instance, I can execute any custom scripts I drop in there.
Now one (three actually) of those scripts sets up all my dev-related paths, exports, as well as a cd
command which switches to the appropriate folder for this dev. However (again forgive me if you already know this...) the script runs in its own 'session' (for lack of a better word) so although the enviro-vars and such are all set up and do execute (as was proven by embedding echo
calls throughout) when the script finishes and I'm returned back to the terminal where I executed the script, that other session no longer exists and with the exception of clearing the screen and echoing output, there's nothing else showing the script ever ran.
Now I'm not sure its even possible to extend exported variables outside of that script back to the calling 'instance' or of there's some kind of flag I can set to execute the script in the existing session, so I'm stumped.
Now if that is not possible, is it at least possible to write a script or set up an icon that can launch a new terminal window, then execute the script but leaving the window open and initialized?
Thanks!
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将脚本放入
~/.bashrc
中的函数定义中。例如Put the script in a function definition in
~/.bashrc
. For example使用源运行命令。
源代码 foo.sh
Run the command with source.
source foo.sh