在shell脚本中使用变量时找不到ssh

时间:2017-06-30 14:05:28

标签: linux shell

编写shell脚本时遇到奇怪的错误。

以下作品完美无缺......

#!/bin/sh
if ssh root@example.com "[ -d /web ]"; then 
    echo "That directory exists!";
fi

运行没有错误。一旦我尝试使用变量...

#!/bin/sh
USER="root"
LOC="example.com"
PATH="/web"

if ssh $USER@$LOC "[ -d $PATH ]"; then 
    echo "That directory exists!";
fi

它只是返回......

6: test.sh: ssh: not found

即使只是将变量设置在顶部并使底部硬编码,也会导致此错误。

1 个答案:

答案 0 :(得分:3)

本地shell正在使用

$PATH来查找二进制文件,在本例中为ssh。只要您将其设置为/web,shell就会尝试找到不存在的/web/ssh

使用其他变量名称:

remote_path="/web"