简体   繁体   中英

Linux .profile overrides other bash commands

I am loading a script (whiptail) when the root user logs into their Linux server, which works fine. 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.

This is what ~/.profile looks like:

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 . 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
  2. There are background scripts that run via crontab such as a check in script, job script, etc.

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; this includes running the user's .profile .

If you don't want that behavior, don't pass -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 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:

# 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...'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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