20 Emulating Another Environment: DIR


#20 Emulating Another Environment: DIR

While many computer aficionados learned how to work with an operating system within a Unix or Linux environment, many others started on other systems with other commands and other styles of interaction. It's quite likely that some users in your organization, for example, are still more comfortable on the MS-DOS command line than they are when faced with a Unix shell prompt. A set of aliases can be installed to ease the transition a little bit, like mapping the DOS command DIR to the Unix command ls :

 alias DIR=ls 

However, this mapping won't help users if they've already taught themselves that the /W option produces a wide listing format, because the ls Unix command will just complain that directory /W doesn't exist. Instead, in the same spirit as wrappers that change the input, the following DIR script can be written to map one style of command flags to another.

The Code

 #!/bin/sh # DIR - Pretends we're the DIR command in DOS and displays the contents #   of the specified file, accepting some of the standard DIR flags. function usage { cat << EOF >&2   Usage: 
 #!/bin/sh # DIR - Pretends we're the DIR command in DOS and displays the contents # of the specified file, accepting some of the standard DIR flags. function usage { cat << EOF >&2 Usage: $0 [DOS flags] directory or directories Where: /D sort by columns /H show help for this shell script /N show long listing format with filenames on right /OD sort by oldest to newest /O-D sort by newest to oldest /P pause after each screenful of information /Q show owner of the file /S recursive listing /W use wide listing format EOF exit 1 } postcmd="" flags="" while [ $# -gt 0 ] do case $1 in /D ) flags="$flags -x" ;; /H ) usage ;; /[NQW] ) flags="$flags -l" ;; /OD ) flags="$flags -rt" ;; /O-D ) flags="$flags -t" ;; /P ) postcmd="more" ;; /S ) flags="$flags -s" ;; *) # unknown flag: probably a dir specifier break; # so let's get outta the while loop esac shift # processed flag, let's see if there's another done # done processing flags, now the command itself: if [ ! -z "$postcmd" ] ; then ls $flags "$@"  $postcmd else ls $flags "$@" fi exit 0 
[DOS flags] directory or directories Where: /D sort by columns /H show help for this shell script /N show long listing format with filenames on right /OD sort by oldest to newest /O-D sort by newest to oldest /P pause after each screenful of information /Q show owner of the file /S recursive listing /W use wide listing format EOF exit 1 } postcmd="" flags="" while [ $# -gt 0 ] do case in /D ) flags="$flags -x" ;; /H ) usage ;; /[NQW] ) flags="$flags -l" ;; /OD ) flags="$flags -rt" ;; /O-D ) flags="$flags -t" ;; /P ) postcmd="more" ;; /S ) flags="$flags -s" ;; *) # unknown flag: probably a dir specifier break; # so let's get outta the while loop esac shift # processed flag, let's see if there's another done # done processing flags, now the command itself: if [ ! -z "$postcmd" ] ; then ls $flags "$@" $postcmd else ls $flags "$@" fi exit 0

How It Works

This script highlights the fact that shell case statements are actually regular expressions, which is a useful characteristic. You can see that the DOS flags /N , /Q , and /W all map to the same -l Unix flag in the final invocation of the ls command.

Ideally, users would be taught the syntax and options of the Unix environment, but that's not always necessary or desired. Of course, an interim step could be to have this script echo the ls command with all of the mapped flags before actually invoking it. Alternatively, you could have this script map the command and then output some message like Please use ls -l instead .

Running the Code

Name this script DIR , and whenever users type DIR at the command line with typical MS-DOS DIR flags, they'll get meaningful and useful output rather than a command not found error message.

The Results

 $  DIR /OD /S /Volumes/110GB/  total 60680     0 WEBSITES                        64 Desktop DB     0 Writing                          0 Temporary Items     0 Microsoft Office X           29648 Norton FS Volume 2     0 Documents                    29648 Norton FS Volume     0 TheVolumeSettingsFolder          0 iTunes Library     0 Trash                            8 Norton FS Index   816 Norton FS Data                   0 Desktop Folder   496 Desktop DF                       0 Desktop Picture Archive 

This listing of the specified directory is sorted from oldest to newest and has file sizes indicated (directories always have a size of 0).




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net