Linux Learning-standard input and output

Source: Internet
Author: User

standard input and output redirection-- any command executed under Linux will go through the process shown:

Directory

    1. Three types of I/O devices

    2. Redirect I/O to a file

    3. Tr

    4. Using the Pipe link command

    5. Practical cases


I. I/O devices

    • Three types of I/O devices are available in Linux:

Standard input (STDIN)-0 accepts input from the keyboard by default

Standard output (STDOUT)-1 Default output to terminal window

Standard error (STDERR)-2 default output to terminal window

    • Operation Symbols:

1> or > correct output to file

2> error Output to file

> cover

2>> Append

file exists:> and >> can create new file

file does not exist:> overwrite >> append


set-c is forbidden, but can be appended

>|+ file name Force Overwrite

Set +c Allow overwrite

&> All output Files

2>&1 Turn the wrong into the right

1>&2 Turn the right into the wrong

() merging output from multiple programs

Example: (Cal 2007;cal) > All.text

    • TR Conversion characters

For example:

tr ABCD 123 converts a conversion of A to 1,b to 2,CD to 3

TR ABC 1234 converts a to 1,b converted to 2,c to 3, 4 invalid

tr-t ABCD 123 will a->1,b->2,c->3,d invalid

tr-d A removes the A string

echo Hello 123 World | tr-cd 0-9 only leaves the 0-9, Deleting all characters in the input text that are not in the complement (a bit around) /c16>

Run results

tr-s ab converts multiple consecutive A and b into a and B

TR A [:p UNCT:] will error

    • < input redirection

tr-d Abd </etc/fstab Delete any of the ABC characters in the Fstab file

cat > File then enter the content, Ctrl+d left in a disguised form of an input

cat > File3.txt </etc/fstab Copy the contents of Fstab to File3.txt

Video 56.03 screenshot

    • << Termination Words ****** is used in scripts ******

cat > File4.txt << EOF

Video Screen 57.19 screenshot


    • | piping  -- Pass the correct result of the front output to the next command to execute

can take advantage 2>&1 | error results can also be passed to the pipeline

Less paging to view output

ls-l/etc |

mail-s "subject" + username --e-mail sent to the specified user

User Lisi send the subject as test, the content is Hello World mail to the user Zhanshan, the result diagram is as follows:

Combined with pipe breaks:

echo Hello World | mail-s "Test" +username


    • Case:

1. Convert the contents of the/etc/issue file to uppercase and save to the/tmp/issue.out file

cat/etc/issue |tr A-z >/tmp/issue.out

TR A-Z </etc/issue >/tmp/issue.out

2. Convert the current system login user's information to uppercase and save to the/tmp/who.out file

W.H.O. | tr [A-z] [a-z] >/tmp/who.out

3, a Linux user to the root email, request the message titled "Help", the message body is as follows:

Hello, I am Username, the system version is Here,please help me to Check it, thanks+ operating system version information

ECHO-E hello,i am ' whoami ', the system version is Here,please help me to check it,thanks. " \ n "' Cat/etc/redhat-release ' | Mail-s Help Root

4, the/root/file list, displayed as a line, and the file name separated by a space

Ls-1/root/|tr "\ n" ""

5. Calculation 1+2+3+. The sum of +99+100

echo {1..100} |tr "" + |BC

6, processing string "Xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4", keep only the numbers and Spaces

echo "Xt.,l 1 jr#hat-releasemn 2 c*/fe 3 uz 4" | TR-CD [:d Igit:][:blank:]

7. Display the path variable in a separate row for each directory

echo $PATH |tr: "\ n"


Linux Learning-standard input and output

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.