I/O and pipelines under Linux

Source: Internet
Author: User

Read Catalogue

    • 1. Standard input and output

    • 2. Input and output redirection

    • 3.tr command

    • 4. Piping and Tee

    • 5. Practice



1. Standard input and output

(1) Linux provides three kinds of I/O devices to the program

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

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

Standard error: (Stdeer)-2 default output to terminal window


(2) Input device: keyboard, mouse, scanner, camera, etc.

Output devices are: printers, monitors, projectors, etc.


2. Input/Output redirection

Correct output

2〉 Error Output

&〉 full output (overwrite redirect)

> file content will be overwritten

SET-C: Prevents overwriting of existing files, but can append

>| File: Force overwrite

Set +C allows overwriting

>> additional content based on the original content

The file does not exist when,> and >> can create a new file, the file already exists > represents an overlay,>> represents an append

2>&1 turns the wrong result into the right result

1>&2 turn the right result into the wrong result

(): Merging stdout of multiple programs

Cal 2007;cal 2008

(Cal; cal) >all.txt

** 3, tr command   

TR Convert and delete characters

TR [OPTION] .... Set1[set2]


Options:

-c-c--complement: Taking the complement of the character set

-D--delete: Delete all characters belonging to the first character set

- s--squeeze-repeats: repeats consecutive characters in a single character

- t--truncate-set1: Converts the first character set corresponding character to the character corresponding to the second character set


import STDIN from a file

(1) Use < to redirect standard input

(2) Some commands can accept STDIN imported from a file

Tr'A- z' 'A- z'</etc/issue This command converts lowercase characters in /etc/issue to uppercase characters


(3) tr-d ABC </etc/fstab Delete all any character in the 11>ABC

(4) Cat > file

Mage

Wangxiaochun

Press Ctrl+d to leave, you can use the file instead of the keyboard input


Use "<< stop words" to redirect multiple lines to STDIN


4. Piping and Tee

use symbols "| "indicates that the command used to connect

Command 1 | Command 2 | Command 3 ...

The ability to combine multiple tools:

ls | Tr ' A-Z ' A-Z

Less: A page-by-page view input ls-l/etc | Less

Mail: Email Send input echo "Test Email" | Mail-s "Test" [email protected]


Tee: Read from standard input, write to standard output, and save to file



5. Practice

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

echo 1+2|bc echo {1..100} echo {1...100} |tr "" +|bc

2, a Linux user to the root email, request the message titled "Help", the message body is as follows: Hello, I am user name, 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

3. Convert the current system login user's information to uppercase and save to the/tmp/who.out file
Who | tr [A-z] [a-z] >/tmp/who.out

4. 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

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"

















I/O and pipelines under Linux

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.