divzeroweb

2f30.org website
git clone git://git.2f30.org/divzeroweb
Log | Files | Refs | README | LICENSE

lecture1.shtml (6520B)


      1 <h3>Meeting the $SHELL</h3>
      2 
      3 <h4>What is a shell anyway?</h4>
      4 
      5 <p>
      6 Well a shell is just a program, but a very special one...
      7 It is the primary method of interfacing a computer with you, the user.
      8 Let's think for a moment an analogue in everyday life...
      9 Imagine a car.  That's a hell of complicated machine.
     10 It has all sorts of stuff in it.
     11 But an everyday driver, only needs some parts of it in order to drive .
     12 These are the wheel, the pedals, the gearshift and the display.
     13 Using only these four, you harness the full power of a modern car.
     14 This common setup, completely hides the differences between each car.
     15 Therefore it is a method of <em>INTERFACING</em>.
     16 You get that face in every car without having to know about engine details,
     17 manufacturer tricks and dead bodies in the trunk ;). 
     18 Well a shell acts in a similar way.  It presents you with an input field
     19 where you can enter the names of commands to be run (programs) and commands
     20 to the shell (the running program).  This second option, implies the art of
     21 <em>SHELL SCRIPTING</em>.
     22 The method of programming the way the shell behaves.
     23 </p>
     24 
     25 <h4>Types of shells</h4>
     26 <p>
     27 If the shell is such an important program,
     28 why haven't you seen one in Windows?
     29 Well you have.  Based on our definition "Windows Explorer" is kind of a shell.
     30 Meaning it let's you execute basic operations (copy, delete, rename) on files.
     31 But it would be a shame to call this idiotic thing a shell.
     32 So let's move on to some real ones ;).
     33 UNIX usually has many shells to offer to the user,
     34 letting him choose the one that he prefers.
     35 But for everyday usage, all act with the same UNIXy way.
     36 They execute commands, redirect I/O (input/output) and are programmable.
     37 The most widely used shells in UNIX currently are:
     38 </p>
     39 <ul>
     40 <li>
     41 <em>C Shell</em> (csh, tcsh).
     42 The C shell was developed by Bill Joy, the creator of the vi text editor,
     43 at the University of California, Berkeley.He based parts of the syntax 
     44 of the C programming language.
     45 That's why the C shell is beautiful to write shell scripts in.
     46 Specially if you are fluent in writing some C code.
     47 </li>
     48 <li>
     49 <em>Bourne Shell</em> (sh, bash).
     50 This was the first shell written.  It was was coded at Bell Labs
     51 by Stephen Bourne.  Usually it is the standard shell a system ships with.
     52 </li>
     53 <li>
     54 <em>Korn Shell</em> (ksh).
     55 Having nothing to do with the band KORN, ksh was written by David Korn
     56 who was again in Bell Labs, during the early 80s.
     57 It is the standard shell of OpenBSD :).
     58 </li>
     59 </ul>
     60 <p>
     61 To find out what kind of shell you are using in your UNIX system, you can run:
     62 </p>
     63 <pre><code>% env | grep SHELL
     64 </code></pre>
     65 
     66 <h4>Redirection</h4>
     67 <p>
     68 Most processes initiated by UNIX commands write to the standard output
     69 (the terminal screen) and many take their input from the standard input
     70 (the keyboard).  There is also the standard error where processes write
     71 their error messages, by default, the terminal screen.
     72 Remember the cat command that writes the contents of a file to the screen.
     73 <code>cat myfile</code> prints the contents of myfile.
     74 Now write the cat command without any argument.
     75 Then type a few words on the keyboard and press the <kbd>Return</kbd> key.
     76 Finally hold the <kbd>Ctrl</kbd> key down and press <kbd>d</kbd>
     77 (written as <kbd>^D</kbd> for short) to end the input.
     78 What has happened?
     79 If you run the cat command without specifying a file to read,
     80 it reads the standard input (the keyboard), and on receiving
     81 the "end of file" (<kbd>^D</kbd>), copies it to the
     82 standard output (the screen).
     83 </p>
     84 <p>
     85 In UNIX, we can redirect both the input and the output of commands.
     86 For output redirection we use the > operator.
     87 For example, to create a file called animalist1
     88 containing a list of animals type:
     89 <pre><code>% cat > animalist1
     90 chicken
     91 cow 
     92 moose
     93 ^D
     94 </code></pre>
     95 <p>Press <kbd>Return</kbd> after each animal name
     96 (<kbd>Ctrl</kbd> <kbd>D</kbd> to stop).
     97 </p>
     98 <p>
     99 What happens is the cat command reads the standard input (the keyboard)
    100 and the > redirects cat's output, which normally goes to the screen,
    101 into a file called animalist1.  To read it's contents type:
    102 </p>
    103 <pre><code>% cat animalist1
    104 </code></pre>
    105 <p>
    106 For input redirection we use the < operator.
    107 For example take the command sort which alphabetically or numerically
    108 sorts a list.  Type:
    109 </p>
    110 <pre><code>% sort
    111 goths
    112 vikings
    113 windows users
    114 visigoths
    115 ostrogoths
    116 ^D
    117 </code></pre>
    118 <p>
    119 Type in the names of some barbaric tribes.
    120 Press <kbd>Return</kbd> after each one.
    121 The output will be:
    122 </p>
    123 <pre><code>goths
    124 ostrogoths
    125 vikings
    126 visigoths
    127 windows users
    128 </code></pre>
    129 <p>
    130 As usual, Windows users are the lowest of them all, even alphabetically.
    131 Using < you can redirect the input to come from a file rather than
    132 the keyboard.  For example, to sort the list of barbarians,
    133 that you now have in a file called barblist type:
    134 </p>
    135 <pre><code>% sort < barblist
    136 </code></pre>
    137 <p>
    138 The sorted list will be output to the screen.
    139 To output the sorted list to a file, type:
    140 </p>
    141 <pre><code>% sort < barblist > barblist-sorted
    142 </code></pre>
    143 <p>
    144 The final ace up our sleeves is the | symbol (PIPE).
    145 This takes the output of one command and sends it as an input to the other.
    146 Examples:
    147 </p>
    148 <p>Print the number of files in the current directory.</p>
    149 <pre><code>% ls | wc -l
    150 </code></pre>
    151 <p>Print those file names containing the string old.</p>
    152 <pre><code>% ls | grep old
    153 </code></pre>
    154 
    155 <b>A taste of things to come</b>
    156 <p>
    157 Write the following script into a file (name it script.sh),
    158 then run <code>chmod +x ./script.sh</code>.
    159 This gives to the file script.sh permission to be executed
    160 (<code>man chmod</code> for info).
    161 <code>./script.sh</code> runs the script (notice the <em>./</em>).
    162 </p>
    163 <pre><code>#!/bin/sh
    164 count=0
    165 for file in `ls`
    166 do
    167 	count=`expr $count + 1`
    168 	echo "$count: $file" > menu_file
    169 done
    170 echo "Please select a number from this menu"
    171 cat menu_file
    172 read choice
    173 echo "Thanks you chose $choice"
    174 filename=`grep $choice menu_file | cut -f2 -d:`
    175 echo "You chose $filename"
    176 </code></pre>
    177 <p>
    178 Observe this script.  Can you understand what it does?
    179 Try to change the line
    180 <code>echo "$count: $file" > menu_file</code> to 
    181 <code>echo "$count: $file" >> menu_file</code>.
    182 Then run it multiple times...  Can you see the bug? ;) 
    183 <p>
    184 
    185 <p>Finally, some useful links:</p>
    186 <ul>
    187 <li>
    188 <a href="http://www.unix-manuals.com/tutorials/vi/vi-in-10-1.html">
    189 Vi Tutorial</a></li>
    190 <li><a href="http://cb.vu/unixtoolbox.xhtml">
    191 One of the most complete references around</a>.  We will use this a lot.</li>
    192 </ul>
    193 
    194 <p>Until next time... ;)<br>Happy Hacking...</p>
    195 <p>dsp@</p>