Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MacOSX如何在su-root之后自动运行脚本?_Macos_Root_.bash Profile - Fatal编程技术网

MacOSX如何在su-root之后自动运行脚本?

MacOSX如何在su-root之后自动运行脚本?,macos,root,.bash-profile,Macos,Root,.bash Profile,假设我是普通用户,我将切换到root: user ~ $ su - root Password: root ~ # 因此,一旦我以root身份登录,我希望自动运行以下命令: source .bash_profile 我怎样才能让上面的命令自动运行呢?首先,当你切换到root用户时,你仍然是你的普通用户的主目录。要执行哪个.bash_配置文件/Users/myuser/.bash\u配置文件还是root的/var/root/.bash\u配置文件 无论您想要执行什么,您都可以编辑/var/r

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

user ~ $ su - root
Password: 
root ~ #
因此,一旦我以
root
身份登录,我希望自动运行以下命令:

source .bash_profile

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

首先,当你切换到root用户时,你仍然是你的普通用户的主目录。要执行哪个.bash_配置文件/Users/myuser/.bash\u配置文件还是root的/var/root/.bash\u配置文件


无论您想要执行什么,您都可以编辑/var/root/.bashrc(如果您没有它,请创建一个)并在那里添加您的命令。

根据bash手册页,
对登录shell执行.bash\u profile
,而对交互式非登录shell执行
.bashrc

在您的情况下,您不需要像这样编写源代码
.bash\u profile

您只需要将
source.bash\u profile
放入根目录的
.bashrc
文件中

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile
fi
阅读以更好地理解
.bash\u配置文件
.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 ~]# 

它没有受到影响。我需要重新启动吗?不,你不需要重新启动任何东西。。你确定你有
~/.bash\u档案
?嗯。。所以您将这个
if
条件放在
/root/.bashrc
中。是吗?是的,当然。然后,我
su-
来自普通用户。当我成为
root
时,什么也没有发生。“终端窗口指南的一个例外是Mac OS X的terminal.app,它默认情况下为每个新的终端窗口运行一个登录shell,调用.bash_profile而不是.bashrc。其他GUI终端模拟器也可以这样做,但大多数都不这么做。”。因此,您可以尝试使用
.bash\u profile
而不是
.bashrc
嗨,现在我将
echo'hello'
添加到这两个文件中:
/var/root/.bash\u profile
/var/root/.bashrc
作为根。但是当我
su-root
再次到达里面时,什么也没发生!使用
su-root
,而不是
sudo-s
,顺便问一下,您使用的是哪个shell?root的默认shell是
/bin/sh
。我使用
/bin/bash
。为此,您可以编辑
/etc/passwd
文件。或者,如果您想继续使用/bin/sh而不是
/var/root/.bash_profile
,则需要
/var/root/.profile
否,OSX终端是一个终端仿真器。Shell是命令解释器。如果您键入
echo$SHELL
,您将看到您的SHELL是什么。也可以这样做:编辑
/var/root/.profile
,添加
echo“hello”
,然后使用
sudo-s
,而不是使用
su-root
。它会起作用的。