这是一个bash脚本,它是
audacious -p
- 这应该播放列表。第三步是脚本失败的地方。这是脚本:
#!/bin/bash
find $1 -name '* *' | while read filename; do
Type=`file -i "$filename" -F "::" | sed 's/.*:: //' | sed 's/\/.*$//'`
if [ $Type=audio ]; then
List="$List '$filename'"
fi
done
audacious2 -p $List &
所以问题是:我如何转换
file name 1
file name 2
file name 3
到
'file name 1' 'file name 2' 'file name 3'
在bash?
答案 0 :(得分:2)
答案 1 :(得分:1)
#!/bin/sh
#find "$1" -name '* *' | # Edited as per OP's request
find . -type f -name '* *' |
while read -r filename; do
case `file -i "$filename" -F "::"` in
*::" audio"/*) echo "$filename" | tr '\012' '\000' ;;
esac
done |
xargs -0 audacious2 -p &
这里的要点是使用xargs
将一个文件名列表提供给一个命令,但我希望你也会欣赏条件匹配模式现在更加优雅;绝对学会使用case
。 (我希望我得到file
的输出正确。)
修改已更新以使用find -type f
,read -r
,tr '\012' '\000'
,xargs -0
。通过使用零字节作为终止符,xargs
可接受文件名中的空格和换行符。
答案 2 :(得分:0)
我设法用
做到了#!/bin/bash
# enques all audio files in the dir and its child dirs to audacious.
# the following finds all audio files in the dir (and its child dirs)
find "`pwd`" -type f | while read filename; do
Type=`file -i "$filename" -F "::" | sed 's/.*:: //' | sed 's/\/.*$//'`
if [ $Type=audio ]; then
# this enqueues them to audacious
audacious2 -e "$filename" &
fi
done
# and this line presses "play"
audacious2 -p &
工作正常。
修改强>:
问题也以“原始”方式解决(即将所有歌曲作为参数放入播放器,如:audacious2 -p "song 1" "song 2"
)。感谢Ignacio的链接,现在它可以工作:
#!/bin/bash
# enques all audio files in the dir and its child dirs to audacious.
# the following finds all audio files in the dir (and its child dirs)
find "`pwd`" -type f | {
while read filename; do
Type=`file -i "$filename" -F "::" | sed 's/.*:: //' | sed 's/\/.*$//'`
if [ $Type=audio ]; then
List="$List \"$filename\""
fi
done
echo $List | xargs audacious2 -p &
}
@tripleee:
没有xargs,它可以按你的方式工作:
#!/bin/bash
# enques all audio files in the dir and its child dirs to audacious.
find . |
while read filename; do
case `file -i "$filename" -F "::"` in
*::" audio"/*) audacious2 -e "$filename" & ;; # echo "$filename";;
esac
done