Michael Hartog

Detailing random internet junk since 2008
  • Home
  • About
  • To-do
27 May 2008

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.

Tags: bash, iso, linux, script, shell

This entry was posted on Tuesday, May 27th, 2008 at 10:34 pm by Michael Hartog and is filed under Computers, Technology, Work, random. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Shell script to extract isos from a folder”

  1. Vince says:
    May 30, 2008 at 1:31 am

    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 :)

    Reply
  2. Apocrypha says:
    May 31, 2008 at 3:49 am

    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.

    Reply

Leave a Reply

Click here to cancel reply.

« Opeth, 3 and that other band
Mixing drinks »
  • Blogs

    • Game Girl
    • James Gunn
    • Rebecca
    • Veronica
  • Links

    • Idle Thumbs
    • Tales of Mere Existence
    • XKCD
  • Twitter

    • :( Starcraft 2 trial finished, now I have to wait for Tuesday night. Link 3 hours ago
    • Took my sister out to see Inception last night. Great movie and surprised that I waited 2 weeks and still didn't get anything spoiled. Link 13 hours ago
    • Twitter presence diminished while I wait to get my N1 back :( Link 2010/07/30
    • @LiruWilkowski Love me some Community :D Link 2010/07/30
  • Categories

    • Books (2)
    • Comparisons (7)
    • Computers (18)
    • Gaming (11)
    • Music (2)
    • Politics (1)
    • random (34)
    • School (12)
    • Technology (15)
    • TV (3)
    • Uncategorized (1)
    • Work (5)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
Michael Hartog's Blog is proudly powered by WordPress and NearlyFreeSpeech.NET
Design & code by Jonk
Entries (RSS) and Comments (RSS).