为什么SSH远程命令在手动运行时获得较少的环境变量? [英] Why does an SSH remote command get fewer environment variables then when run manually?

查看:224
本文介绍了为什么SSH远程命令在手动运行时获得较少的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ssh user @ IP< command> 

使用两种方法比较不同环境中的env的输出。当我手动登录到机器并运行env,我得到更多的环境变量,然后当我运行:

  ssh user @ IP env

任何想法为什么?

解决方案

有不同类型的shell。 SSH命令执行shell是一个非交互式shell,而您的普通shell是登录shell或交互式shell。描述如下,从man bash:

 
一个登录shell是一个参数
零的第一个字符是一个 - 或一个以--login选项开头。

交互式shell是一个没有非选项
参数的开始,没有-c选项,其标准输入
和错误都连接到终端(如确定的
由isatty(3)),或者以-i选项启动。 PS1是
set,$ - 包括i如果bash是交互式的,允许
shell脚本或启动文件来测试此状态。

以下段落描述了bash如何执行其
启动文件。如果任何文件存在但不能
读取,bash会报错。波纹在文件
名称中扩展,如下所述,在
EXPANSION部分的波浪扩展下。

当bash被调用为交互式登录shell,或者
a非交互式shell使用--login选项时,首先
从文件/ etc中读取并执行命令/ profile,如果
该文件存在。在阅读该文件之后,它在
命令中查找
〜/ .bash_profile,〜/ .bash_login和〜/ .profile,并从第一个
读取并执行命令存在并且是可读的。 --noprofile选项可以
在shell启动时禁用此行为
ior。

当登录shell退出时,bash从文件〜/ .bash_logout读取并执行命令
(如果存在)。

当不是登录shell的交互式shell是
启动时,bash从〜/ .bashrc,
中读取并执行命令,如果该文件存在。使用
--norc选项可能会阻止这种情况。 --rcfile文件选项将强制bash
从文件而不是
〜/ .bashrc读取和执行命令。

当bash以非交互方式启动时,要运行shell
脚本,例如,它将在
环境中查找变量BASH_ENV,如果它出现在那里,则扩展其值,
并使用扩展值作为文件的名称读取
并执行。 Bash的行为就好像以下命令
被执行:
如果[-n$ BASH_ENV];然后 。 $ BASH_ENV; fi
,但PATH变量的值不用于搜索
的文件名。


I have a command that runs fine if I ssh to a machine and run it, but fails when I try to run it using a remote ssh command like :

ssh user@IP <command>

Comparing the output of "env" using both methods resutls in different environments. When I manually login to the machine and run env, I get much more environment variables then when I run :

ssh user@IP "env"

Any idea why ?

解决方案

There are different types of shells. The SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell. Description follows, from man bash:

       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 terminals  (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  behav­
       ior.

       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.

这篇关于为什么SSH远程命令在手动运行时获得较少的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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