我需要在ssh上运行一个多行bash命令,所有可能的尝试都用尽了,但没有运气--
echo "3. All files found, creating remote directory on the server."
ssh -t $id@$host bash -c "'
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp/*
else
mkdir ~/_tmp
fi
'" ;
echo "4. Sending files ..."
scp ${files[@]} $id@$host:~/_tmp/ ; 这是输出--
user@linux:/tmp$ ./remotecompile
1. Please enter your id:
user
2. Please enter the names of the files that you want to compile
(Filenames *must* be space separated):
test.txt
3. All files found, creating remote directory on the server.
Password:
Unmatched '.
Unmatched '.
Connection to host.domain.com closed.请注意,我不想把每2-3行bash,如果-否则-fi命令到不同的文件。
正确的方法是什么?
发布于 2014-10-30 23:44:03
使用转义的本地文档来传递其文字内容。(没有逃跑).仅使用<<EOF,shell展开就会在本地处理--如果在远程运行的代码中使用变量,就会出现更有趣的角落情况)。
ssh "$id@$host" bash <<'EOF'
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp/*
else
mkdir ~/_tmp
fi
EOF如果您想传递参数,以一种明确正确的方式这样做会变得更有趣(因为涉及两个单独的shell解析层),但是printf '%q'内置可以节省时间:
args=( "this is" "an array" "of things to pass" \
"this next one is a literal asterisk" '*' )
printf -v args_str '%q ' "${args[@]}"
ssh "$id@$host" bash -s "$args_str" <<'EOF'
echo "Demonstrating local argument processing:"
printf '%q\n' "$@"
echo "The asterisk is $5"
EOF发布于 2014-10-30 23:47:01
这对我来说很管用:
ssh [hostname] '
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp
else
mkdir ~/_tmp
fi
'https://stackoverflow.com/questions/26665307
复制相似问题