简体   繁体   English

Linux .profile覆盖其他bash命令

[英]Linux .profile overrides other bash commands

I am loading a script (whiptail) when the root user logs into their Linux server, which works fine. 当root用户登录到他们的Linux服务器时,我正在加载脚本(whiptail),效果很好。 The thing is, now, when I attempt to run other scripts from the command prompt (or crontab) the initial script is loaded instead, and it looks like the script that I want to run is not. 问题是,现在,当我尝试从命令提示符(或crontab)运行其他脚本时,将加载初始脚本,而看起来好像我不想运行的脚本。

This is what ~/.profile looks like: 这是~/.profile样子:

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n
source /root/menu.sh

So, when I try to run bash -lc 'ruby some/other/script.rb I'm taken into the script that runs at the end of ~/.profile which is menu.sh . 所以,当我试图运行bash -lc 'ruby some/other/script.rb我考虑到运行在结束脚本~/.profilemenu.sh How can I keep this from happening? 我怎样才能避免这种情况发生?

Here's what I need to have happen in the long run: 从长远来看,这是我需要做的事情:

  1. The server boots up and takes the user to /root/menu.sh 服务器启动,将用户带到/root/menu.sh
  2. There are background scripts that run via crontab such as a check in script, job script, etc. 有一些通过crontab运行的后台脚本,例如签入脚本,作业脚本等。

Best-practices: Don't use a login shell unless you need one 最佳做法:除非您需要一个登录外壳,否则不要使用它

When you pass the -l argument to bash, you're telling it to behave as a login shell; 当将-l参数传递给bash时,是在告诉它充当登录外壳程序; this includes running the user's .profile . 这包括运行用户的.profile

If you don't want that behavior, don't pass -l . 如果您不想要这种行为,请不要传递-l Thus: 从而:

bash -c 'ruby some/other/script.rb'

That said, there's no advantage to doing that over just invoking ruby directly, without any enclosing shell: 就是说,这样做比直接调用ruby而不带任何外壳没有好处:

ruby some/other/script.rb

If you must use a login shell... 如果必须使用登录外壳...

If you want other effects of running the user's .profile , you might set an environment variable to indicate that you want to bypass this behavior: 如果您想要运行用户的.profile其他效果,则可以设置一个环境变量以指示您要绕过此行为:

# in the user's login scripts
[ -n "$skip_menu" ] || source /root/menu.sh

...and then... ...接着...

skip_menu=1 bash -lc '...your command here...'

...or, if being executed without an enclosing shell... ...或者,如果在没有封闭外壳的情况下执行...

env skip_menu=1 bash -lc '...your command here...'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM