0

我的脚本中有以下行

script_list=`ssh@hostip ls -A /directory 2>/dev/null` 

有没有办法在 if 条件下使用它,以便我可以分配 script_list 变量或在 else 条件下处理失败场景

提前致谢

4

2 回答 2

1

You can simply check the automatic variable $? in the next line:

script_list=$( ssh ... )
rc=$?
if [[ $rc -ne 0 ]]; then
    ...something is wrong...
fi

This works because the exit code of ssh is the exit code of the command it ran remotely if ssh itself could be executed successfully. But usually, you don't care which part of the command chain failed, it's good enough to know that any part (the local ssh or the remote command failed).

于 2013-03-28T19:28:47.933 回答
0

没问题,做就行了。分配作为命令非常好(命令是指可以在 之后的东西if)。

if asdf=$(echo test1; exit 1); then
    echo "SUCCESS1: $asdf"
fi
if asdf=$(echo test0; exit 0); then
    echo "SUCCESS0: $asdf"
fi
于 2013-03-28T19:32:22.083 回答