Shell脚本:正确解析空格

时间:2015-10-23 18:17:48

标签: linux shell ubuntu

在下面的代码中我期待这样的输出:

Transmission_Downloads/The.Last.Man.On.Earth.S02E04.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt
Transmission_Downloads/Fargo.S02E01.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt
Transmission_Downloads/Nightwish - Showtime Storytime (2013) 1080p 5.1/SEMEAR.txt

但我得到的是这个

Transmission_Downloads/The.Last.Man.On.Earth.S02E04.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt
Transmission_Downloads/Fargo.S02E01.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt
Transmission_Downloads/Nightwish
-
Showtime
Storytime
(2013)
1080p
5.1/SEMEAR.txt

我应该如何为shell脚本正确解析文件和文件夹名称上的空格?谢谢!

#!/bin/bash

#opcao -s nao mostra o texto na tela
echo "Digite sua senha"
read -s answer

root_folder="/home/pi/"
user="pi"
server="*****"
local_folder="/home/dellcloud2"

#Colocar aqui os paths dos arquivos que deseja puxar
array=(
    "Transmission_Downloads/The.Last.Man.On.Earth.S02E04.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt"
    "Transmission_Downloads/Fargo.S02E01.HDTV.x264-KILLERS[ettv]/Torrent-Downloaded-from-ExtraTorrent.cc.txt"
    "Transmission_Downloads/Nightwish - Showtime Storytime (2013) 1080p 5.1/SEMEAR.txt"
)
for i in ${array[@]}; do
    #sshpass -p $answer rsync -aP $user@$server:$root_folder$i $local_folder
    echo $i
done

1 个答案:

答案 0 :(得分:2)

您需要使用"${array[@]}"来保留空白区域。数组周围的引号加上通配符@一起启用此功能。

这也适用于位置参数,即"$@"为您提供了一个命令行参数数组,并在给出时正确引用。