CS 497C – Introduction to UNIX Lecture 21: - The Shell Chin-Chih Chang
Published byModified over 6 years ago
Presentation on theme: "CS 497C – Introduction to UNIX Lecture 21: - The Shell Chin-Chih Chang"— Presentation transcript:
CS 497C – Introduction to UNIX Lecture 21: - The Shell Chin-Chih Chang email@example.com firstname.lastname@example.org
Redirection (Standard Output) Using the symbols > and >>, you can redirect the output to a disk file. For example, when we issue who > new- file, the shell opens the file new-file, writes the stream into it and the closes the file. Using the > symbol will overwrite the existing file. To append to a file, use the >> symbols: who >> new-file
Redirection (Standard Output) Redirection also becomes a useful feature when concatenating the standard output of a number of files. cat chap?? > textbook Two or more commands can be combined and their aggregate output can be redirected to a file. A pair of parentheses groups the commands, and a single > symbol can be used to redirect both of them: (ls -l ; who) > lsfile
Redirection (Standard Output) You can redirect a message to the terminal /dev/pts/6 provided the terminal is enabled accordingly. echo hello > /dev/pts/6 There are three destinations of standard output: terminal (default), file, and pipe. Some commands are designed to take their input as a stream. This stream represents the standard input to a command.
Redirection (Standard Input) The wc command expects input from the keyboard when the filename is omitted: $ wc This is a test of the wc comand. It reads the input from the standard input. 2 16 77
Redirection (Standard Input) The standard input stream has three sources: the keyboard (default), a file using redirection with <, and another program using a pipeline. You can make calculations in a batch as shown in the following example: $ cat calc.lst 2^32 25*50
Redirection (Standard Input) $ bc result.lst $ cat result.lst 4294967296 1250 When a command takes input from multiple resources – say a file and standard input, the – symbol must be used to indicate the sequence of taking the input.
Redirection (Standard Error) For example. cat - foo indicates first reading from standard input and then from foo. Each of three standard files has a number, called a file descriptor: –0 - standard input: < or 0< –1 - standard output: > or 1> –2 - standard error: 2> You need to use the descriptor 2> for the standard error: $ cat bar 2>errorfile
Redirection (Combining Streams) In C Shell, use >& for the standard error. The following Stream combinations are equivalent: wc newfile wc > newfile < infile > newfile < infile wc The standard output and error symbols can also used in the same command line: cat newfile nofile 2> errorfile > outfile