首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash:播放当前目录中的所有音频

Bash:播放当前目录中的所有音频
EN

Stack Overflow用户
提问于 2011-08-18 07:57:11
回答 3查看 449关注 0票数 1

这是一个bash脚本,它

  1. 获取当前dir中的所有文件,然后
  2. 获取其中的所有音频文件(允许文件名有空格)。
  3. 将列表发送到audacious -p --后者应该播放该列表。

第三步是脚本失败的地方。下面是脚本:

代码语言:javascript
复制
#!/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 &

所以问题是:我如何转换

代码语言:javascript
复制
file name 1
file name 2
file name 3

代码语言:javascript
复制
'file name 1' 'file name 2' 'file name 3'

在巴什?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-13 19:25:51

代码语言:javascript
复制
#!/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 fread -rtr '\012' '\000'xargs -0。通过使用零字节作为终止符,xargs可以接受文件名中的空白和换行符。

票数 1
EN

Stack Overflow用户

发布于 2011-08-18 07:59:27

BASH条目#50:“我试图在变量中放置一个命令,但是复杂的情况总是失败的!”

票数 2
EN

Stack Overflow用户

发布于 2011-08-22 18:53:06

我已经设法做到了

代码语言:javascript
复制
#!/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的链接,现在它起作用了:

代码语言:javascript
复制
#!/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的情况下按您的方式工作:

代码语言:javascript
复制
#!/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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7104138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档