您可以指定远程主机在本地使用的 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