Shell脚本:在命令

时间:2015-11-22 03:38:33

标签: shell ubuntu-14.04

我正在编写一个脚本,通过ssh从我的服务器到我的本地计算机上获取电影(mp4和mkv文件),但我收到错误的文件夹在完整路径上有空格。该脚本如下所示(尚未实施!)

#!/bin/bash

cmd=(`find /home/pi/Transmission_Downloads/ -type f | grep .mp4`)

for i in "${cmd[@]}"; do
    echo $i
done

我得到的输出就是这个

/home/pi/Transmission_Downloads/Nightwish.Rock.in.Rio.2015.720p.WEBRip-NRD.mp4
/home/pi/Transmission_Downloads/Limitless.S01E09.HDTV.x264-LOL[ettv]/limitless.109.hdtv-lol[ettv].mp4
/home/pi/Transmission_Downloads/The.Last.Man.On.Earth.S02E07.HDTV.x264-KILLERS[ettv]/The.Last.Man.On.Earth.S02E07.HDTV.x264-KILLERS[ettv].mp4
/home/pi/Transmission_Downloads/Mom.S03E03.HDTV.x264-LOL[ettv]/mom.303.hdtv-lol[ettv].mp4
/home/pi/Transmission_Downloads/Family.Guy.S14E06.HDTV.x264-KILLERS[ettv]/Family.Guy.S14E06.HDTV.x264-KILLERS[ettv].mp4
/home/pi/Transmission_Downloads/Ant-Man.2015.720p.WEB-DL.DD5.1.H264-RARBG/RARBG.com.mp4
/home/pi/Transmission_Downloads/Heroes.Reborn.S01E10.HDTV.x264-FUM[ettv]/Heroes.Reborn.S01E10.INTERNAL.HDTV.x264-FUM[ettv].mp4
/home/pi/Transmission_Downloads/The.Middle.S07E08.HDTV.x264-LOL[ettv]/the.middle.708.hdtv-lol[ettv].mp4
/home/pi/Transmission_Downloads/Fargo.S02E06.INTERNAL.HDTV.x264-BATV[ettv]/Fargo.S02E06.INTERNAL.HDTV.x264-BATV[ettv].mp4
/home/pi/Transmission_Downloads/Family.Guy.S14E05.HDTV.x264-FLEET[rarbg]/Family.Guy.S14E05.HDTV.x264-FLEET.mp4
/home/pi/Transmission_Downloads/The
Man
in
the
High
Castle
S01E02
Sunrise
x264
poke/The_Man_in_the_High_Castle_S01E02_Sunrise_x264_poke.mp4
/home/pi/Transmission_Downloads/The.Walking.Dead.S06E06.HDTV.x264-KILLERS[ettv]/The.Walking.Dead.S06E06.HDTV.x264-KILLERS[ettv].mp4

如何防止这些换行?

1 个答案:

答案 0 :(得分:1)

在脚本解决问题之前和之后添加这些行吗?

BKPIFS=$IFS
IFS='
' # note this is a newline character after the first quote so IFS is set to a newline
... your little script
IFS=$BKPIFS

如果是这样,问题是IFS(内部字段分隔符)默认包含空格,导致包含它的流沿着空白边界分割。因此,在备份之后明确地将IFS设置为换行符(BKPIFS),解决与空白的冲突