巴什 - 转义SSH命令 [英] Bash - Escaping SSH commands

查看:116
本文介绍了巴什 - 转义SSH命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组,我使用通过FTP下载文件,然后从服务器中删除脚本。

I have a set of scripts that I use to download files via FTP and then delete them from the server.

它的工作原理如下:

for dir in `ls /volume1/auto_downloads/sync-complete`
do
if [ "x$dir" != *"x"* ]
then
echo "DIR: $dir"

echo "Moving out of complete"
        # Soft delete from server so they don't get downloaded again
        ssh dan@172.19.1.15 mv -v "'/home/dan/Downloads/complete/$dir'" /home/dan/Downloads/downloaded

现在$ DIR可能是这是一个文件,它工作正常。

Now $dir could be "This is a file" which works fine.

我遇到的问题是有特殊字符,例如:

The problem I'm having is with special characters eg:


  • 这是(一)文件

  • 这是一个文件&安培;东西

往往错误:

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `mv -v '/home/dan/Downloads/complete/This is (a) file' /home/dan/Downloads/downloaded'

我不能工作,如何逃避它所以无论是变量被评估,并命令被正确转义。我试过的转义字符的各种组合,文字引号,正常报价,等等

I can't work out how to escape it so both the variable gets evaluated and the command gets escaped properly. I've tried various combinations of escape characters, literal quotes, normal quotes, etc

推荐答案

如果双方都使用bash,您可以使用逃脱参数的printf'%Q ,例如:

If both sides are using bash, you can escape the arguments using printf '%q ', eg:

ssh dan@172.19.1.15 $(printf '%q ' mv -v "/home/dan/Downloads/complete/$dir" /home/dan/Downloads/downloaded)

这篇关于巴什 - 转义SSH命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆