Script Bash que em si gera comandos bash

0

Se este comando for inserido enquanto uma música estiver sendo reproduzida no Audacious:

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

O comando produz uma lista de todas as músicas desse álbum por caminho e nome de arquivo completos:

/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

O comando (que atualizará cada tag) deve ser construído em três partes antes que possa ser usado para executar cada arquivo listado acima. (Vamos supor que o nome da variável $song_path_var seja usado para cada iteração da lista acima).

Aqui estão as três partes:

A parte 1 do comando pode ser um arquivo de texto (pt1newcomm.txt) ou uma variável com a primeira parte do comando:

id3ted --COMM "

A parte 2 é construída selecionando e copiando o texto para a área de transferência em X e executando o comando abaixo para salvar o conteúdo da área de transferência no arquivo de texto da Parte 2 (pt2newcomm.txt) ou variável. Isso pode ser feito facilmente com o xclip:

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

O resultado do comando acima produz um arquivo de texto com o comentário real a ser adicionado a cada tag:

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.

A parte 3 do comando pode ser um arquivo de texto (pt3newcomm.txt) ou variável com a terceira e última parte do comando:

:" $song_path_var

Então, quando combinamos as três partes com:

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

produz um arquivo de texto (pt4newcomm.txt) ou variável com o seguinte comando, que precisará ser executado no 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)

O comando acima atualiza corretamente o quadro de comentários da tag id3 para um determinado arquivo mp3, mas deve ser feito manualmente para cada arquivo no diretório. Como você atribui cada um à variável $song_path_var e, em seguida, executa esses comandos automaticamente para todos os arquivos?

Eu suponho que podemos usar um formato: 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

Isso gera uma saída das linhas de comandos bash, bit, isso não as processa, então como você as faz executar no modo de comando dentro deste mesmo script?

UPDATE: O script a seguir gera um arquivo contendo cada linha de comando:

#!/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

O arquivo com comandos é assim:

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

Ao inserir cada linha manualmente no terminal, ela funciona, mas quando executada no script, ela falha, provavelmente devido a aspas na seqüência de caracteres de comando.

RESOLVIDO:

Alterou o final deste script para ser executado em uma instância separada do 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
    
por LPC11 06.01.2018 / 18:21

1 resposta

0

command to execute pt4newcomm.txt here?

Faça isso:

$COMMAND='cat pt4newcomm.txt'
$COMMAND

Obviamente, você precisa ter certeza de que pt4newcomm.txt contém comandos válidos, não excluirá arquivos que você não deseja, etc.

Há provavelmente cinco maneiras melhores de fazer isso, mas o acima deve funcionar.

    
por 06.01.2018 / 18:30