制作一个bash脚本切换到交互模式,并给出提示 [英] Making a bash script switch to interactive mode and give a prompt
问题描述
我写一个训练工具,它是写在bash教的bash / UNIX。
I am writing a training tool, it is written in bash to teach bash/unix.
我想一个脚本运行到设定一些东西,然后用手控制权交给用户。
我希望它通过键入很容易可运行 ./脚本的名称
I want a script to run to set things up, then to hand control to the user.
I want it to be easily runnable by typing ./script-name
我如何做到这一点?
即
- 用户类型:
教程/运行
- 的运行教程脚本设置的事情了。
- 的用户是与任务psented $ P $。 (此位有效)
- 返回命令提示符,与外壳仍然配置。
目前,如果我键入它会工作。教程/ .bashrc的
推荐答案
有几种选择:
- 您在同一个shell启动脚本,使用
源
或 - 您开始一个新的外壳,但你的脚本作为初始化脚本:
首先是明显;我写的第二多一点点的细节。
The first is obvious; I write a little bit more details about the second.
对于这一点,您使用 - 初始化文件
选项:
For that, you use --init-file
option:
bash --init-file my-init-script
您甚至可以在家当行使用此选项:
You can even use this option in the shebang line:
#!/bin/bash --init-file
,然后你开始你的脚本一如既往:
And then you start you script as always:
./script-name
例如:
$ cat ./script-name
#!/bin/bash --init-file
echo Setting session up
PS1='.\$ '
A=10
$ ./script-name
Setting session up
.$ echo $A
10
.$ exit
$ echo $A
$
正如你所看到的,该脚本取得了用户的环境,然后给了他提示。
As you can see, the script has made the environment for the user and then has given him the prompt.
这篇关于制作一个bash脚本切换到交互模式,并给出提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!