CSE 80 -- Lecture 5 -- Jan 21


Assignment 2: fix dumbshell.c so that it prints the correct exit status information. Due before class next Tuesday. To do this, you need to understand what the wait system call does.

Magic numbers. Executables have headers, which start with special values called magic numbers. These numbers are part of a convention between compilers/assemblers and the operating system kernel: the magic numbers tell the OS kernel how to load the rest of the file when the executable file is invoked with the exec system call. The first line of a shell script (or more generally, an interpreted script file) must be the characters #!/full/path/to/interpreter -- with shell scripts, the path is typically one of /bin/sh, /bin/csh, or /software/common/gnu/bin/bash -- where the character sequence #! serve as a magic number, indicating to the kernel that the file should be interpreted as a script file and that immediately following the #! is the name of the interpreter which should read the file and interpret the contents as commands. (Note that for all the standard shells, # is how comments are started, so the first line is read by the shells as a comment.)

$#, $?, $*, $@.

$#
the number of arguments to a shell script.
$?
the exit status of the last process executed.
$*
all the arguments to the script.
$@
all the arguments to the script.
The two notation $* and $@ are virtually identical, with the only difference in what happens when they are quoted: when the shell sees "$*", the shell expands it to a single argument containing all the shell script's arguments, each separated from the next by a single space; when the shell sees "$@", the shell expands it to $# separate arguments.

Control flow constructs. while, until, if constructs, which controls execution by examining the exit status of commands. The case statement, which performs pattern matching, filename style.

test, and grep. at. Alarm clock example script because I just moved and can't find my alarm clock in the packing boxes:

$ cat > beep.me
#!/bin/sh
while test ! -f $HOME/.stop
do
	echo '\a\a\aWake up!\a\a\a\r\c' >> /dev/console
done
^D
$ chmod +x beep.me
$ at 0830
beep.me
^D
$
(This relies on the fact that I stay logged in on the console over night.)
[ CSE 80 | ACS home | CSE home | CSE calendar | bsy's home page ]
picture of bsy

bsy@cse.ucsd.edu, last updated Tue Mar 18 15:49:27 PST 1997.

email bsy