您可以指定遠程主機在本地使用的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' –