floppycopy (my script)

Discuss Programming
Post Reply
User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

floppycopy (my script)

Post by Calum »

ok well i started a little script of my own to try to learn a bit of shell scripting. i am calling it floppycopy because i keep my university notes on a floppy (for portability), and so does my girlfriend. i thought if i could write a script to automatically do backups then it would be really cool.

anyway, here's version 0.1 of floppycopy, it works fine as far as i can see:

Code: Select all

#!/bin/bash
##################################################################
# floppycopy
#
# This script is designed to make a backup image of a floppy disk
# in /dev/fd0 (the first floppy drive in Linux) and write it to a
# file.
#
# This script (such as it is) where copied is regulated by the
# terms of the GNU General Public Licence, see www.gnu.org for
# more information.
#
# Usage: floppycopy <nameofimage>
# where nameofimage is the filename you wish to give the new
# image file. The actual filename will be "<nameofimage>.img"
# in the current directory. This file can be mounted and edited 
# like any other filesystem using:
# 	mount -o loop <nameofimage>.img <directory>
# where <directory> is the name of the directory at which to mount
# the image. It can be rewritten to another floppy using:
# 	dd if=<nameofimage>.img of=/dev/fd0
#
# This script depends on "mount" and "dd" 
#
# Version 0.1
##################################################################
umount /dev/fd0
dd if=/dev/fd0 of=$1.img
mount /dev/fd0
echo -e "\nBackup complete, floppy disk has now been remounted"
echo -e "Disk image has been written as $1.img"
and in fact i now have it sitting in my /usr/local/bin directory as a testament to my 1337 scripting skills! :D
however...
Version 0.2 is a little more ambitious and is not entirely "bug free" i think. it's (hopefully) simple enough but i thought i would post it here because of ESR's famous 'many eyeballs' thing.

Code: Select all

#!/bin/bash
##################################################################
# floppycopy
#
# This script is designed to make a backup image of a floppy disk
# in /dev/fd0 (the first floppy drive in Linux) and write it to a
# file.
#
# This script (such as it is) where copied is regulated by the
# terms of the GNU General Public Licence, see www.gnu.org for
# more information.
#
# Usage: floppycopy <nameofimage>
# where nameofimage is the filename you wish to give the new
# image file. The actual filename will be "<nameofimage>.img"
# in the current directory. This file can be mounted and edited 
# like any other filesystem using:
# 	mount -o loop <nameofimage>.img <directory>
# where <directory> is the name of the directory at which to mount
# the image. It can be rewritten to another floppy using:
# 	dd if=<nameofimage>.img of=/dev/fd0
#
# This script depends on "mount", "grep" and "dd" 
# It also assumes the floppy disk drive is /dev/fd0, of course you
# could edit the script if your floppy drive is referred to by
# another device name.
#
# Version 0.2
#
# Version History:
# 0.2 - checks given filename before starting operation
#       only attempts un/remounts if necessary
# 0.1 - can write floppy image to a file & remount disk
##################################################################

# Firstly I'm just going to check if /dev/fd0 is already mounted
mount | grep /dev/fd0 > /dev/null
mounted="echo $?"

# Now, let's unmount the floppy before we try to make our image
if [ $mounted -eq 0 ]
then
   umount /dev/fd0
   if [ $? -ne 0 ]
then
      echo -e "Cannot unmount floppy disk,\nplease ensure no programs are accessing it and try 
again"
      exit 1
   fi
fi

# Here, I am using dd to create an image file of the floppy disk
if [ $1 = 0 ]
then
   echo -e "You must enter a file name\nExample: floppycopy filename"
   exit 2
else
   dd if=/dev/fd0 of=$1.img
fi
if [ $? -ne 0 ]
then
   echo -e "PANIC PANIC PANIC!!!!! I couldn't make a backup of your floppy."
   echo -e "Is the floppy in the drive?"
   echo -e "Do you have write permission to the current directory?"
exit 3
fi


# If the disk was mounted before, let's be nice and remount it here
if [ $mounted -eq 0 ]
then
   mount /dev/fd0
   if [ $? -ne 0 ]
then
   echo -e "Could not remount floppy, perhaps /dev/fd0 is not in /etc/fstab?"
   fi
fi

# Goodbye Message:
echo -e "\nBackup complete,\nDisk image has been written as $1.img"
exit

now this doesn't seem to work properly to me, although it does appear to do the job, it doesn't seem to be unmounting the floppy while it does it (if i do 'mount' in another window, /dev/fd0 still shows up) also, whenever i execute the script, bash gives the error "./floppycopy: [: too many arguments" before continuing. I have limited skill at this, and in fact floppycopy 0.2 represents virtually my entire scripting knowledge (apart from a couple of hours learning perl i have since forgotten). I was going to try and recreate each of the errors i was trying to make it check for to see if it went wrong correctly (heh, if you see what i mean) but i haven't had time.

Anyway, comments, criticisms, insults are all welcomed.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

I take it your using Automount on Womandrake, maybe do this is what I do when I make a backup of somthing...
$ cp /dev/fd0 floppy_backup_filename.extension
much quicker!

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum »

automount?
if i was using mandrake that would be the first thing i would get rid of.
nope i am using slack 9.
and i am just trying to do a little script and then add little features. the idea is that hopefully i can get it interactive and asking questyions of the user, then possibly rewrite it in some other language.

Tux
guru
guru
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Post by Tux »

w00teh w00teh it's Calum!

Post Reply