分页: 1 / 1

求教一个蛋痛的shell script的问题

发表于 : 2013-02-24 17:03
IsoaSFlus
rt
我想写一个批量把wav转换成flac的shellscript,利用for,find,和ffmpeg。
主要代码如下:
for name in $(find -name "*.wav")
do
ffmpeg -i $name -f flac ${name//.wav/.flac}
done
但是蛋痛的问题出现了。。。文件名中有空格。。。就就导致有些路径不正确了,请问有什么办法能解决?
Sent from my JY-G2 using Tapatalk 2

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-24 17:17
cjxgm
[bash]
IFS=$'\n'
for name in $(find -name "*.wav")
do
ffmpeg -i $name -f flac ${name//.wav/.flac}
done
unset IFS
[/bash]

man bash

代码: 全选

       IFS    The  Internal  Field  Separator  that is used for word splitting
              after expansion and to split lines  into  words  with  the  read
              builtin  command.   The  default  value  is  ``<space><tab><new‐
              line>''.

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-24 19:35
IsoaSFlus
cjxgm 写了:[bash]
IFS=$'\n'
for name in $(find -name "*.wav")
do
ffmpeg -i $name -f flac ${name//.wav/.flac}
done
unset IFS
[/bash]

man bash

代码: 全选

       IFS    The  Internal  Field  Separator  that is used for word splitting
              after expansion and to split lines  into  words  with  the  read
              builtin  command.   The  default  value  is  ``<space><tab><new‐
              line>''.
不行的说,这样有需要空格特殊意义的地方空格就失效了。。。
Sent from my JY-G2 using Tapatalk 2

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-24 22:32
aerofox

代码: 全选

find -name "*.wav" | while read name
do
    ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-25 8:56
eexpress
都带“”嘛

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-25 12:40
IsoaSFlus
aerofox 写了:

代码: 全选

find -name "*.wav" | while read name
do
    ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done
不行。。。

Sent from my Galaxy S2 using Tapatalk 2

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-25 12:41
IsoaSFlus
eexpress 写了:都带“”嘛
试过,但用正则表达式替换上去的字符全是转义了的。。。

Sent from my Galaxy S2 using Tapatalk 2

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-25 15:50
eexpress
你这2句里面,哪里有正则哦。那只是shell的内置替换。你使用\' \' 包括吧。

● name="xxe yy.avi"; echo \'${name//.avi/.flac}\'
'xxe yy.flac'

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-26 6:28
aerofox
IsoaSFlus 写了:
aerofox 写了:

代码: 全选

find -name "*.wav" | while read name
do
    ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done
不行。。。

Sent from my Galaxy S2 using Tapatalk 2

代码: 全选

$ mkdir test
$ cd test
$ touch 'file 1.txt' 'file 2.txt'
$ ls
file 1.txt  file 2.txt
$ find -name '*.txt' | while read file; do cp "$file" "${file%.*}.TXT"; done
$ ls
file 1.txt  file 1.TXT  file 2.txt  file 2.TXT
为什么我这就行呢?

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-26 6:30
aerofox
如果用 zsh,还可以不用 find:

代码: 全选

for name in **/*.wav; do
    ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done

Re: 求教一个蛋痛的shell script的问题

发表于 : 2013-02-26 10:23
IsoaSFlus
aerofox 写了:
IsoaSFlus 写了:
aerofox 写了:

代码: 全选

find -name "*.wav" | while read name
do
    ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done
不行。。。

Sent from my Galaxy S2 using Tapatalk 2

代码: 全选

$ mkdir test
$ cd test
$ touch 'file 1.txt' 'file 2.txt'
$ ls
file 1.txt  file 2.txt
$ find -name '*.txt' | while read file; do cp "$file" "${file%.*}.TXT"; done
$ ls
file 1.txt  file 1.TXT  file 2.txt  file 2.TXT
为什么我这就行呢?
十分感谢,成功了!

Sent from my Galaxy S2 using Tapatalk 2