CSE80 -- Lecture 4, Apr 25 -- Test and Debug Shell Script


Test and Debug a Shell Program:

After you write a shell script, you should test it to make sure it works. One good way of doing so is to do:
$ sh -xv program_name 2>&1 | more
"-xv" switch tells shell to display and execute the shell script one line at a time. As it is executing, it will also show the actual command being executed (leading by a plus sign). So, you will know exactly if the shell script did what you want it to do.

2>&1 is applied to the sh -xv program_name process, and it dups stderr from stdout. The stdout was already referring to the input end of the pipe, so both the standard error and standard output of the sh will get written to the pipe. If 2>&1 is omitted, the standard output will still be displayed one page at a time by more, but the standard error message will be randomly written to the screen.

The corresponding notation for the C-shell is

% sh -xv program_name |& more

back forward


[ CSE 80 | ACS home | CSE home | CSE calendar | bsy's home page ]
picture of bsy

bsy@cse.ucsd.edu, last updated Thu May 23 13:04:14 PDT 1996.

email bsy