小编And*_*ita的帖子

转义bash变量中的特殊字符

我正在尝试逐行读取包含文件路径的文件并将文件scp到另一台服务器,但由于文件名中的某些字符如'(',')','&'等,我需要转义输入:

input.txt:

/folder1/folderA/filename+(oh+how+nice,+parantheses)
Run Code Online (Sandbox Code Playgroud)

script.sh:

#!/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"
Run Code Online (Sandbox Code Playgroud)

输出:

-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: …
Run Code Online (Sandbox Code Playgroud)

bash scp escaping special-characters

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

escaping ×1

scp ×1

special-characters ×1