Unix shell programming (2)

Source: Internet
Author: User

Unix shell programming (2)

 

Character matching
The asterisk (*) matches more than 0 characters, and the question mark (?) Match 1 character.
For example:
Ls [A-Z] * [0-9]
Displays the list of objects whose names start with lowercase letters and end with digits.

Output redirection
Command output is generally submitted to the standard output device and can be switched to the file. This is called output redirection.
After a standard output command is added, the command output is written to the file after the> file symbol is added.
For example:
[Root @ localhost MISC] # Who
Root pts/1 (10.3.34.117)
Fedora tty7 (: 0)
[Root @ localhost MISC] # Who> Users
[Root @ localhost MISC] # ls
Collect mon users WB wbb2 wbx writeback
[Root @ localhost MISC] # Cat users
Root pts/1 (10.3.34.117)
Fedora tty7 (: 0)

Another type of output redirection is represented by a character>, which means that the output of a command is appended from the standard output to the end of the specified file. Therefore, the previous content of the file will not be lost, and the new output will be added to the end.
For example:
[Root @ localhost MISC] # Cat users
Root pts/1 (10.3.34.117)
Fedora tty7 (: 0)
[Root @ localhost MISC] # ls
Collect mon users WB wbb2 wbx writeback
[Root @ localhost MISC] # ls> Users
[Root @ localhost MISC] # Cat users
Root pts/1 (10.3.34.117)
Fedora tty7 (: 0)
Collect
Mon
Users
WB
Wbb2
Wbx
Writeback

Shell can identify output redirection in a special format, if the input:
> File
If there is no command before, shell will create an empty file (with a length of 0 characters). If the file exists before, its content will be lost.
For example:
[Root @ localhost MISC] #> file4
[Root @ localhost MISC] # WC file4
0 0 0 file4
[Root @ localhost MISC] # Cat file3
This is in file1.
This is in file2.
This is in file1.
[Root @ localhost MISC] #> file3
[Root @ localhost MISC] # WC file3
0 0 0 file3

Input redirection
Same as output redirection, command input can also be redirected to from files. Use a greater than sign> to indicate output redirection, and a smaller than sign to indicate input redirection.
For example:
[Root @ localhost MISC] # WC-l users
9 users
[Root @ localhost MISC] # WC-L <users
9

MPs queue
Count the number of users currently logged on to the system:
[Root @ localhost MISC] # Who> Users
[Root @ localhost MISC] # WC-L <users
2
It indicates that two users have logged on to the system. This command sequence can be used to determine the number of logon users.
There is also a way to determine the number of login users without using files.
UNIX systems can connect the effects of two directories, which are called pipelines.
The pipeline can directly use the output of one command as the input of another command.
Pipeline effect is implemented by character |, which must be between two commands.
Therefore, the number of login users can be calculated as follows:
[Root @ localhost MISC] # Who | WC-l
2
After the pipeline is created between the two commands, the standard output of the first command is directly connected to the standard input of the second command.
For example, count the number of files in the current directory:
[Root @ localhost MISC] # ls | WC-l
11

Filter
UNIX refers to any program that can accept input from standard input and write the result into standard output after processing the input.
Command cat and sort are filters, but who, date, CD, PWD, Echo, RM, MV and CP are not.

Standard Error
In addition to standard input and standard output, there is also a standard device called a standard error.
Standard errors are also terminal errors by default.
For example:
[Root @ localhost MISC] # ls N *
Ls: cannot access N *: no such file or directory
As shown above, "cannot access..." is actually a standard error written by the LS command, rather than a non-standard output.
[Root @ localhost MISC] # ls N *> foo
Ls: cannot access N *: no such file or directory
Although the output is redirected to the file Foo, this information is still displayed on the terminal. And:
[Root @ localhost MISC] # WC foo
0 0 0 foo
The Foo file is empty.
Standard errors can also be redirected to files in the following format:
Command 2> File
Note: there must be no space between 2 and>.
For example:
[Root @ localhost MISC] # ls N * 2> foo
[Root @ localhost MISC] # Cat foo
Ls: cannot access N *: no such file or directory

Multiple commands can be entered in one line. commands are separated by semicolons.
For example, you can view both the current time and the current working directory:
[Root @ localhost MISC] # date; pwd
Tue Apr 14 11:29:15 CST 2009
/Tools/test/Misc

Command sent to the background
Some commands take a long time and can be executed in the background. After the command is entered with an & symbol, the command is sent to the background for execution.
For example:
[Root @ localhost MISC] # Sort file3> out & # Send sort to the background for execution
[2] 30584 # process ID
In this case, the terminal can do other work immediately.
[2] 30584 explanation: the first is the working number of the command, and the second is the process ID number.
You can run the ps command to view the information:
[Root @ localhost MISC] # ps
PID tty time cmd
2303 pts/1 00:00:00 bash
30354 pts/1 00:00:00 sort
30589 pts/1 00:00:00 PS
[2]-done sort file3> out
PS displays information about running processes in the system.
PID process ID, tty process terminal number, time when the process is running, CMD process name.
If the-F option is added, more details are displayed.
[Root @ localhost test] # ps-F
Uid pid ppid C stime tty time cmd
Root 30665 30657 0 00:00:00 pts/2-Bash
Root 30775 30665 0 00:00:00 pts/2 PS-F
Note: ID of the parent ppid process, the start time of the stime process, and command parameters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.