for scripting...

Associate
Joined
28 Oct 2002
Posts
1,819
Location
SE London
Herro, I need some help with some "for" scripting, basically I want to do a find *.foo in a directory, then run bar.sh on those files.

Can anyone help? I'm pretty sure I can run this as a one shot line. I'm basically trying to get a script that I can use to create a watch directory on my server, when I drop files in there, it'll convert them using handbrakecli and then once converted delete or move them, which I've done.
 
You could also do:

Code:
for i in $(ls *.foo); do ./bar.sh $i; done

Since backticks have been depreciated for some time...
 
new and improved version of this, I realised, that this works, but I'd ideally like to do some sed\cut\awk magic on the output first, as basically what it's currently doing is taking SomeVideo.avi and converting it fine, but calling it SomeVideo.avi.mp4...

Ideas? I did the sed work, but I don't know how to get that to dump through to the conversion script, the process I'd like is:

1) ls /storage/to_be_converted
2) remove .xxx from the output
3) pass each variable, less the file extension to convert.sh
4) ???
5) PROFIT

What I did to get the sed to work, is I had to do a ls > derp.txt then ran sed against the file.

Ideas?
 
Code:
for i in $(ls /storage/to_be_converted/*.foo); do
    convert.sh $(basename $i .foo)
done

???
There's numerous ways it can be done...
 
Actually you don't need the subshell at all -- at least in bash, zsh and modern shells
Code:
for i in *.foo; do echo $i; done
 
Working product, if anyone is interested:

watch.sh
Code:
#!/bin/bash
for i in $(ls /storage/2tb/incoming_videos/); do convert.sh $i $(basename $i); done

Run every 30mins

I used 2 variables in the end, one which was the output from ls and the other with the $(basename $i) for the output file name...

Converter script:

Code:
#!/bin/bash
HandBrakeCLI -e x264  -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:8x8dct=0:trellis=0:subme=6 -i $1 -o /storage/2tb/converted_videos/$2.mp4

This then converts the file into Apple Universal format which I can dump on any portable device pretty much. :)

I know it's kinda silly to have multiple copies of videos, but meh - storage is such a commodity these days, I might as well have ones which are ready for dumping on mobile devices as well as HD versions for watching on my TV.
 
Back
Top Bottom