求教一个蛋痛的shell script的问题
- IsoaSFlus
- 帖子: 366
- 注册时间: 2012-11-13 11:30
- 系统: ubuntu17.10,Arch
- 联系:
求教一个蛋痛的shell script的问题
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
我想写一个批量把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
- cjxgm
- 帖子: 1952
- 注册时间: 2010-04-23 20:40
- 系统: Arch Linux
- 来自: 浙江·杭州
- 联系:
Re: 求教一个蛋痛的shell script的问题
[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=$'\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>''.
- IsoaSFlus
- 帖子: 366
- 注册时间: 2012-11-13 11:30
- 系统: ubuntu17.10,Arch
- 联系:
Re: 求教一个蛋痛的shell script的问题
不行的说,这样有需要空格特殊意义的地方空格就失效了。。。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
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 求教一个蛋痛的shell script的问题
代码: 全选
find -name "*.wav" | while read name
do
ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- IsoaSFlus
- 帖子: 366
- 注册时间: 2012-11-13 11:30
- 系统: ubuntu17.10,Arch
- 联系:
Re: 求教一个蛋痛的shell script的问题
不行。。。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
- IsoaSFlus
- 帖子: 366
- 注册时间: 2012-11-13 11:30
- 系统: ubuntu17.10,Arch
- 联系:
Re: 求教一个蛋痛的shell script的问题
试过,但用正则表达式替换上去的字符全是转义了的。。。eexpress 写了:都带“”嘛
Sent from my Galaxy S2 using Tapatalk 2
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
Re: 求教一个蛋痛的shell script的问题
你这2句里面,哪里有正则哦。那只是shell的内置替换。你使用\' \' 包括吧。
● name="xxe yy.avi"; echo \'${name//.avi/.flac}\'
'xxe yy.flac'
● name="xxe yy.avi"; echo \'${name//.avi/.flac}\'
'xxe yy.flac'
● 鸣学
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 求教一个蛋痛的shell script的问题
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
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 求教一个蛋痛的shell script的问题
如果用 zsh,还可以不用 find:
代码: 全选
for name in **/*.wav; do
ffmpeg -i "$name" -f flac "${name//.wav/.flac}"
done
- IsoaSFlus
- 帖子: 366
- 注册时间: 2012-11-13 11:30
- 系统: ubuntu17.10,Arch
- 联系:
Re: 求教一个蛋痛的shell script的问题
十分感谢,成功了!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