微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每次调用/ bin / sh时都可以运行脚本吗?

使用bash,可以将〜/ .bashrc文件设置为在每次创建新的bash shell时运行.是否可以使用/ bin / sh做同样的事情? (顺便说一下,这是在Debian上的).

现在,我只想在调用/ bin / sh时回显“我是sh”.在bash中很容易做到(“ echo’I am bash’”位于文件顶部).

谢谢!

解决方法:

启动破折号的登录外壳(在类似debian的系统上为/ bin / sh),它将读取〜/ .profile.如果您还希望读取交互式非登录外壳程序的配置文件,请将以下行添加到〜/ .profile文件中:

ENV=$HOME/.shinit; export ENV

然后,在环境中出现变量ENV的情况下,文件$HOME / .shinit将随每个新的交互式(破折号)shell一起获得.

您可以将ENV指定的文件名更改为您喜欢的任何文件名.

为了确保破折号登录外壳已将ENV添加到环境中,您可能需要注销并重新登录,或者可能重新引导,具体取决于您的系统设置.

文献资料

这在man破折号中有记载:

A login shell first reads
commands from the files /etc/profile and .profile if they exist. If the environment variable ENV
is set on entry to an interactive shell, or is set in the .profile of a login shell, the shell
next reads commands from the file named in ENV. Therefore, a user should place commands that are
to be executed only at login time in the .profile file, and commands that are executed for every
interactive shell inside the ENV file.

假设我们设置了以下文件

$echo "ENV=$HOME/.shinit; export ENV" >>~/.profile
$cat .shinit
echo FOUND ME

由于我只是将ENV行添加到〜/ .profile文件中,因此ENV尚未在环境中.如果我们运行破折号:

$dash
$

什么也没发生,因为这是一个登录外壳,并且环境中还没有ENV.

如果我们启动登录外壳,则将ENV放入环境中并运行〜/ .shinit:

$dash -l
FOUND ME

如果作为该外壳程序的子级运行一个交互式非登录外壳程序,则将运行〜/ .shinit,因为父外壳程序创建了ENV变量:

$dash
FOUND ME

上面的登录shell创建的环境仅影响其子级.如上所述,要确保所有交互式仪表板的环境中都具有ENV,可能需要注销然后重新登录或重新启动.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐