Linux-shell Input and Output __linux

Source: Internet
Author: User
Tags control characters echo command

http://fhqdddddd.blog.163.com/blog/static/186991542012417104144188/

Echo
Use the echo command to display a line of text or a variable, or to enter a string into a file. Its general form is:
echo string
The echo command has many features, the most common of which are the following:
\c do not wrap.
\f into the paper.
\ t jumps.
\ n Line Wrap.

When using escape characters in Linux, you need to use the-e parameter, otherwise the escape character does not take effect
# ECHO-E, AAA, Triple-BBB.
Aaa
Bbb
# echo, Triple-A-BBB.
AAA \ BBB

You can output system variables with ECHO, or the results of system commands
# echo ' My home is: $HOME '
My home is:/root

# echo ' The time is: ' Date '
The time Is:sun June 13:03:47 CST 2010

Note that the symbols on either side of the date command are not single quotes.

The quotation mark is a special character, so you must use the backslash \ to make the shell ignore its special meaning
# echo ' \ '/dev/rmt0″\ '
"/dev/rmt0″

Read
Use the r e a D statement to read information from a line of text in a keyboard or file and assign it to a variable. If only one variable is specified, then r e a D

All input will be assigned to the variable until the first file terminator or carriage return is encountered.

Read Varible1 varible2 ...
For example:
# read FirstName LastName
Arthur MR Liu
# echo $firstname
Arthur
# echo $lastname
MR Liu

Cat

Cat is a simple and generic command that you can use to display the contents of a file, create a file, and use it to display control characters.

The general form of CAT commands is:
Cat [Options] filename1 ... filename2 ...
The most useful options for cat commands are:
-V Display control characters
Show Files
$ Cat MyFile
Create a file
$ cat > MyFile
End Press < C T R l–d >

Merging files
$ cat Myfile1 myfile2 myfile3 > Bigfile
Pipeline
You can pass the output of one command to another command as input through a pipe. The pipe is represented by a vertical bar.

Command 1 | command 2
where | is the pipe symbol.

SED, awk, and grep are all good for piping, especially in a simple line of commands. In the following example, the output of the WHO command is piped to the awk command to display only the user name and the terminal in which it resides.
# who | awk ' {print $1″\t ' $} '
Root PTS/1

# Df-k | awk ' {print $} ' | Grep-v "FileSystem"
/dev/mapper/volgroup00-logvol00
8030648
/dev/sda1
Tmpfs
Tee
The T e command function can be represented graphically by the letter T, i.e. the tee in the water pipe.
# who | Tee who.out
Root PTS/1 2010-06-13 11:45 (192.168.164.1)
# Cat Who.out
Root PTS/1 2010-06-13 11:45 (192.168.164.1)

standard input and output
Input file-standard input 0
Output file-standard Output 1
Error output file-standard error 2

Why there is a special file specifically for the error. This is because a lot of people like to save the error individually to
A file, especially when working with large data files, can cause a lot of errors.

Command > FileName redirect standard output to a new file
Command >> filename REDIRECT standard output to a file (append)
Command 1 > Fielname redirect standard output to a file
Command > FileName 2>&1 redirects standard output to a file with standard errors
Command 2 > FileName redirects standard errors to a file
Command 2 >> filename redirects standard output to a file (append)
Command >> filename 2>&1 redirects the standard output to a file (append) with the standard error
Command < filename >filename2 the command command with the f i l e n a m e file as the standard input, with F i l e n a m e 2 file for standard output
Command < filename the command command with the f i l e n a m e file as the standard input
Command << delimiter reads from standard input until it encounters the D e l i m t e R delimiter
Command <&m the file descriptor m as the standard input
Command >&m redirects standard output to file descriptor M
Command <&-turn off the standard input

Cat passwd | Awk-f: ' {print $} ' | Sort >sort.out

Root will receive a message containing the contents of the file Contents.txt:
#mail Root < Contents.txt

Redirection operator Command << delimiter is a very useful command, which is often referred to as the "here" text block.
The shell takes the identifier D e l m i t e R and all the contents before the next same delimiter as input, encounters the next delimiter,

The shell knows the input is over. # cat >> myfile1 << EOF
> AAA
> BBB
> CCC
> EOF
> EOF
# Cat Myfile1
Aaa
Bbb
Ccc
Eof
# grep "Standard" > Grep.out 2>&1
In the above example, the standard output and standard errors of the grep command are redirected to the Grep.out file
Exec
The EXEC command can be used to override the current shell
A common use of the EXEC command is to use it to perform some scripts for enhanced security when the user's. Profile is last executed.

File Descriptor
Can be understood as the code of the file

# VI Readtest
EXEC 4<&0 0<myfile1
Read line1
Read Line2
EXEC 0<&4
echo $line 1
Echo $line 2

# Cat Myfile1
Aaa
Bbb
Ccc
Eof

# chmod 744 readtest
#./readtest
Aaa
Bbb

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.