Shell script to extract isos from a folder
I had to do this repeatedly manually at work so it took me like 2 minutes to make my first script to do it for me. It was horribly messy and looked something this:
for x in $(ls -1 .); do
mount -t iso9660 -o loop $x temp/;
cp -r temp/* .;
umount temp/;
done;
Which did the job but was horribly messing on doing it more than once with tons of errors and stuff.
When I came home I decided to make it much better. I even included how long it takes to do the whole process, which was harder than the process itself.
So right now it looks like:
before=$(date +%s);
mkdir temp/;#tempdir
for x in $(ls -1 ./*.iso); do
echo Currently unpacking $x | sed -e ‘s/.\///; s/.iso//’; #remove extra output
mount -t iso9660 -o loop $x temp/; #mounts iso
cp -ru temp/* .;
umount temp/;
done;
rmdir temp/; #cleanup
after=$(date +%s);
elapsed=$(expr $after – $before); #time calculations
((hour=$elapsed/60/60));
((min=$elapsed/60-hour*60));
((sec=$elapsed-hour*60*60-$min*60));
echo “Elapsed time: “`printf “%02d” $hour`”:”`printf “%02d” $min`”:”`printf “%02d” $sec`;
Which does everything I want it too, even the pain in the ass zero-padding of the times. Remember to chmod +x <filename> if you want to use it
.
Edit: Well, I messed up a few things, I only tested in under one minute times and the shell on that server only contains sh and ash. Here is the revised edition:
#! /bin/sh
before=$(date +%s);
mkdir temp/;#tempdir
for x in $(ls -1 ./*.iso); do
echo Currently unpacking $x | sed -e ‘s/.\///; s/.iso//’; #remove extra output
mount -t iso9660 -o loop $x temp/; #mounts iso
cp -ru temp/* .;
umount temp/;
done;
rmdir temp/; #cleanup
after=$(date +%s);
elapsed=$(expr $after – $before); #time calculations
hour=”$(expr “$elapsed” ‘/’ ’60′ ‘/’ ’60′)”
min=”$(expr “$elapsed” ‘/’ ’60′ ‘-’ ‘$hour’ ‘*’ ’60′)”
sec=”$(expr “$elapsed” ‘-’ ‘$hour’ ‘*’ ’60′ ‘*’ ’60′ ‘-’ ‘$min’ ‘*’ ’60′)”
echo “Elapsed time: “`printf “%02d” $hour`”:”`printf “%02d” $min`”:”`printf “%02d” $sec`;
And now I know that sh is *really* annoying to do math in.
Interesting, nice use of sed
. I wonder if, on a minescule scale, using bash’s built in file searching would be faster than “outsourcing” to ls. Like “for x in *.iso; do..” etc. Also, for timing, couldn’t you just have used “time ./script”? May have saved some trouble.
It’s cool that your getting into shell scripting though, great stuff
Time had more output than I wanted.
And it turns out there are errors in this script, and it uses features from bash which the environment I was running on only has sh. I had to make a few changes and I’ll post that one soon.