Shell Program Design (1) pipelines and redirection redirect output
Chen123 @ ubuntu :~ /C ++ $ ls-l> lsoutput.txt
This command saves the lscommand output to the lsoutput.txt file.
File descriptor 0 represents the standard input of a program, file descriptor 1 represents the standard output, and file descriptor 2 represents the standard error output. In the preceding example, the standard output is redirected to a file using the> operator. By default, if the file already exists, its content will be overwritten. Use the set-C command to set the noclobber option to prevent the redirection operation from overwriting an existing file. You can also use set + o noclobber or set-o noclobber to enable or disable noclobber.
We use the> operator to append the output content to a file, for example
Chen123 @ ubuntu :~ /C ++ $ ps> lsoutput.txt
This command attaches the ps output to the end of the specified file.
To redirect standard error output, use the 2> operator. Because the standard error output file descriptor is 2, add 2 before>.
The following command redirects the standard output and standard error output to different files.
Chen123 @ ubuntu :~ /C ++ $ kill-HUP 1234> killout.txt 2> killerr.txt
If you want to redirect both sets of output to a file, you can use the> & operator to combine the two outputs.
Chen123 @ ubuntu :~ /C ++ $ kill-l 1234> killouterr.txt 2> & 1
This command redirects the standard output to killouterr.txt and redirects the standard output to the same place as the standard output.
You can directly use the same UNIX recycle bin/dev/null to effectively discard the output information.
Chen123 @ ubuntu :~ /C ++ $ kill-l 1234>/dev/null 2> & 1
Redirect Input
Chen123 @ ubuntu :~ /C ++ $ more <killerr.txt
Obviously, this is not big because more can directly accept the file name as a parameter.
Chen123 @ ubuntu :~ /C ++ $ more killerr.txt
MPs queue
In linux, processes connected through pipelines can run at the same time, and can automatically coordinate with the transmission of data streams between them. For example, we can use the sort command to sort the output of the ps command.
If we do not use pipelines, this requires several steps.
Chen123 @ ubuntu :~ /C ++ $ ps> psout.txt
Chen123 @ ubuntu :~ /C ++ $ sort psout.txt> pssort. out
However, pipelines are more sophisticated.
Chen123 @ ubuntu :~ /C ++ $ ps | sort> pssort1.out
If you want to display the output results by page on the screen, you can link the third process more and place them on the same command line.
Chen123 @ ubuntu :~ /C ++ $ ps | sort | more
If a series of commands need to be executed, the corresponding output file is created or written immediately when the group of commands are created, therefore, you must never reuse the corresponding file name in the Command stream. If you try to execute the following command
Chen123 @ ubuntu :~ /C ++ $ cat mydata.txt | sort | uniq |> mydata.txt
You will eventually get an empty file, because the contents of the file have been overwritten before reading the file mydata.txt.
Shell as a programming language
There are two ways to write shell scripts. You can enter a series of commands for shell interaction to execute them, save these commands to a file, and then call the file as a program.
Interactive Program
Assume that we are looking for a file containing the string hello, you can use the following interactive script to perform the entire operation:
Chen123 @ ubuntu :~ /C ++ $ for file in *
> Do
> If grep-l hello $ file
> Then
> More $ file
> Fi
> Done
Note that when you enter a shell command, the shell prompt $ is changed to the> symbol. You can enter it all the time, and the shell will determine when the input is complete and immediately execute the script program.
Create script
First, create a file named first in the text editor. The content of the file is as follows:
#! /Bin/sh
# First
# This file looks through all the files inthe current
# Directory for the string hello, and thenprints the names
# Those files to the standard output.
For file in *
Do
Ifgrep-q hello $ file
Then
Echo $ file
Fi
Done
Exit0
Where #! The character tells the system that the parameter followed by the same line is used to execute text programs.
The exit command is used to ensure that the program can return a meaningful exit code.
You can run the file command to check whether the file is a script program.
Chen123 @ ubuntu :~ /C ++ $ file first
First: POSIX shell script text executable
Execute scripts
There are two methods to run the script:
Method 1:
Chen123 @ ubuntu :~ /C ++ $/bin/bash first
Method 2:
Chen123 @ ubuntu :~ /C ++ $./first