您可以指定远程主机在本地使用的shell。
echo 'echo "Bash version: ${BASH_VERSION}"' | ssh -q localhost bash
并小心(单)引用您希望由远程主机扩展的变量;否则可变扩展将由您的本地shell完成!
# example for local/remote variable expansion
{
echo "[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'" |
ssh -q localhost bash
echo '[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive"' |
ssh -q localhost bash
}
因此,要检查是否有特定的文件在远程主机上存在,你可以做到以下几点:
host='localhost' # localhost as test case
file='~/.bash_history'
if `echo 'test -f '"${file}"' && exit 0 || exit 1' | ssh -q "${host}" sh`; then
#if `echo '[[ -f '"${file}"' ]] && exit 0 || exit 1' | ssh -q "${host}" bash`; then
echo exists
else
echo does not exist
fi
你试图执行该文件... –
一般情况下, 'then'前面需要一个分号:'if cmd;然后cmd;其他cmd; fi' –