PDA

View Full Version : [SOLVED] Loop in bash script



alfirdaous
March 16th, 2014, 12:41 PM
Hello everybody,

I would like to delete a batch of MP3 files lyrics, so I need to do a loop, MP3 files name are like below:

001, 002, 003,... and so on

Bash code:



#!/bin/bash
read -p "Please enter a starting number to delete lyrics? : " Start
read -p "Please enter a ending number to delete lyrics? : " EnD

for i in {$Start..$EnD}
do
/usr/bin/python /usr/bin/eyeD3 --remove-lyrics --remove-all $i.mp3
echo "Lyrics has been deleted successfully"
done


Result:


File Not Found: {1..5}.mp3


Thanks in advance

Lars Noodén
March 16th, 2014, 01:00 PM
Maybe with seq?



a=2;b=5;
for i in $(seq $a $b);do echo $i;done

Vaphell
March 16th, 2014, 01:53 PM
bash does {} interpretation first and expects proper values, $var substitution goes next, so {$x..$y} can't work

bash supports C-like for loop that can be used for that purpose


#!/bin/bash

read -p "Please enter a starting number to delete lyrics? : " start
read -p "Please enter a ending number to delete lyrics? : " end

for(( i=start; i<=end; i++ ))
do
printf -v f '%03d.mp3' $i # f is a variable that stores 0-padded 3-digit wide $i.mp3
/usr/bin/python /usr/bin/eyeD3 --remove-lyrics --remove-all "$f"
done

btw echo will print regardless of the result of the actual command
you should chain eye3d and echo with && so echo gets executed only when it's actually true
is there a reason why you use full paths for python and eyeD3?

alfirdaous
March 17th, 2014, 09:48 AM
@Lars Noodén (http://ubuntuforums.org/member.php?u=168643): I think seq is deprecated

@Vaphell (http://ubuntuforums.org/member.php?u=347382): Thx for your help

mörgæs
March 17th, 2014, 10:01 AM
If this solves your problem please mark the thread so.

alfirdaous
March 17th, 2014, 11:05 AM
is there a reason why you use full paths for python and eyeD3?

If not precising the path, will show an error that python and eyed3 not found

Vaphell
March 17th, 2014, 11:30 AM
what did you do to your PATH that you don't have /usr/bin in it? o.O