我試圖讀取一個文件包含文件路徑逐行並scp文件到另一臺服務器,但由於文件名中的某些字符像'(',')','&'等我需要逃避輸入:轉義bash變量中的特殊字符
input.txt中:
/folder1/folderA/filename+(oh+how+nice,+parantheses)
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 [email protected]$promote_to:$uat_path
scp $dev_path".atr" [email protected]$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]
[[[email protected]]host1:]file1 [...] [[[email protected]]host2:]file2
ssh: random.server.com: Name or service not known
lost connection
任何形式的幫助表示讚賞。
非常感謝您的詳細解答! – 2012-02-29 08:02:04