search
HomeOperation and MaintenanceLinux Operation and MaintenanceExamples of input and output, redirection, and pipelines

1, Input and output, redirection, pipeline
2, (cmd)
3、>>;
1. Input and output, Redirection, pipe
1. Linux programs have three standard input and output, which are:
Standard input, represented by the number 0, By default, it is connected to the keyboard. The program is usually used to obtain user input.
Standard output, represented by the number 1, is connected to the screen by default. The program is usually used to output information.
Standard error, represented by the number 2, is connected to the screen by default. Programs are usually used to output error messages
Analysis:
Command passwd reads the user password from the standard input, so the user can use the keyboard to enter the password The echo command outputs the result to the standard output, so the result can be seen on the screen
Ordinary users do not have permission to view /root directory, so an error occurred, so ls outputs the error message to the standard error, that is, screens
2, > and >>
> Delete the original data and write new data
##>> Append the new data after the original data
##3, /dev /null
##Commonly known as: trash can file
# echo hello world > /dev/null --The data redirected to this file will be redirected by the kernel Lost it, you can use this feature to block certain output information
# ls / /root 2> /dev/null --Shield the error message of the command
4. Output Redirection
Users can modify the default input and output direction as needed
# ls -l > test
# ls -l 1> test --Both items redirect standard output to the test file, that is, the default 1 can be omitted
# ls /jjjj 2> test --Redirect standard error to the test file
# ls / /jjjj 1> stdout 2> stderr --Redirect standard output and standard error to two different files
# ls /jjjj / > std 2>&1 -- Merge standard error into standard output and redirect it to a file
# ls /jjjj / &> std -- Result and previous entry The statements are equivalent and more concise
# ls /jjjj / &>> std -- also redirects standard output and standard error to a file
5, standard input Redirection
 # cat 0
 # cat
 # cat /etc/passwd --cat command The standard input comes from
6 and pipe
in the file /etc/passwd. The redirection described above connects the standard input and output to the file. We can also connect the standard input and output between multiple programs to transfer data between commands. This technology is vividly called pipeline. The output of the program is like running water flowing in the pipe. Same, flow from the leftmost program to the rightmost program. Pipes are a very common technology in the Linux shell. Using pipe technology, multiple small tools can be used together to complete very complex and powerful functions.
# cat /etc/passwd | head -n 3 --The standard output of the command on the left is used as the standard input of the command on the right
# cat /etc/passwd | head -n 3 | tail -n 1
7. In-depth discussion of standard input and output
The standard input and output are three created by Linux for programs by default. File descriptors. Although most programs will use these three file descriptors as their own input and output, this is not mandatory. That is to say, the program does not need to use these three standard file descriptors. It is to open a new file descriptor for use. When the program does not use standard input and output, the redirection used here will not work for it. For example:
passwd: The program reads the user password from standard input, so we can provide it with the password by redirecting standard input
ssh: The command is a program used for remote login. When it reads the password entered by the user, it does not use the standard input, so we cannot provide the password to it by redirecting the standard input
8. Verify the files connected to the standard input and output of the process
# sleep 999 1> /tmp/good 2>&1 --Run the command in a terminal
# pgrep -x sleep --In another terminal, find the process ID of sleep
# lsof -anop 16715 -d 0,1,2 --View the files opened by the process
9. Verify the timing of redirection
# vim file
# cat -n file > file --expect cat to add a line number in front of each line , and then redirect and save the result back to the original file file
# cat file -- is empty
Cause: Before the shell executes the command, it will first perform a redirection operation. In the second command, it first executes > file. This redirection operation will clear the contents of the file file. After the redirect operation clears the file file, and then cat the file, you will naturally not see any content.
$ sudo ls /root > /root/ls.log --In this example , the user does not have the opportunity to enter the password, because the redirection operation is performed first, the current user is an ordinary user, and the file cannot be created in the path /root/ls.log. The shell errors and exits: bash: /root/ls.log: Permission denied. At this time, the sudo command has not been executed, so there is no chance to enter the password
2. (cmd)
1,
Redirect standard input by process replacement
2, >(cmd)
Use process replacement to redirect standard output
3. Process replacement
The standard output of the process is stored in a temporary file and returned to the temporary file. The path of
# paste
# paste
3.>;>;>>;
##1, > --Output redirection, create (overwrite if existing)
# echo 'hello world' > test -- If test does not exist, it will be created. If it exists, it will overwrite the content inside.
2.
# less
# cat
# wc -l
# wc -l
3. >> --Redirect to the file and create (add to the end of the file if it exists)
# echo 'hello george' >> george - - If george does not exist, create it, if it exists, append the content to the end of the file
4,
4.1. Here Document
is a
special redirection method in Linux Shell. Its basic form is as follows:
cmd
Here Document Content
delimiter
:
Here Document Content
delimiter
Function: Pass the content between two delimiters (Here Document Content) to cmd as an input parameter
4.2. Terminal
# cat
> one
> two
> ; three
> EOF
EOF --It's just a logo and can be replaced with any legal character
##> - - This symbol is the identifier generated by the terminal to prompt for input information
delimiter -- it must be written in the top format, and there cannot be any characters before and after it, including spaces
4.3. shell
# vim here.sh --
Note: You can also use variables in it
#!/bin/bash
cat output.sh
echo "hello"
echo "world"
echo $1
EOF
# chmod a+x here.sh
# ./here.sh george
# cat output.sh --View the contents; here $1 is expanded Becomes a parameter of the script
Note: If you do not want to expand this variable, you need to use double quotes to enclose the first EOF.
## 4.4. Here Document Another usage is to change '
5, # wc -l
# while read x; do echo "hello";done
# bc
# vim string.sh
#!/bin/bash
while read line
do
if [ "${line#ftp :}" != "$line" ];then
awk -F: '{print $6}'
break
fi
done
Comment: Loop through each line in the /etc/passwd file. If it is an ftp user, print out its home directory and Exit the loop
${line#ftp:}: The beginning of a line matches ftp:; then only the unmatched part of the line is taken
# chmod a +x string.sh
# ./string.sh
4. Text processing_1: cat;head;tail;cut;wc;sort ;uniq;tr;tac;rev
Text processing is a task that every system administrator will frequently come into contact with. Its core content is the use of related tools. The key point is to flexibly combine multiple tools to Complete the task
1. cat --concatinate, concatenate the contents of one or more files in order, and output to the standard output
# cat -n /etc /passwd --Display the file content and add the line number
# cat -A /etc/passwd --Print out some invisible characters and position marks
# cat 1.txt 2 .txt > test.txt -- Merge files
2, head --Read file header
# head -n 3 /etc/passwd -- Read the first three lines of the file /etc/passwd
# head -n -1 file --discard the last line of the file
# head -c 3 /etc/passwd --read The first three bytes of the file /etc/passwd
# head -c -3 file --discard the last three bytes of file
# head -c 10m /dev/urandom > ; big --Create a 10M file
3, tail --Read the tail of the file
# tail -n 3 /etc/passwd -- Read the last three lines of the file /etc/passwd
# tail -n +28 /etc/passwd --read from line 28 until the end of the file; discard the first 27 lines
# tail -c 3 /etc/passwd --Read the last three bytes of the file /etc/passwd
# tail -c +28 /etc/passwd --Read from the 28th byte until the end of the file; discard the 27 bytes of the header
# tail -f /etc/passwd - -Track changes in the content at the end of files, often used to inspect changes in log files, very practical
4. cut --The function is similar to awk, but not as powerful and complex as awk. When you want to manipulate data When doing column output, awk is often used, and cut is rarely used.
Commonly used options:
-d -- Define the delimiter
-b --Output the byte at the specified position
-c --Output the specified position Character (character)
# echo "a;b;c d;e" | cut -d ";" -f1,3,4 -- -d defines the separator (default is TAB); -f defines the output corresponding fields
# cat -n /etc/passwd | cut -d $'\n' -f1,3-5,7 -- Use newline character as separator
# echo I am Chinese | cut -b1-3 -- -b outputs the byte at the specified position; a utf8 Chinese character occupies 3 Bytes
# echo I am Chinese | cut -c2-4 -- -c outputs the character (character) at the specified position; the difference from -b is the processing of non-English character
# echo Be a brave Chinese | cut -b1-2,9 -- will output a "fake" character
# echo -n Be a brave Chinese people | xxd -- will find that the three bytes 1, 2, 9 are: e581 87
# echo -n false | xxd -- and "false" is also
5, wc -- Calculate the number of bytes, characters, words, and lines of data
Common options:
-c --Calculate the number of bytes
-m --Calculate the number of characters
-w --Calculate the number of words
-l -- Calculate the number of lines
# echo -n I am Chinese | wc -c -- -c Calculate the number of bytes, 5 utf8 Chinese account for 15 Bytes
# echo -n I am Chinese | wc -m -- -m counts the number of characters. The difference from -c is when processing non-English characters, similar to the command cut
# echo -n I am Chinese | wc -w -- -w counts the number of words, there is no separator to separate them, 5 Chinese characters count as one word, which is different from the so-called "words" in Chinese
# echo -n Uppercase CHINESE | wc -w --Two words
# echo -n Uppercase CHINESE | wc -c --17 English bytes, 18 without -n (because there are a newline character)
# echo -n Uppercase CHINESE | wc -m --17 English characters, 18 without -n (because there is a newline character)
# wc -l /etc/passwd - - -l Count the number of lines
6, sort -- Sort files by lines
Common options:
-t --Specify the delimiter
-k --Specify the sorting field
-u --Remove duplicate rows
-n, -h --Sort by value
-r --Sort in reverse
# cut -d ":" -f7 /etc/passwd | sort -u -- -u remove duplicate lines
# echo -e "1\n2\n10" | sort
# echo -e "1\n2\n10" | sort -n -- -n sorts by numerical value, cannot handle unit characters such as K, M, G etc.
# ls -lh | tail -n +2 | sort -k5,5n -- -k specifies the sorted field
# ls -lh | tail -n +2 | sort -k5,5h -- -h When sorting by numerical value, you can Process unit characters such as K, M, G
# head -4 /etc/passwd | sort -t: -k7,7 -- -t uses colon: as the field separator, press 7 Field sorting
# head -4 /etc/passwd | sort -t: -k7,7 -k3,3n --First sort by the 7th field, if the 7th field has the same, then Sort by field 3
# echo -e "1\n2\n3" | sort -nr -- -r reverse the sort
# head -4 /etc/passwd | sort -t: -k7,7 -k3,3nr --To reverse the sorting of the third field, you can also reverse the two fields at the same time
7, uniq --Remove consecutive duplicate lines
Commonly used options:
-c --Count the number of duplicate rows
# echo -e "1 \n1\n2\n1" | uniq -- There are still two 1's in the result because the two 1's are discontinuous
# echo -e "1\n1\n2\n1" | sort - u -- sort removes duplicates without consecutive
# cut -d: -f7 /etc/passwd | sort | uniq -c -- Based on the sort command example, count the number of occurrences of different login shells
8, tr - Convert, delete, reduce the same characters
Common options:
-d --Delete, delete all matching letters
-s --Reduce, reduce the same characters
Format: tr SET1 SET2
Note: Convert the characters in set 1 to the characters in the corresponding position in set 2, so in principle, the characters in the two sets The numbers should be the same. However, if the number of characters in the two sets is not equal, the program will not error. Please pay attention to the results in this case. Points:
a. tr does not care what characters are in the two sets, it just simply replaces the characters at the corresponding positions one by one.
b. tr replaces a single character, not a string
# echo abc | tr a-z A-Z --Convert 26 lowercase letters into corresponding uppercase letters
# echo abc | tr ab BA -- Convert a and b to B and A respectively
# echo Good people do good things | tr Good or bad -- "good" is converted to "bad"
# echo abcdefg | tr a-z AB --Set 2 is shorter, the program automatically uses the last character in set 2 to extend set 2
# echo abcdefg | tr a-b A-Z --Set 2 is longer, the program automatically extends set 2 Cut short
# echo Abc | tr a-zA-Z A-Za-z --Reverse the size of English letters
# echo hello world | tr -d ow -- -d delete, all matching letters
# echo 0123456789 | tr -d 13579
# tr -d '\012'
# echo aabbaacc | tr -s a -- -s, reduce each connected a to one
# echo aabbaacc | tr -s a A -- do the conversion after aggregation
9. tac -- concatenate the contents of one or more files in order and output them to the standard output. In each file, the contents are printed in reverse order by line number
# echo -e "111111111\n2222222" > f1
# echo -e "333333333\n4444444" > f2
# tac f2 f1
10, rev --reverse the lines in the file
# echo -e "1234567\nabcdefg "> test
# rev test
5. Extension
1. cat, md5sum
# echo file1 > file1
# echo file2 > file2 --Create two files
# md5sum file1 file2 --Compare whether their md5 values ​​are the same
# head -c 10m /dev/urandom > bigfile --Create a 10M file using a random device
# head -c 3m bigfile > file1 --Import the first 3M data into file1
# tail -c 4m bigfile > file3 --Import the last 3M data into file3
# head -c 6m bigfile | tail -c 3m > file2 --Import the intermediate 4M data into file2
# ls -lh file*
# cat file1 file2 file3 > newbigfile --Use cat merges three files into a new big file
# md5sum newbigfile bigfile --Compare the md5sum value of the old big file and the new big file using the command

The above is the detailed content of Examples of input and output, redirection, and pipelines. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Debian mail server firewall configuration tipsDebian mail server firewall configuration tipsApr 13, 2025 am 11:42 AM

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

Debian mail server SSL certificate installation methodDebian mail server SSL certificate installation methodApr 13, 2025 am 11:39 AM

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version