

Ask HN: Help with BASH Scripting? - jason_slack

I am trying to write a bash script that automates several types of video encodes.<p>I pass in a path and a file name and the script takes that info and encodes  the video several ways, then renames some files and copies to a final destination path. Not so hard.<p>What I cannot figure out for the life of me is can I provide a file for my bash script to pull my paths, file names and destinations from and just mass add files all day to the single file and then run the script to encode overnight.<p>Example have a file that has in it.<p>orignal path, file name, destination path<p>orignal path, file name, destination path<p>orignal path, file name, destination path<p>orignal path, file name, destination path<p>and I run a script that parses each line, does the encodes and copies for it, then moves to the second line when the first is done and moves to the third, etc, etc<p>Lastly, is it possible to automate MySQL Insert statements with BASH. I supposes at the very least I could create the insert statements in batch from the parameters and output them to a file containing the SQL and manually execute it in the morning....<p>Can anyone provide insight on how use a file with all my paths and parameters? OSX 10.6.
======
jasonrojas
You want to check the exit code of the mysql insert before continuing, it's $?
in bash. (basically a loop with an exit if $? != 0) More info on exit codes:
<http://tldp.org/LDP/abs/html/exit-status.html> And bash scripting:
<http://www.tldp.org/LDP/abs/html/>

------
aeontech
You'll probably get more help on stackoverflow... HN isn't really the right
venue for specific programming questions :)

That being said, you can just use tail -f as input for your script. Something
like this:

tail -f input.txt | encoder.sh &

and have encoder.sh run in a loop reading stdin into a var, encoding the file
if var was non-empty and sleeping if it's empty

This is if you are going to be modifying the file while the encoder is
running; if the file is static, then sorbus's solution is totally fine.

As for mysql, again, input redirection is your friend.

mysql -u root -D db < dump.sql will run statements in dump.sql against
database called db

------
sorbus
Taking information from a file and acting on it is something like

    
    
      for $line in `cat file`
      do
         encode stuff
      done
    

You would probably want to use awk or something similar to split up the lines
into multiple values, though. It's been a while since I've done something like
this, but for loops are definitely the way to go.

~~~
jasonrojas
cat doesn't like spaces.

