Results 1 to 7 of 7

Thread: Loop in bash script

  1. #1
    Join Date
    May 2012
    Beans
    364

    Loop in bash script

    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:

    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:
    Code:
    File Not Found: {1..5}.mp3
    Thanks in advance

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    seq

    Maybe with seq?

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

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Loop in bash script

    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

    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=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?
    Last edited by Vaphell; March 16th, 2014 at 02:07 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    May 2012
    Beans
    364

    Re: Loop in bash script

    @Lars Noodén: I think seq is deprecated

    @Vaphell: Thx for your help

  5. #5
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    11,860
    Distro
    Lubuntu 16.04 Xenial Xerus

    Re: Loop in bash script

    If this solves your problem please mark the thread so.
    Bringing old hardware back to life. About problems due to upgrading.
    Please visit Quick Links -> Unanswered Posts
    Don't use this space for a list of your hardware. It only creates false hits in the search engines.

  6. #6
    Join Date
    May 2012
    Beans
    364

    Re: Loop in bash script

    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

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Loop in bash script

    what did you do to your PATH that you don't have /usr/bin in it? o.O
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •