I. Miscellaneous Knowledge collation
1, the program's data flow has three kinds:
Input data stream, standard input stdin (keyboard)
Output data stream, standard output stdout (display)
Error output stream, standard output stderr (display)
FD file descriptor: Standard input 0 standard output 1 Error output 2
2. Redirect: Output redirection > Features: Overwrite output
Output redirection >> Features: Append output, preserve content
Error output redirect 2> 2>>
Merge normal output stream and error output stream
&> Overwrite redirects &>> chase-weighted redirects
2 > &1 merge errors and standard output
(Command;command) Merges stdout of multiple programs. For example:
ls 2>1 redirects the error output of LS to the 1 file, LS does not have an error and therefore normally outputs and establishes an empty file 1:
[[email protected] copytest]# ls 2>1
1 7.txt
[[email protected] copytest]# Cat 1
ls XXX 2>1 will not have xxx This file error output to file 1
[email protected] copytest]# Cat AAA 2>1
[[email protected] copytest]# ls
1 7.txt
[[email protected] copytest]# Cat 1
CAT:AAA: No file or directory
ls xxx 2>&1 will not generate 1 of this file, but the error ran to the standard output
[[email protected] copytest]# ls xxx 2>&1
LS: cannot access xxx: No file or directory
[[email protected] copytest]# ls
7.txt
ls xxx > Out1.txt 2>&1 merge errors and standard output to OUT1
[[email protected] copytest]# ls xxx >out1.txt 2>&1
[[email protected] copytest]# ls
7.txt Out1.txt
[email protected] copytest]# cat Out1.txt
LS: cannot access xxx: No file or directory
[Email protected] copytest]# ls/etc/issue > Out2.txt 2>&1
[[email protected] copytest]# ls
7.txt Out1.txt Out2.txt
[email protected] copytest]# cat Out2.txt
/etc/issue
[[Email protected] copytest]# (ls/;cat aaaaa) > Out4.txt 2>&1
[[email protected] copytest]# ls
7.txt out1.txt out2.txt out3.txt out4.txt
[email protected] copytest]# cat Out4.txt
Bin Boot Dev etc home lib Lib64 media mnt opt (omitted)
CAT:AAAAA: No file or directory
3. Set command: Used to set the shell.
-c The current shell process is valid, the settings so that the file can not be overwritten, to prevent false operation;
+c Cancel file cannot be overwritten
-F cancels the use of a wildcard character
-N read instruction only and not execute
-V shows the input values that the shell reads
-L record the variable name for the For loop
Ii. Detailed orders and examples
1. TR command: Bitwise conversion, TR [OPTION] ... SET1 [SET2] [< FILE] Converts the characters in the input data, all of which appear within the definition of SET1, to the characters appearing in the SET2;
-D Delete all the contents of the file that appear in Set1
[[email protected] copytest]# tr-d ' A-Z ' </etc/issue COS 7.2 K \ #tr命令只是读取文件, the source file will not be modified
-C replaces the Set1 with the complement of the characters in the Set1, where the character set is ASCII.
[[email protected] copytest]# tr-c ' A-Z ' </etc/issue tr: "A-Z" after a missing operand the two sets of strings must be given when the substitution operation occurs. Try ' tr--help ' for more information. [[email protected] copytest]# tr-c ' A-z ' * ' </etc/issue *ent*********ernel**r*on*an**m**d**l**n**r**s**v**t**o*[[ Email protected] copytest]#
After the given format, a-Z complement is all non-lowercase letters, all replaced with *, and wildcard glob are supported.
-S removes duplicate characters in the file that appear in Set1, leaving only one
[email protected] testdir]# cat tr lsglalsjgllkksajldkglklkjfjfjjd asgaaasdjdjjdjdjdddjjkk AAAA [email p Rotected] testdir]# tr-s [[: Alpha:]] < tr LSGLALSJGLKSAJLDKGLKLKJFJFJD ASGASDJDJDJDJDJK a
2, Mail command: for sending and receiving mail;
Mail-s "title" Address or user name <<eof (Terminator)
Mail-s "title" Address or Username < file (redirect input to content)
Pipelines can also pass content to mail, for example:
[Email protected] testdir]# Cat/etc/issue | Mail-s "Hello" root
Iii. Assignments and exercises (Part I: Courseware Part V redirects and pipelines)
1, Cat/etc/issue | Tr ' A-Z ' A-Z ' >/tmp/issue.out
2. Who | Tr ' A-Z ' A-Z ' >/tmp/who.out
3, [[email protected] copytest]# mail -s "Help" root <<EOF > hello > i ' m $ (whoami) > the system version is here,please help me to check it > thanks > $ (uname &NBSP;-A) > EOF you have mail in /var/spool/mail/root [[email protected] copytest]# mail heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 6 messages 1 new 2 unread u 1 [email protected] tue jul 26 10:01 138/5231 "[abrt] full Crash report " 2 root sat jul 30 11:28 28/665 "Hello" 3 root sat Jul 30 12:57 22/800 "Help" 4 root Sat Jul 30 12:58 22/800 "Help" 5 root Sat Jul 30 12:58 22/800 "Help" >n 6 root Sat Jul 30 13:00 22/792 "Help" & 6 message 6: from [ email protected] sat jul 30 13:00:06 2016 return-path: <[email protected]> x-original-to: root delivered-to: [email protected] date: sat, 30 jul 2016 13:00:06 +0800 To: [email protected] subject: help user-agent: heirloom mailx 12.5 7/5/10 content-type: text/plain; charset=us-ascii from: [email protected] (root) status: r hello i ' m root the system version is here,please help me to check it thanks Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 smp thu nov 19 22:10:57 UTC 2015 x86_64 x86_ 64 x86_64 Gnu/linux
4. The list of files under root is displayed in one line and separated by:
[Email protected] copytest]# Ls/root | Tr ' \ n ' ' aaa.txt aa.txt abc.txt anaconda-ks.cfg mypython VIMRC [[email protected] copytest]#
5. The contents of the File1 file are summed:
[email protected] testdir]# cat File1.txt 1 2 3 4 5 6 7 8 9 [[email protected] testdir]# cat file1.sh #!/bin/bash # declare num_count=0 for NUM in $ (cat file1.txt) does num_count=$[$num _count+ $num] done EC Ho "num_count1 = $num _count" # [[email protected] testdir]# bash file1.sh num_count1 = 55
6. Delete ^m characters in Windows text file: two methods:
[Email protected] testdir]# cat-a test.txt | Tr ' ^m$ ' $ ' Weh $ ASDLGH $ QWETHL $ qwl Lwwhet $ Kweeh $ Llhwet $
Dos2unix Direct Conversion format
7. Keep only the numbers and spaces in the string
[email protected] testdir]# cat string.txt xt.,l 1 jr#! $mn 2 c*/fe 3 uz 4 [[email protected] testdir]# Cat St Ring.txt | Tr-d ' [[: alpha:]][[:p UNCT:]] ' 1 2 3 4
This article from "Jing Xuan," blog, declined reprint!
Linux Learning Note 10