小编kal*_*gne的帖子

Ssh X11 转发与默认 shell 混淆

bash我的 bash 配置文件中有一个特定的代码:

$ cat ~/.bash_profile
#!/usr/bin/env bash

echo "SHELL: $SHELL"
function printfiles() {
  while IFS='' read -r _file || [[ -n "$_file" ]]; do
    echo "file: ${_file}"
  done < <(ls)
} && export -f printfiles
Run Code Online (Sandbox Code Playgroud)

< <(FUNCTION)bash特定的语法,不支持的sh。当我使用 ssh 登录到远程机器时:

$ ssh my.remote
me@xxx.xx.xxx.xx's password: 
Last login: Fri Nov 17 11:27:39 2017 from yyy.yy.yy.yy
sourcing /home/me/.bash_profile
SHELL: /bin/bash
Run Code Online (Sandbox Code Playgroud)

它工作正常。现在我想再次登录但转发我的X11

$ ssh -X my.remote
me@xxx.xx.xxx.xx's password: 
sh: printfiles: line 2: syntax error …
Run Code Online (Sandbox Code Playgroud)

ssh bash shell sh x11-forwarding

5
推荐指数
1
解决办法
195
查看次数

在 bash 脚本中调用源文件的函数

我有一个 bash 文件,src-useful.bash包含有用的函数,例如say_hello(),位于 /path/to/useful 中。

在我的中~/.bash_profile我添加了以下几行:

export BASH_USEFUL=/path/to/useful
source $BASH_USEFUL/src-useful.bash
Run Code Online (Sandbox Code Playgroud)

打开新终端我可以检查以下内容:

$ echo $BASH_USEFUL
/path/to/useful

$ cat $BASH_USEFUL/src-useful.bash
function hello() {
    echo "hello!"
}

$ hello
hello!
Run Code Online (Sandbox Code Playgroud)

我创建了一个脚本say_hello.sh

$ cat say_hello.sh
echo "BASH_USEFUL: $BASH_USEFUL"
hello

$ ./say_hello.sh
BASH_USEFUL: /path/to/useful  # Recognizsed
say_hello.sh: line 2: say_hello: command not found  # Not recognized?
Run Code Online (Sandbox Code Playgroud)

如果我来源$BASH_USEFUL/src-useful.bashsay_hello.sh会工作但是:

$ cat say_hello.sh
echo "BASH_USEFUL: $BASH_USEFUL"
source $BASH_USEFUL/src-useful
say_hello

$ ./say_hello.sh
BASH_USEFUL: /path/to/useful  # Recognized
hello! …
Run Code Online (Sandbox Code Playgroud)

export environment-variables bash-scripting

4
推荐指数
1
解决办法
2561
查看次数