MacOSX 如何在 su - root 后自动运行脚本? [英] MacOSX how to autorun a script after su - root?

查看:66
本文介绍了MacOSX 如何在 su - root 后自动运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我是普通用户,我将切换到 root:

Lets assume i am normal user, the i will switch to root:

user ~ $ su - root
Password: 
root ~ #

因此,一旦我以 root 身份登录,我希望 自动运行以下命令:

So once i logged in as root, i want to run following command automatically:

source .bash_profile

我怎样才能让上面的命令自动运行?

How can i have that above command run automatically please?

推荐答案

根据 bash 手册页,.bash_profile 是针对登录 shell 执行的,而 .bashrc 是为交互式非登录 shell 执行.

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

在您的情况下,您不需要像这样获取 .bash_profile .

In your case, you don't need to source .bash_profile like this.

您只需要将 source .bash_profile 放在根目录的 .bashrc 文件中

You just need to put source .bash_profile in your root's .bashrc file

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile
fi

阅读me以更好地理解.bash_profile.bashrc

Read me for better understanding of .bash_profile and .bashrc

更新

示例:

[root@sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root@sgeorge-ld ~]# exit
logout
[sgeorge@sgeorge-ld ~]$ su - root
Password: 
Testing .bashrc for a stack query
[root@sgeorge-ld ~]# 

这篇关于MacOSX 如何在 su - root 后自动运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆