One of the Shell language series: document processing

Recommended for you: Get network issues from WhatsUp Gold. Not end users.


   Standard input / output may be the most basic concepts and principles of software design. There are many UNIX programs follow this design experience. By default, they can read standard input, write to standard output, and the error message to the standard error output.

   This paper is mainly about the read command, redirect, as well as the file name matching.

    This series of articles were written by the author, there are some mistakes or errors, if friends have good suggestions or better algorithms, please generous with your criticism.

The text

Read the line using the read

   The read command is one of the important way to transfer the information to the shell program:


[zhouzhengle@localhost ~]$ x=abc; printf "x is now '%s' Enter new value: " $x; read x
x is now 'abc' Enter new value: PDQ
@localhost ~]$ echo $x


   Read can read all the values to a variable. In this case, in the $IFS character will separate input in the data, which is called the separate words. For example:
printf "Enter name, rank, serial number: "
read name rank serno

   The most typical usage is to handle /etc/passwd files. The standard format for the 7 separated by a colon: Username: password password: numeric user ID: numerical type group ID: full name: the root directory: shell. You can use a cycle of progressive processing simple/etc/passwd:


[zhouzhengle@localhost ~]$ while IFS=: read user pass uid gid fullname homedir shell
> do
> echo $user
> done </etc/passwd



   We already know that the input and output redirection operator basic:, > > and |, here have a look what operator.

   [The use of set -C with POSIX Shell] provides prevent accidental file truncated options: execute the set -C command can open shell so-called banned coverage (noclobber) option. When this option is turned on, the pure > redirect meet target file already exists will fail. >| operators can make the noclobber failure.

   [Provide line input

[zhouzhengle@localhost ~]$ i=5
@localhost ~]$ cat <<EOF
> this is the value of i $i
> Here is a command substitution : $(echo hello, world)
this is the value of i
Here is a command substitution : hello, world

   In the system, UNIX is a small integer numbers, called a file descriptor (File descriptors), said the open file for each process. Digital by the beginning of 0. At least to the system definition open file limit. Traditionally, Shell allows you to directly handle up to 10 open file descriptors, from 0 to 9

   The file descriptor of 0,1 and 2 respectively correspond to the standard input, standard output and the standard error output.
make 1>results 2>ERRS, the standard output to the results, the error output to ERRS.
make 1>results 2>/dev/null, discarded and error messages.
make > results 2>&1, the output and error information is transmitted to the same file.

read name rank serno

Tilde expansion and wildcards

   Shell has two and files related to the. A tilde expansion, called a wildcard expansion.

   ~Tilde expansion purposes, is the symbol of user root said directory path to the actual.
$vi ~/.profile VI $HOME/.profile
$vi # with the same ~tolstoy/.profile # edit user Tolstoy.Profile file

   Looking for special characters in the file name, one of the services provided by Shell. When it finds the character, treats them as the matching pattern.
? Said any single character
* any character in the string
[set] any of the set character in
[! Set] no set characters



   In this paper, the copyright belongs to the author, welcome to reprint, but without the permission of the author must keep this statement, and the connection in the article page is the obvious position, or retain the right to pursue legal responsibilities.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Sean at November 13, 2013 - 1:55 PM