home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / The-Disk-Scripts / eject.sh next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1996-03-13  |  6.2 KB  |  197 lines

  1. #!/bin/sh
  2. #
  3. # AUTHOR:     Timothy J. Luoma
  4. #
  5. # EMAIL:     luomat@capitalist.princeton.edu
  6. #         476tjl@ptsmail.ptsem.edu
  7. #
  8. # DATE:     21 Nov 1995
  9. #
  10. # WARRANTY:    "  " (this space blank on purpose)
  11. #
  12. # PURPOSE:    Allows (root) user to eject a disk at the
  13. #         command-line.
  14. #
  15. # RULES FOR USE: Use at your own risk.  This has only been tested
  16. # by me under limited, specific usage.  Don't charge someone else
  17. # money for it.  If you improve it, let me know.  If you redistribute
  18. # it after improving it, please keep my name on their somewhere,
  19. # even if just to say "This idiot did this all wrong, so I fixed
  20. # it."
  21. #
  22. # SOME USAGE NOTES:
  23. # 1) Some error messages occur when I am eject a disk using this
  24. # script, but none of them seem to harm anything.
  25. # 2) Usage: both 'umount' and 'disk -e' must be run by 'root',
  26. # unless root changes the execute permissions.  Sorry.  There are
  27. # obvious workarounds (SETUID root/chmod 4755 is what I did since
  28. # I'm the only user on my NeXTStation).
  29. #
  30. # 3) The 'umount' and 'disk -e' commands are done on ONE line,
  31. # connected by "&&" so that the 'disk -e' is done ONLY when the
  32. # 'umount' has already been done.  This keeps the disk from being
  33. # ejected when it hasn't been umounted, which can be a real pain
  34. # in the compiler.
  35. #
  36. # 4) I use 'umount -v' which echoes when the disk is unmounted.
  37. # You can remove the '-v' if you don't like this.
  38. #
  39. # 5) If the script complains about not being able to find the
  40. # 'mount' command, either use the full path (usually /usr/etc/mount)
  41. # or add the foldername to the $PATH (but you knew that, didn't
  42. # you ;-)
  43. #  
  44. # 6) This script assumes that your floppy disks are mounted on
  45. # '/dev/rfd0b' because this is where my 3.2 NeXTStation mounts
  46. # floppy disks.
  47. #
  48. # 7) CDs seem to be mounted in a way that makes little sense to me.
  49. # My CD drive is /dev/sd2a.  Some CDs are mounted on /dev/sd2a
  50. # (most notably the NeXT CDs), and some are mounted on /dev/rsd2h.
  51. # To account for this, I simply defined it as if there was an
  52. # additional drive which gets mounted on 'rsd2h'.
  53. #
  54. # 8) ONE VERY important note: to eject a disk using 'disk -e' you
  55. # need to reference the RAW device.  That is, for /dev/sd2a I need
  56. # to use /dev/rsd2a.  TO THAT END: When you define the drive
  57. # variables, you must use the regular device.  THEN when you use
  58. # 'disk -e' you must use 'disk -e /dev/r$yourvariablename'
  59. # "WHY?" you ask.  Because 'mount' reports the regular device,
  60. # while 'disk -e' requires the raw device.  Hey, this is a hack,
  61. # after all.
  62. #
  63. # 9) IF you can install another drive, I'm quite sure that you can
  64. # figure out how to add the eject code for it to this simple script.
  65. # I hope I have given a sufficiently useful road map on how this
  66. # can be done.
  67. #
  68. # 10) This entire script is a hack.  It isn't meant to be particularly
  69. # pretty, it's just meant to allow you to eject a disk from the
  70. # commandline.
  71. #
  72. #
  73. # 11) Oh, once you have read this, you can remove the next few lines and
  74. # make the program actually work:
  75. # echo "You should read through the code first to make sure that you"
  76. # echo "know how this program works and what assumptions it operates"
  77. # echo "under or else it might not work, or not work how you think "
  78. # echo "it should work.    USE AT YOUR OWN RISK!!!!"
  79. # exit 0
  80. # end of sermon about reading the code before running the program.
  81.  
  82.  
  83.  
  84.  
  85.  
  86. name=`basename $0`
  87.  
  88.     ##########################################################
  89.     # The FORMAT is important here.  It is simply the part   #
  90.     # which comes after "/dev/".  If you don't know it is,   #
  91.     # check 'mount -p' and look at the first field.  Example:#
  92.     # my floppy drive is /dev/rfd0b, so I use "rfd0b"        #
  93.     ##########################################################
  94.  
  95. # This is where my floppies get mounted
  96. floppydrive=rfd0b
  97. floppy2=fd0a
  98.  
  99. # this is where my NeXT CDs get mounted
  100. # NOTE:    this is not the RAW device, 
  101. #     see BELOW for how this is handled. 
  102. #     see ABOVE for why this is a problem.
  103. otherdrive=sd2a
  104.  
  105. # This is where my non-NeXT CDs get mounted
  106. yadrive=rsd2h
  107.  
  108.  
  109. ####################################
  110. # the actual program begins here.  #
  111. ####################################
  112.  
  113. if [ $# -lt 1 ]
  114. then 
  115.     echo "<Usage> $name /diskname"
  116.     exit 0
  117. fi
  118.  
  119.  
  120.  
  121.     # check the mountpoint, ie, which /dev it is mounted on.
  122.     # This is the first field in the 'mount -p' command.
  123. mounted=`mount -p | grep $1 | awk '{print $1}' | sed 's/\/dev\///'`
  124.  
  125.  
  126.  
  127.     ##########################################################
  128.     # If the mountpoint is the floppydrive, eject the floppy #
  129.     # drive                                                  #
  130.     ##########################################################
  131.     
  132. if [ "$mounted" = "$floppydrive" ]
  133. then
  134.      umount -v $1 && \
  135.      disk -e /dev/$floppydrive 
  136.  
  137.  
  138. elif [ "$mounted" = "$floppy2" ]
  139. then
  140.      umount -v $1 && \
  141.      disk -e /dev/r$floppy2
  142.  
  143.     ##########################################################
  144.     #                                          #
  145.     # NOTE: This is a SPECIAL CASE.  You need to use the RAW #
  146.     # device here.                                  #
  147.     #                                          #
  148.     ##########################################################
  149.  
  150. elif [ "$mounted" = "$otherdrive" ]
  151. then
  152.      umount -v $1 && \
  153.      disk -e /dev/r$otherdrive
  154.          # note the "r" ^
  155.  
  156.  
  157.  
  158.     ##########################################################
  159.     # if the mountpoint is the otherdrive, eject that.       #
  160.     ##########################################################
  161.  
  162. elif [ "$mounted" = "$yadrive" ]
  163. then
  164.      umount -v $1 && \
  165.      disk -e /dev/$yadrive
  166.  
  167.  
  168.  
  169.  
  170.     ############################################################
  171.     # if the mountpoint is none of the above, don't try to     #
  172.     # eject it... You'd hate to dismount something by accident,#
  173.     # like "/" ;-)                                             #
  174.     ############################################################
  175.  
  176. else
  177.     echo "$name error: not a disk"
  178. fi
  179.  
  180.  
  181. ####################################################################
  182. # If you have TickleServices, then you have a command called
  183. # "WSNotify" which updates the File Viewer from the command-line,
  184. # much like "Update Viewer" in the WM menu.  If you have it, this
  185. # would be a good place to invoke it, so the File Viewer will know
  186. # that the disk has been ejected.
  187.  
  188. # NOTE: WSNotify only seems to work for me if the File Viewer is
  189. # not hidden.  Your mileage may vary ;-)
  190.  
  191. # If you have WSNotify, uncomment the next line. 
  192. WSNotify && echo "$name: Updated File Viewer"
  193.  
  194. exit 0
  195.  
  196.