Bash

Bash

Loops

For

Simple Example

#!/bin/bash
for i in $( ls ); do
    echo item: $i
done

For with Slight Extra Commands

This generates EXIF data from JPEG files
for i in $( ls *.jpg ); do
  echo item: $i
  o=$(basename $i .jpg)
  exif $i > $o.txt
done

Trapping the time command

time is a very useful command to see how things are going. However this does not work

time ps -ef | grep real

This is because time sends the output to stderr (channel 2) ... so we need some shell trickery....

(time (perl t_mem.pl 10k.xml > /dev/null) 2>&1 ) | grep real

and in a script this will become

taken=$((time (perl t_mem.pl 10k.xml > /dev/null) 2>&1 ) | grep real) echo "It took "$taken" to do that command"

Video Converting

This example uses HandBrake

for a in *.mkv
do
file=$(basename "$a" .mkv)".mp4"
echo "HandBrakeCLI -i \"$a\" -o \"$file\""
HandBrakeCLI -i "$a" -o "$file" -f mp4
done

sha256

echo -n foobar | sha256sum

The -n is needed to not generate a 'n'