这是一个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'在巴什?
发布于 2011-09-13 19:25:51
#!/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可以接受文件名中的空白和换行符。
发布于 2011-08-18 07:59:27
BASH条目#50:“我试图在变量中放置一个命令,但是复杂的情况总是失败的!”
发布于 2011-08-22 18:53:06
我已经设法做到了
#!/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
donehttps://stackoverflow.com/questions/7104138
复制相似问题