转义bash变量中的特殊字符
我试图逐行读取包含文件路径的文件,并将文件scp到另一台服务器,但由于文件名中的某些字符,如“(”、“)”、“&”等,我需要转义输入: input.txt:转义bash变量中的特殊字符,bash,escaping,special-characters,scp,Bash,Escaping,Special Characters,Scp,我试图逐行读取包含文件路径的文件,并将文件scp到另一台服务器,但由于文件名中的某些字符,如“(”、“)”、“&”等,我需要转义输入: input.txt: /folder1/folderA/filename+(oh+how+nice,+parantheses) #!/bin/sh promote_to=random.server.com dev_catalog=/folderX/ uat_catalog=/folderY/ while read line do uat_path=$(ec
/folder1/folderA/filename+(oh+how+nice,+parantheses)
#!/bin/sh
promote_to=random.server.com
dev_catalog=/folderX/
uat_catalog=/folderY/
while read line
do
uat_path=$(echo "$uat_catalog$line" | sed -e "s/(/\\\(/g" | sed -e "s/)/\\\)/g")
dev_path=$(echo "$dev_catalog$line" | sed -e "s/(/\\\(/g" | sed -e "s/)/\\\)/g")
scp $dev_path user@$promote_to:$uat_path
scp $dev_path".atr" user@$promote_to:$uat_path".atr"
done < "input.txt"
-bash: /folder1/folderA/filename+(oh+how+nice,+parantheses): No such file or directory
-bash: /folder1/folderA/filename+(oh+how+nice,+parantheses): No such file or directory
usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 [...] [[user@]host2:]file2
ssh: random.server.com: Name or service not known
lost connection
script.sh:
/folder1/folderA/filename+(oh+how+nice,+parantheses)
#!/bin/sh
promote_to=random.server.com
dev_catalog=/folderX/
uat_catalog=/folderY/
while read line
do
uat_path=$(echo "$uat_catalog$line" | sed -e "s/(/\\\(/g" | sed -e "s/)/\\\)/g")
dev_path=$(echo "$dev_catalog$line" | sed -e "s/(/\\\(/g" | sed -e "s/)/\\\)/g")
scp $dev_path user@$promote_to:$uat_path
scp $dev_path".atr" user@$promote_to:$uat_path".atr"
done < "input.txt"
-bash: /folder1/folderA/filename+(oh+how+nice,+parantheses): No such file or directory
-bash: /folder1/folderA/filename+(oh+how+nice,+parantheses): No such file or directory
usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 [...] [[user@]host2:]file2
ssh: random.server.com: Name or service not known
lost connection
非常感谢您提供的任何帮助。您正在尝试将
/folder1/folderA/filename+(oh+how+nice,+parantises)
作为命令执行。你可能想做echo/folder1/folderA/filename+(哦+多好,+偏执)|……
编辑:@Ignacio所说的。这里的部分问题是本地和远程文件名的解析方式不同:本地文件名直接使用,所以您只需将其括在双引号中(如@Ignacio的回答中),但是远程文件名被传递到一个远程shell,该shell通过另一层解析(引号和转义删除等)运行它。因此,您只想将转义添加到远程路径。我还随意简化了
sed
命令:
#!/bin/sh
promote_to=random.server.com
dev_catalog=/folderX/
uat_catalog=/folderY/
while read line
do
uat_path="$(echo "$uat_catalog$line" | sed -e 's/[()&]/\\&/g')"
dev_path="$dev_catalog$line"
scp "$dev_path" "user@$promote_to:$uat_path"
scp "$dev_path.atr" "user@$promote_to:$uat_path.atr"
done < "input.txt"
#/垃圾箱/垃圾箱
将_升级到=random.server.com
dev_catalog=/folderX/
uat_目录=/folderY/
读行时
做
uat_path=“$(echo“$uat_目录$line”| sed-e的/[()&]/\\\&/g”)”
dev_path=“$dev_目录$line”
scp“$dev_path”用户@$promote_to:$uat_path”
scp“$dev_path.atr”“用户@$promote_to:$uat_path.atr”
完成<“input.txt”
注意,我使用的
sed
模式,'s/[()&]/\\&/g'
,仅转义括号和符号;如果文件名包含任何其他shell元字符,请确保将它们添加到[]
中的字符列表中,没错。。。错过了回音,但仍然不起作用:“/folder1/folderA/filename+\(哦+多么好,+parantheses\):没有这样的文件或目录”从每个sed中删除一个反斜杠会抛出这样的结果:“bash:-c:line 0:unexpected token附近的语法错误('bash:-c:line 0:
scp-t bash:-c:line 0:unexpected token附近的语法错误('bash:-c:line 0:
scp-t/folder1/folderA/filename+(哦+多好,+paranethes)'lost connection'非常感谢您提供的详细答案!对于那些想知道如何避开单引号的人,请参阅此处: