Michael Hartog

Detailing random personal things since 2008
  • Michael Hartog
  • Blog
  • About
  • Livestream
  • Mini DB App
  • To-do
  • Resumé
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, random, Technology, Work. 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

    • Kurina
    • Rebecca
    • Veronica
  • Links To Things

    • Idle Thumbs
    • Proxy
    • Tales of Mere Existence
    • XKCD
  • Social

    • Facebook
    • Google+
    • Last.fm
    • Steam
    • Tumblr
    • Twitter
  • Twitter

    • Wouldn't it be a great night for zombies?
      2012/02/03 21:24
    • So now that I found the skill cause my Diablo 3 to lag out, I've just stopped using it and started playing. Very fun times.
      2012/02/03 03:57
    • Finally in the Diablo 3 beta, woo!
      2012/02/01 15:38
    • When exactly has banning books a) actually worked or b) been a good idea?
      2012/01/14 12:02
  • Categories

    • Books (3)
    • Comparisons (11)
    • Computers (20)
    • Gaming (16)
    • Music (4)
    • Politics (2)
    • random (38)
    • School (12)
    • Sports (1)
    • Technology (16)
    • 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).