3.5. Redirecting Input and Output

Most shell utilities write their output to the screen. However, under GNO/ME, like ORCA, it is possible to redirect that output to a file or a GS/OS device. The output of the ls command above was imported into this manual by redirecting it to a file. In addition to redirecting the output of a shell utility, it is also possible to redirect the input of that utility. Consider the following gsh session:


[1]% echo this is a test
this is a test
[2]% echo this is a test > file1
[3]% cat file1
this is a test
[4]% cat < file1
this is a test

In the example above, cat takes input from "standard input". In command 3 above, cat takes as an argument the filename file1 and writes the contents of that file to "standard output". Where no filename argument is given, cat reads input from standard input and writes the output to standard output.

In the case of command 4 above, cat contains no arguments and therefore reads from standard input. However, gsh interprets the "<" redirection operator and opens the file file1 for use as standard input. Therefore, cat will take its input from file1, even though it thinks it is reading input from standard input. This input redirection is transparent to the utility, making it work with most shell utilities.

Command 2 above created a new file called file1. If this file had existed prior to the command then it would have been erased. It is possible to append output to the end of the file by using the ">>" redirection operator. Consider the following gsh session:


[5]% echo second line >> file1
[6]% cat file1
this is a test
second line

Output that is sent to "standard error", can also be redirected. The ">&" operator redirects standard error to a file and ">>&" appends standard error to the end of the file. Below is a summary of the redirection operators:

Table 3-1. GSH Redirection Operators

 stdinstdoutstderr
redirect input from file<  
redirect output to file >>&
redirect output to EOF >>>>&

Output can be redirected to a storage device, printer, modem, or any other valid GNO or GS/OS device. This provides a very powerful means of communicating directly with these devices from within gsh. One quick and dirty example of redirection allows a background version of gsh to be run on a terminal connected directly through the modem serial port:


[1]% gsh < ttya > ttya >& ttya &