Bash 脚本本身会生成 bash 命令

Bash 脚本本身会生成 bash 命令

如果在 Audacious 播放歌曲时输入此命令:

songdir=$(echo -n $(audtool current-song-filename) | cut -d'/' -f-5);for entry in "$songdir"/*.mp3; do echo "$entry";done

该命令通过完整路径和文件名生成该专辑中所有歌曲的列表:

/media/sdc2/stereophonics/graffiti_on_the_train/01_-_stereophonics_-_we_share_the_same_sun.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/02_-_stereophonics_-_graffiti_on_the_train.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/03_-_stereophonics_-_indian_summer.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/04_-_stereophonics_-_take_me.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/05_-_stereophonics_-_catacomb.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/06_-_stereophonics_-_roll_the_dice.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/07_-_stereophonics_-_violins_and_tambourines.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/08_-_stereophonics_-_been_caught_cheating.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/09_-_stereophonics_-_in_a_moment.mp3
/media/sdc2/stereophonics/graffiti_on_the_train/10_-_stereophonics_-_no-ones_perfect.mp3

该命令(将更新每个标签)本身必须分为三部分,然后才能用于执行上面列出的每个文件。(我们假设变量名称$song_path_var用于上述列表的每次迭代)。

以下是三个部分:

命令的第 1 部分可以是文本文件 (pt1newcomm.txt) 或包含命令第一部分的变量:

id3ted --COMM "

第 2 部分是通过在 X 中选择文本并将其复制到剪贴板,然后运行以下命令将剪贴板内容保存到第 2 部分文本文件 (pt2newcomm.txt) 或变量来构建的。这可以通过 xclip 轻松完成:

xclip -out -selection clipboard > ~/data/mmdata/pt2newcomm.txt

上述命令的结果生成一个文本文件,其中包含要添加到每个标签的实际注释:

Stereophonics are a Welsh rock band that formed in 1992 in the village of Cwmaman in the Cynon Valley. The band consists of Kelly Jones (lead vocals and lead guitar), Richard Jones (bass guitar, piano and backing vocals), Adam Zindani (rhythm guitar and backing vocals), Jamie Morrison (drums) and touring member Tony Kirkham (keyboards). The group previously included Stuart Cable (1992–2003) and then Javier Weyler (2004–2012) on drums. Stereophonics have released ten studio albums, including six UK number one albums, their latest album being Scream Above the Sounds (2017). A successful compilation album, Decade in the Sun, was released in November 2008 and charted at number two in the United Kingdom.

该命令的第 3 部分可以是文本文件 (pt3newcomm.txt) 或包含该命令第三部分和最后一部分的变量:

:" $song_path_var

因此,当我们将所有三个部分结合起来时:

paste ~/data/mmdata/pt1newcomm.txt ~/data/mmdata/pt2newcomm.txt ~/data/mmdata/pt3newcomm.txt > ~/data/mmdata/pt4newcomm.txt

它会生成一个文本文件(pt4newcomm.txt)或变量,其中包含以下命令,然后需要在 bash 中执行:

id3ted -c " Stereophonics are a Welsh rock band that formed in 1992 in the village of Cwmaman in the Cynon Valley. The band consists of Kelly Jones (lead vocals and lead guitar), Richard Jones (bass guitar, piano and backing vocals), Adam Zindani (rhythm guitar and backing vocals), Jamie Morrison (drums) and touring member Tony Kirkham (keyboards). The group previously included Stuart Cable (1992–2003) and then Javier Weyler (2004–2012) on drums. Stereophonics have released ten studio albums, including six UK number one albums, their latest album being Scream Above the Sounds (2017). A successful compilation album, Decade in the Sun, was released in November 2008 and charted at number two in the United Kingdom.    :" $(audtool --current-song-filename)

上述命令正确地更新了给定 mp3 文件的 id3 标签的注释框架,但必须手动为目录中的每个文件执行此操作。如何将每个文件分配给变量$song_path_var,然后自动为所有文件运行这些命令?

我假设我们可以使用:for OUTPUT in $(bash line command)格式:

   #!/bin/bash
    #The comment from the clipboard need only be executed once, so it is run first before the loop begins
    xclip -out -selection clipboard > ~/data/mmdata/pt2newcomm.txt
    #Next is the for loop
    for song_path_var in $(songdir=$(echo -n $(audtool current-song-filename) | cut -d'/' -f-5);for entry in "$songdir"/*.mp3; do echo "$entry";done)
    do  
       paste ~/data/mmdata/pt1newcomm.txt ~/data/mmdata/pt2newcomm.txt $song_path_var > ~/data/mmdata/pt4newcomm.txt
    #command to execute pt4newcomm.txt here?
     done

这会生成 bash 命令行的输出,但实际上并不处理它们,那么如何让它们在同一个脚本中以命令模式运行呢?

更新:以下脚本生成一个包含每个命令行的文件:

#!/bin/bash
#
truncate -s 0 ~/data/mmdata/test*.txt
xclip -out -selection clipboard > ~/data/mmdata/pt2newcomm.txt
songdir=$(echo -n $(audtool current-song-filename) | cut -d'/' -f-5);for entry in "$songdir"/*.mp3; do echo "$entry">> ~/data/mmdata/test1.txt;done
sed -e 's/^/\:\" /' ~/data/mmdata/test1.txt > test2.txt
commtxt=$(cat ~/data/mmdata/pt2newcomm.txt);file=~/data/mmdata/test2.txt; while read -r line; do echo "${commtxt}$line"; done <$file > test3.txt
sed -e 's/^/id3ted --COMM \"/' ~/data/mmdata/test3.txt > test4.txt
$COMMAND `cat ~/data/mmdata/test4.txt`
$COMMAND

包含命令的文件如下所示:

id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/10_-_feeder_-_dove_grey_sands.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/1_-_feeder_-_feeling_a_moment.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/2_-_feeder_-_bitter_glass.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/3_-_feeder_-_tumble_and_fall.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/4_-_feeder_-_tender.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/5_-_feeder_-_pushing_the_senses.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/6_-_feeder_-_frequency.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/7_-_feeder_-_morning_life.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/8_-_feeder_-_pilgrim_soul.mp3
id3ted --COMM "Pushing the Senses is the fifth album by the British rock band Feeder. It was released on Echo, Liberation Music and PIAS on 31 January 2005:" /media/sdc2/feeder/pushing_the_senses/9_-_feeder_-_pain_on_pain.mp3

当在终端中手动输入每一行时,它可以工作,但是在脚本中运行时,它会失败,可能是由于命令字符串中的引号。

解决了:

将此脚本的末尾更改为在单独的 bash 实例中运行:

#!/bin/bash
#
truncate -s 0 ~/data/mmdata/test*.txt
xclip -out -selection clipboard > ~/data/mmdata/pt2newcomm.txt
songdir=$(echo -n $(audtool current-song-filename) | cut -d'/' -f-5);for entry in "$songdir"/*.mp3; do echo "$entry">> ~/data/mmdata/test1.txt;done
sed -e 's/^/\:\" /' ~/data/mmdata/test1.txt > test2.txt
commtxt=$(cat ~/data/mmdata/pt2newcomm.txt);file=~/data/mmdata/test2.txt; while read -r line; do echo "${commtxt}$line"; done <$file > test3.txt
sed -e 's/^/id3ted --COMM \"/' ~/data/mmdata/test3.txt > test4.txt
bash ~/data/mmdata/test4.txt

答案1

在这里执行pt4newcomm.txt的命令?

做这个:

$COMMAND=`cat pt4newcomm.txt`
$COMMAND

显然,您需要非常确定其中pt4newcomm.txt包含有效的命令,不会删除您不想要的文件等等。

可能有五种更好的方法可以做到这一点,但上述方法应该可行。

相关内容