最近忙于hadoop生态系统的自动化流程控制,使用shell脚本完成了自动化部署修复的脚本,还有R等乱七八糟的东西。期间遇到了些问题,最经典的莫过于ssh远程执行命令的时候,出现环境变量找不到的问题,找到了很多资料最后整理如下:
参考资料:centos man bash其中的INVOCATION
A login shell is one whose first character of argument zero is a -, or one started with the --login option.
An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to termi-
nals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a
startup file to test this state.
The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are
expanded in file names as described below under Tilde Expansion in the EXPANSION section.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the
file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads
and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this
behavior.
When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be
inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if
it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the
POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and
execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an
interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a
file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option
has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters
posix mode after the startup files are read.
When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for startup files. In this mode, interactive
shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value. No other startup files are read.
Bash attempts to determine when it is being run by the remote shell daemon, usually rshd. If bash determines it is being run by rshd, it reads and exe-
cutes commands from ~/.bashrc, if that file exists and is readable. It will not do this if invoked as sh. The --norc option may be used to inhibit
this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or
allow them to be specified.
If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files
are read, shell functions are not inherited from the environment, the SHELLOPTS variable, if it appears in the environment, is ignored, and the effec-
tive user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not
reset.
谈到ssh远程执行命令,其环境变量参数等等,最终引起问题的根源还在shell,其区别如下
在脚本的调用方面(interactive、login相关),bash与sh也是存在差异
以下是详细说明(假如被调用执行的脚本名字叫xxx.sh)
BASH: 1、交互式的登录shell (bash –il xxx.sh) 载入的信息: /etc/profile ~/.bash_profile( -> ~/.bashrc -> /etc/bashrc) ~/.bash_login ~/.profile 2、非交互式的登录shell (bash –l xxx.sh) 载入的信息: /etc/profile ~/.bash_profile ( -> ~/.bashrc -> /etc/bashrc) ~/.bash_login ~/.profile $BASH_ENV 3、交互式的非登录shell (bash –i xxx.sh) 载入的信息: ~/.bashrc ( -> /etc/bashrc) 4、非交互式的非登录shell (bash xxx.sh) 载入的信息: $BASH_ENV SH: 1、交互式的登录shell 载入的信息: /etc/profile ~/.profile 2、非交互式的登录shell 载入的信息: /etc/profile ~/.profile 3、交互式的非登录shell 载入的信息: $ENV 4、非交互式的非登录shell 载入的信息: nothing |
由此可以看出,最主要的区别在于相关配置文件的是否载入,
而这些配置的是否载入,也就导致了很多默认选项的差异
(具体请仔细查看~/.bash_profile 等文件)
如:
[wangweiyu@ComSeOp ~]$ grep ulimit /etc/profile ulimit -S -c unlimited > /dev/null 2>&1 |
即,如果/etc/profile没有被载入,则不会产生core dump
非常值得一提的是,使用ssh远程执行命令,
远端sshd进程通过“bash –c”的方式来执行命令(即“非交互式的非登录shell”)
所以这一点,和登录之后再在本地执行执行命令,就存在了一定的差异
如:
[wangweiyu@ComSeOp ~]$ ssh wangweiyu@127.0.0.1 'echo $-' wangweiyu@127.0.0.1's password: hBc [wangweiyu@ComSeOp ~]$ echo $- himBH [wangweiyu@ComSeOp ~]$ ssh wangweiyu@127.0.0.1 'echo $0' wangweiyu@127.0.0.1's password: bash [wangweiyu@ComSeOp ~]$ echo $0 -bash |
注:
“$-” 中含有“i”代表“交互式shell”
“$0”的显示结果为“-bash”,bash前面多个“-”,代表“登录shell”
没有“i“和“-”的,是“非交互式的非登录shell”
另外还有一点,虽然ssh远程执行的命令是“非交互式的非登录shell”,
但在执行命令之前,ssh的那一次登录本身是“交互式的登录shell”,所以其会先载入“~/.bash_profile”
如:
[wangweiyu@ComSeOp ~]$ cat .bashrc # .bashrc # User specific aliases and functions # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi echo 'xxx' # 随便输出一些字符 [wangweiyu@ComSeOp ~]$ ssh wangweiyu@127.0.0.1 'echo $-' wangweiyu@127.0.0.1's password: xxx # .bashrc 被执行 hBc |
这一点,衍生出一个关于scp的问题,scp在传输数据之前,会先进行一次ssh登录,
而当.bashrc文件有输出的时候,则会导致scp失败!原因是解析返回的数据包出现混乱
如:
[wangweiyu@ComSeOp ~]$ cat .bashrc # .bashrc # User specific aliases and functions # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi echo 'xxx' # 随便输出一些字符 [wangweiyu@ComSeOp ~]$ scp file wangweiyu@127.0.0.1:/tmp wangweiyu@127.0.0.1's password: xxx # 输出xxx,随后scp失败 [wangweiyu@ComSeOp ~]$ echo $? 1 [wangweiyu@ComSeOp ~]$ ls /tmp/ [wangweiyu@ComSeOp ~]$ |