AUUGN, August, 1999

My contributions to AUUG's newsletter.   This time it's tricks and traps.

Tricks and Traps
By David Newall <david.newall@tellurian.com.au>
August 16, 1999,
Adelaide, South Australia,
Overcast with occassional light showers

How do you find the C: drive in Linux?

One of the questions which pesky DOS users will ask is how to find the C: drive in Linux.   Perhaps the most concise answer is RTFM, which of course every true Unix Wizard binds as a macro to some easy to locate key such as the space-bar.   Sometimes, if I'm in a particularly irksome mood, I like to answer thus...

The C drive is the first DOS or Windows partition.   It's probably on your first IDE hard drive, so the Linux name will be /dev/hda-something.   If the C drive is on the first partition it will be /dev/hda1; if it's the second partition it will be /dev/hda2; and so on.   To "find" the C drive you must mount it.   The following command will work:

   # mount /dev/hda1 /

Following this your C drive will be available at /.

Unix in maintenance mode

Ever had the problem that your most basic Unix utilities weren't available, such as ls, cat and more?   Perhaps this was when you were rebuilding your system after a disaster.   Install disk often permit you to use a shell, but due to space limits on a floppy, they only include the utilities that are absolutely essential.

You can build a surprising set of "standard utilities" using just the Bourne shell:

Listing directories

ls() {
   [ -z "$1" ] && echo * || echo $1/*
}

Displaying the content of files

cat_out() {
   while read cat_line; do
      echo "$cat_line"
   done
}
cat() {
   if [ -z "$1" ]; then
      cat_out
   else
      for cat_file; do
         [ "$cat_file" = - ] && cat_out || cat_out < $cat_file
      done
   fi
   unset cat_file cat_line
}

Paging through files

more_out() {
   more_count=0
   while read more_line; do
      [ $more_count -eq 23 ] && {
         echo "--more-- \c"; read more_count; more_count=0
      }
      more_count=`expr $more_count + 1`
      echo "$more_line"
   done
}
more() {
   if [ -z "$1" ]; then
      more_out
   else
      for more_file; do
         [ "$more_file" = - ] && more_out || more_out < $more_file
      done
   fi
   unset more_file more_line more_count
}

I might just say that with a little thought and effort a great many problems can be solved with this technique.   For example, I recently was unable to log in to a machine.   I guessed (correctly) that all process slots were full; and noticed that getty respawned when it quit.   "Race," thought I, and proceeded to log in on one screen while quitting getty on another.   With no free process slots it was a challenge to discover exactly what was wrong.   For the standards conformant amongst us, probably exec ps is necessary, which means winning another race to log in again.   Those who are not so standards challenged can ls /proc (see above).   You can't use pipelines when there are no spare process slots, but you certainly can use temporary files to hold the output from each stage.   A shell-coded, specialised grep resulted in an appropriate kill statement, and all that was left to do was exec 0<kill-file and presto! one working system.