Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hello Everyone, This is literally my first script so please go easy on me Code: #!/bin/bash # # find ~/Projects/Scripts/ -iname "*.avi" | \ while IFS= read -r I; do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5

    Need Find & Convert Shell Script To Loop


    Hello Everyone,

    This is literally my first script so please go easy on me
    Code:
    #!/bin/bash
    #
    #
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while IFS= read -r I;
    do
    HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done
    It's a simple script to find video files and convert them to a different format. However, the script will only find one video, convert and then stop. I need it to find ALL video files, convert them and then stop (there are two videos files in the script directory for testing purposes). Could someone shed some light on this issue? I'm open for any suggestions and as always any help is greatly appreciated!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    are you *sure* it is finding all avi files, irregardless of the conversion command? try putting an echo right before HandBreakCLI to just show that it is iterating over all files found.

    there are other ways to loop, too. this is a common convention:

    Code:
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done < <(find . -iname "*.avi")
    make sure that your find command is turning up all the files.

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by atreyu View Post
    are you *sure* it is finding all avi files, irregardless of the conversion command? try putting an echo right before HandBreakCLI to just show that it is iterating over all files found.

    there are other ways to loop, too. this is a common convention:

    Code:
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done < <(find . -iname "*.avi")
    make sure that your find command is turning up all the files.
    Thank you so much for your prompt reply! Unfortunately when I amended your modified script to mine:
    Code:
    #!/bin/bash
    #
    #
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done <<(find . -iname "*.avi")
    I received this error:
    Code:
    Video-Conversion-2.sh: 8: Video-Conversion-2.sh: Syntax error: "(" unexpected
    I looked into this error and I believe it is becaue I'm using a different version of BASH, so here is my setup:

    Code:
    GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
    Distributor ID:	Ubuntu
    Description:	Ubuntu 13.04
    Release:	13.04
    Codename:	raring
    Kernel:      3.8.0-31-generic
    I apologize that I left that information out from the first post. Is there another way to approach this and then try again?

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by alteredstate View Post
    Unfortunately when I amended your modified script to mine:
    Code:
    #!/bin/bash
    #
    #
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done <<(find . -iname "*.avi")
    I received this error:
    Code:
    Video-Conversion-2.sh: 8: Video-Conversion-2.sh: Syntax error: "(" unexpected
    you need a space inbetween those two parentheses, e.g.:
    Code:
    done <  <(find . -iname "*.avi")

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,294
    Hi.
    Code:
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done <<(find . -iname "*.avi")
    It looks like the loop is coded to have input from the top in addition to the bottom -- piped into the top at the while and re-directed at the bottom at the done. I don't know if one will take priority over the other (might be an interesting experiment), but I think one should be removed.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Just Joined!
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by atreyu View Post
    you need a space inbetween those two parentheses, e.g.:
    Code:
    done <  <(find . -iname "*.avi")
    Quote Originally Posted by drl View Post
    Hi.
    Code:
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while read I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done <<(find . -iname "*.avi")
    It looks like the loop is coded to have input from the top in addition to the bottom -- piped into the top at the while and re-directed at the bottom at the done. I don't know if one will take priority over the other (might be an interesting experiment), but I think one should be removed.

    Best wishes ... cheers, drl
    Quote Originally Posted by atreyu View Post
    you need a space inbetween those two parentheses, e.g.:
    Code:
    done <  <(find . -iname "*.avi")
    Here is the complete script I ran (including spaces as you indicated):

    Code:
    #!/bin/bash
    #
    #
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while IFS= read -r I; do
      HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done < <(find . -iname "*.avi")
    but I still get error:

    Code:
    Video-Conversion-2.sh: 8: Video-Conversion-2.sh: Syntax error: redirection unexpected
    Now I ran the script sprinkled with an "echo" command:

    Code:
    #!/bin/bash
    #
    #
    find ~/Projects/Scripts/ -iname "*.avi" | \
    while IFS= read -r I; do
      echo HandBrakeCLI -i "${I}" -o "${I%.avi}".mkv --preset="iPhone & iPod Touch"
        echo "$I has been converted"
    done
    and here here is the output:

    Code:
    $ sh Video-Conversion-2.sh 
    HandBrakeCLI -i /home/marcus/Projects/Scripts/Video-Test2.avi -o /home/marcus/Projects/Scripts/Video-Test2.mkv --preset=iPhone & iPod Touch
    /home/marcus/Projects/Scripts/Video-Test2.avi has been converted
    HandBrakeCLI -i /home/marcus/Projects/Scripts/Video-Test1.avi -o /home/marcus/Projects/Scripts/Video-Test1.mkv --preset=iPhone & iPod Touch
    /home/marcus/Projects/Scripts/Video-Test1.avi has been converted
    It appears that when the HandBrake conversion completes, it does not go back and grab the other video that was found. Is there a way to output each result of the "find" command to HandBrake one at a time?

  8. #7
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    yeah, like drl said, you had the find bit both before and after the loop. definitely not what you want.

    Quote Originally Posted by alteredstate View Post
    It appears that when the HandBrake conversion completes, it does not go back and grab the other video that was found. Is there a way to output each result of the "find" command to HandBrake one at a time?
    yes, that's a good idea, actually. you want find's -exec parameter. like this:

    Code:
    find ~/dir -type f -iname '*.avi' -exec Handbrake -options {} \;
    in the above command, the {} is the variable name that represents each avi file found. the \; is required at the end of the find command.

  9. #8
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Why making things complicated?

    I do not know anything about "Handbrake" but the basic loop can be coded:

    Code:
    for file in `ls *.avi | sed "s/\.avi//"`
    do
    echo input_file ${file}.avi
    echo output_file ${file}.mkv
    done
    A list of all *.avi files is created with ".avi" removed with sed. The back-slash is to escape the "." in the "sed script". Having these basic names you can then add characters before and/or after to have your input and output files. All basic UNIX without any new "bash inventions"!

    "find" is for searching in all sub-directories! Assuming all files to be in the same directory "ls" is the right command to use!
    Last edited by skitgubbe; 10-06-2013 at 09:27 PM.

  10. #9
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    226
    hi,

    `ls' is never the right tool to be used in a script.
    keep using `find'.
    `find' is not «for searching in all sub-directories»; if you do not intend to go down the whole hierarchy, then configure its --maxdepth option!

  11. #10
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Quote Originally Posted by watael View Post
    hi,

    `ls' is never the right tool to be used in a script.
    keep using `find'.!
    Why do you have bad feelings towards "ls"? Strange statement!

Page 1 of 2 1 2 LastLast

Posting Permissions

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