+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
Scripts Thread, simple bash for loop script in Coding and Web Development; Im after a bash script that will do the following, for every file in the current folder (which will be ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,516
    Thank Post
    1,323
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199

    simple bash for loop script

    Im after a bash script that will do the following,

    for every file in the current folder (which will be .avi or .mkv)

    execute this command

    Code:
    mencoder input.file.name.avi -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=2500:acodec=libfaac -af lavcresample=48000 -vf scale=432:240,harddup -lavfopts format=psp -ofps 30000/1001 -o ouput.file.name.mp4
    but replace the filenames from the files from the list - it doesnt even matter if the output then becomes output.file.avi.mp4

    could someone knock something up for me please, Its pretty late and I want to let this run over-night and I dont really have time to teach myself the syntax (sorry if this seems lazy)


  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,729
    Thank Post
    3,252
    Thanked 1,049 Times in 971 Posts
    Rep Power
    364
    Quote Originally Posted by RabbieBurns View Post
    Im after a bash script that will do the following,

    for every file in the current folder (which will be .avi or .mkv)

    execute this command

    Code:
    mencoder input.file.name.avi -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=2500:acodec=libfaac -af lavcresample=48000 -vf scale=432:240,harddup -lavfopts format=psp -ofps 30000/1001 -o ouput.file.name.mp4
    but replace the filenames from the files from the list - it doesnt even matter if the output then becomes output.file.avi.mp4

    could someone knock something up for me please, Its pretty late and I want to let this run over-night and I dont really have time to teach myself the syntax (sorry if this seems lazy)

    Is this a *nix question again or is this just a general vbs / java script type question ?

  3. #3

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,516
    Thank Post
    1,323
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    [ame=http://en.wikipedia.org/wiki/Bash]Bash - Wikipedia, the free encyclopedia[/ame]

  4. #4

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,403
    Thank Post
    638
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319

  5. Thanks to webman from:

    RabbieBurns (5th August 2009)

  6. #5

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,663
    Thank Post
    516
    Thanked 2,446 Times in 1,894 Posts
    Blog Entries
    24
    Rep Power
    831
    Code:
    FILES="*.avi"
    
    for f in $FILES; 
    do 
     mencoder $f -oac lavc -ovc lavc -of lavf /
      -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=2500:acodec=libfaac /
     -af lavcresample=48000 -vf scale=432:240,harddup -lavfopts /
     format=psp -ofps 30000/1001 -o  $f.mp4
    done
    That, I think, would convert just the avi files. You could have the same block below, with FILES=*.mkv instead, but it can probably be done with a | perhaps. Can't test it as i'm in Windows at the moment *eugh* :P

    Or something similar. Been a while since I did proper bash stuff... Should help you in the right direction though.

    I'm sure Geoff will be along soon enough
    Last edited by localzuk; 4th August 2009 at 07:15 PM. Reason: tidy

  7. Thanks to localzuk from:

    RabbieBurns (5th August 2009)

  8. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,729
    Thank Post
    3,252
    Thanked 1,049 Times in 971 Posts
    Rep Power
    364
    Quote Originally Posted by localzuk View Post

    I'm sure Geoff will be along soon enough
    lol yeah he is like a lazy susan , he gets around just takes him a few minutes lol

    @ Geoff - only kidding !!

  9. #7

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    You don't need a for loop, find will do it without bashims:

    Code:
    find . -type f -name "*.avi" -o -name "*.mkv" -exec mencoder "{}" -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=2500:acodec=libfaac -af lavcresample=48000 -vf scale=432:240,harddup -lavfopts format=psp -ofps 30000/1001 -o "{}.mp4" \;
    Last edited by powdarrmonkey; 4th August 2009 at 07:39 PM.

  10. 2 Thanks to powdarrmonkey:

    localzuk (4th August 2009), RabbieBurns (5th August 2009)

  11. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,663
    Thank Post
    516
    Thanked 2,446 Times in 1,894 Posts
    Blog Entries
    24
    Rep Power
    831
    Quote Originally Posted by powdarrmonkey View Post
    You don't need a for loop, find will do it without bashims:

    Code:
    find . -type f -name "*.avi" -o -name "*.mkv" -exec mencoder "{}" -oac lavc -ovc lavc -of lavf -lavcopts aglobal=1:vglobal=1:vcodec=mpeg4:vbitrate=2500:acodec=libfaac -af lavcresample=48000 -vf scale=432:240,harddup -lavfopts format=psp -ofps 30000/1001 -o "{}.mp4" \;
    This is why I love Bash. So versatile!

  12. #9

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    My point was that this isn't bash at all, it's just the find command. Thus it's portable between shells, sans bashisms.

  13. #10

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,663
    Thank Post
    516
    Thanked 2,446 Times in 1,894 Posts
    Blog Entries
    24
    Rep Power
    831
    Quote Originally Posted by powdarrmonkey View Post
    My point was that this isn't bash at all, it's just the find command. Thus it's portable between shells, sans bashisms.
    Pff

  14. #11

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    In Debian-land we have the check-bashisms tool (part of the package maintenance toolkit); portability is especially important right now because we're changing the default system shell to dash, which would have broken many many packages a couple of years ago. It's been a monumental effort.

    Ubuntu have already been through this process.
    Last edited by powdarrmonkey; 4th August 2009 at 07:59 PM.

  15. #12

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,663
    Thank Post
    516
    Thanked 2,446 Times in 1,894 Posts
    Blog Entries
    24
    Rep Power
    831
    Quote Originally Posted by powdarrmonkey View Post
    In Debian-land we have the check-bashisms tool (part of the package maintenance toolkit); portability is especially important right now because we're changing the default system shell to dash, which would have broken many many packages a couple of years ago. It's been a monumental effort.

    Ubuntu have already been through this process.
    Why is bash being replaced by dash, a cut down, less feature-filled version? One that misses some key POSIX requirements at that?

    EDIT: It's down to 'speed' which is a bizarre reason considering how little time bash takes to start on anything moderately modern.
    Last edited by localzuk; 4th August 2009 at 08:06 PM.

  16. #13

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    It's lighter and faster, especially for embedded systems that don't have a lot of resources to spare - getting boot time down is our major aim. It's also easier to guarantee its presence on a base system because there are fewer dependencies. The few bits that are missing from POSIX are in developement AFAIK, but aren't common problems and so far (touch wood) we haven't had any disasters

    Ubuntu have a nice wiki page with a list of common bashisms: https://wiki.ubuntu.com/DashAsBinSh. The user shells aren't changing, just system shells.

  17. #14

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Can I just hijack this because I've been doing something a bit related to this and started with a loop and then realised that find did more or less what I want?

    I've got a set of users whose Windows roaming profiles are stored on a Linux server. I'm trying to find those who haven't used the profile for (say) 90 days. Each profile has a folder called <username>.pds

    What I've got so far is:

    Code:
    for folder in `find . -type d -name '*.pds' -maxdepth 1`
    do
      echo $folder
      du $folder -hc | grep total
      find $folder -name 'ntuser.dat'  -ls
    done
    This kind of works but it's messy - I get output looking like this:
    ./cbm.pds
    1.4M total
    472818 772 -rwxr--r-- 1 cbm stats 786432 Jul 17 2008 ./cbm.pds/ntuser.dat

    I can live with the first two lines - it wouldn't be a big deal to process those but I'd really like to just get the date on the third line. I *think* I need to take the output of the second find command and pipe it through "cut" but I can't work out how to do that - the exec command looks promising but I can't make sense of how to use it.

    Any offers?? Even if it's "start again, this is rubbish" :-)

  18. #15

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,403
    Thank Post
    638
    Thanked 961 Times in 661 Posts
    Blog Entries
    2
    Rep Power
    319
    @srochford: How's this?

    Code:
    find $folder -name 'ntuser.date' -ls | grep -o -E "[a-zA-Z]{3}(\s+)[0-9]+(\s+)[0-9]{4}"
    A grep using regular expression for the date format.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. bash to GUI
    By RabbieBurns in forum *nix
    Replies: 17
    Last Post: 27th May 2010, 10:32 AM
  2. Replies: 4
    Last Post: 19th March 2009, 02:35 PM
  3. bash script doesnt like folders with spaces
    By RabbieBurns in forum Scripts
    Replies: 20
    Last Post: 12th February 2009, 12:28 PM
  4. Simple PHP script for notes about students?
    By ChrisS in forum Scripts
    Replies: 1
    Last Post: 29th November 2007, 10:25 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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