Shell_advanced, advancedbashshell

Source: Internet
Author: User

Shell_advanced, advancedbashshell
1. Redirection, pipeline 2, <(cmd);> (cmd) 3, >;>;<<>>>>>; <4. Text Processing _ 1: cat; head; tail; cut; wc; sort; uniq; tr; tac; rev 1. Redirect inbound and outbound, redirection, pipeline 1 and Linux programs have three standard inputs and outputs: Standard inputs, represented by numbers 0, connected to the keyboard by default. Programs are usually used to obtain the standard output of user input, it is represented by number 1 and connected to the screen by default. The program is usually used to output standard information errors. It is represented by number 2. It is connected to the screen by default. The program is usually used to output error information parsing: the passwd command reads the user password from the standard input. Therefore, you can use the keyboard to enter the password command echo to output the result to the standard output, so we can see the result on the screen that normal users do not have the permission to view the/root directory, so an error occurs, so ls outputs the error information to the standard error, that is, screen 2, >>>> Delete the original data, write new data> append the new data to the original data 3,/dev/null is commonly known: Garbage bin file # echo hello world>/dev/null -- Data redirected to this file is lost by the kernel, this feature can be used to shield some output information # ls/root 2>/dev/null -- block the error information of the command 4. The output redirection user can, as needed, modify the default input/output direction # ls-l> test # ls-l 1> test -- both are standard output redirected to the test file, that is, the default value 1 can be omitted. # ls/jjjj 2> test -- redirects the standard error to the test file # ls // jjjj 1> stdout 2> stderr -- redirects the standard output and standard error respectively. to two different files: # ls/jjjj/> std 2> & 1 -- merge standard errors into standard output, and redirect to a file # ls/jjjj/&> std -- the result is equivalent to the previous statement, more concise # ls/jjjj/&> Std -- also append standard output and standard error redirection to a file. 5. Standard input redirection # cat 0 </etc/passwd # cat/etc/passwd -- The standard input of the cat command is from file/etc/passwd 6. Redirection described above in the pipeline, is to connect the standard input and output to the file. We can also connect the standard input and output of multiple programs to transmit data between commands. This technology is visually called a pipeline, the program output flows from the leftmost program to the rightmost program, just like the flow of water in the pipeline. Pipeline is a very common technology in linux shell. Using pipeline technology, you can use multiple gadgets to complete 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 17. in-depth exploration of standard input and output the standard input and output are the three file descriptors created by the program by default in Linux, although most programs use these three file descriptors as their input and output, this is not mandatory. That is to say, the program may not use these three standard file descriptors, instead, open a new file descriptor. When the program does not use standard input and output, the redirection used here does not work. For example: passwd: The program reads the user password from the standard input, so we can provide it with the password through the standard input redirection ssh: command is used for remote login program, it does not use standard input when reading the password entered by the user, therefore, we cannot redirect the standard input to provide it with a password. 8. Verify that the standard input of the Process outputs the connected file # sleep 999 1>/tmp/good 2> & 1 -- in terminal run command # pgrep-x sleep -- in another terminal, locate the sleep process ID # 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 row number before each row, then save the result redirection back to the original file # cat file -- it is null. Cause: Before the shell executes the command, it will first perform the redirection operation. In the 2nd commands, first execute> file, this is heavy The targeted operation clears the file content. When the redirection operation clears the file and then cat the file, it will naturally not see any content $ sudo ls/root>/root/ls. log -- in this example, the user has no chance to enter the password because the redirection operation is executed first. The current user is a common user and cannot be in the path/root/ls. create File in log, shell error and exit: bash:/root/ls. log: Permission denied. At this time, the command sudo has not been executed, so there is no chance to enter the password 2, <(cmd);> (cmd) 1, <(cmd) redirect standard input by process replacement 2.> (cmd) Redirect standard output by process replacement 3. Save the standard output of the Process replacement process to a temporary file, return the path of the temporary file <(). When you need to use the program output but do not want to generate an intermediate file, you can consider using process replacement # paste <(seq $ (wc-l/etc/passwd | cut-d ""-f1) <(awk-F: '{print $1}'/etc/passwd) # paste <(seq 26) <(awk-F: '{print $1}'/etc/passwd) -- compare the two commands. 3. >;>;<<>;>>>;<>;<<<<> -- output redirection, and create (overwrite if the command exists) # echo 'Hello world'> test -- create if test does not exist. Save 2. <-- input redirection, source File # less </etc/passwd -- input the content in/etc/passwd to tool less # cat <(ls /) -- replace # wc-l <$ (echo/etc/passwd) # wc-l <(cat/etc/passwd) -- use process replacement here 3.> -- redirect to a file and create (add to the end of the file if the file exists) # echo 'Hello george '> george -- create if george does not exist, if the object exists, append the content to the end of the object. 4. <-- redirection. Here document4.1 and Here Document are a special redirection method in Linux Shell. The basic format is as follows: cmd <delimiterHere Document Contentdelimiter: <delim Iter -- shell batch comment Here Document Contentdelimiter function: the Content between two delimiter (Here Document Content) pass to cmd as input parameter 4.2, terminal # cat <EOF> one> two> three> EOFEOF -- it's just an identifier, can be replaced with any valid character> -- this symbol is the identifier of the prompt input information generated by the terminal delimiter -- it must be written in the top level, and there cannot be any character behind it, including space 4.3, shell # vim here. sh -- Note: You can also use the variable #! /Bin/bashcat <EOF> output. shecho "hello" echo "world" echo $ 1EOF # chmod a + x here. sh #. /here. sh george # cat output. sh -- check the content. $1 is expanded as the script parameter here. Note: if you do not want to expand this variable, use double quotation marks to cause the first EOF. 4.4. Another use of <-Here Document is to change '<' to '<-'. The only change using <-is that the tab (tab) at the front of each line in the content section of the Here Document will be deleted, this method is used to indent the content part when writing the Here Document to facilitate reading Code 5 and <-- redirection, here string # wc-l <"$ (ls-l/home)" # while read x; do echo "hello "; done <"$ (seq 5)" # bc <"2 ^ 10" # vim string. sh #! /Bin/bashwhile read linedoif ["$ {line # ftp :}"! = "$ Line"]; thenawk-F: '{print $6}' <$ linebreakfidone </etc/passwd note: read every row in the/etc/passwd file cyclically. If you are an ftp user, print out the home directory and exit the loop $ {line # ftp :}: if a line matches ftp:;, only the unmatched part of the line is obtained. # 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 access, 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, connect the content of one or more files in the sort order and export them to the standard output # cat-n/etc/passwd-display the file content, add the row number # cat-A/etc/pa Sswd -- print out some invisible characters and positions # cat 1.txt 2.txt> test.txt -- merge files 2, head -- Read File Header # head-n 3/etc/passwd -- read files the first three lines of/etc/passwd # head-n-1 file -- discard the last line of file # head-c 3/etc/passwd -- read the first three lines of file/etc/passwd byte # head-c-3 file -- discard the last 3 bytes of the file # head-c 10 m/dev/urandom> big -- create a 10 m file 3. tail -- read the file tail # tail-n 3/etc/passwd -- read the file/etc/passwd and the next three lines # tail-n + 28/etc/passwd -- read from row 28th, until the end of the file; discard 27 rows in the header # Tail-c 3/etc/passwd -- read the last three bytes of the file/etc/passwd # tail-c + 28/etc/passwd -- read from 28th bytes, wait until the end of the file; discard 27 bytes of the header # tail-f/etc/passwd -- trace the changes in the content at the end of the file, which is often used to inspect the change of the log file, very practical 4. cut-the function is similar to awk, but it is not as powerful and complex as awk. When we want to output data in a separate manner, we often use awk. There are not many common options when we use cut: -d -- Define separator-B -- output byte-c -- output character (character) # echo "a; B; c d; e "| cut-d"; "-f1, 3,4 ---d defines the separator (TAB by default ); -f defines the output field # cat-n/etc/passwd | cut-d $ '\ N'-f1, 3-5, 7- -Use line breaks for separator # echo I am Chinese | cut-B1-3 ---B output the bytes at the specified position (byte ); A utf8 text occupies 3 bytes # echo I am Chinese | cut-c2-4 ---c output character at the specified position (character ); the difference with-B is manifested in processing non-English characters # echo be a brave Chinese | cut-b1-2, 9 -- will output a "false" word # echo-n to be a brave Chinese | xxd -- will find the three bytes, 9 are: e581 87 # echo-n false | xxd -- and "false" is also 5. wc -- common options for calculating the number of bytes, characters, words, and lines of data: -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, five utf8 Chinese characters occupy 15 bytes # echo-n I am from China Person | wc-m ---m calculates the number of characters. The difference between wc-m and wc-c is that when non-English characters are processed, similar to the command cut # echo-n, I am a Chinese | wc-w ---w calculates the number of words, no Delimiter is separated, and five Chinese words are counted as one word, this is different from the so-called "word" in CHINESE. # echo-n Uppercase CHINESE | wc-w -- two words # echo-n Uppercase CHINESE | wc-c -- 17 English bytes, if there is no-n, there will be 18 (because there is also a line break) # echo-n Uppercase CHINESE | wc-m -- 17 English characters, if there is no-n, there are 18 (because there is also a line break) # wc-l/etc/passwd ---l calculate the number of rows 6, sort -- sort files by row common options: -t -- specify the delimiter-k -- specify the sorted field-u -- remove duplicate rows-n,-h -- sort by value-r -- reverse Sort by # cut-d ": "-f7/etc/passwd | sort-u ---u remove duplicate rows # echo-e" 1 \ n2 \ n10 "| sort # echo-e" 1 \ n2 \ n10 "| sort-n ---n is ordered by numerical value, cannot process unit characters such as K, M, and G # ls-lh | tail-n + 2 | sort-k5, 5n ---k specifies the sorting field # ls-lh | tail-n + 2 | sort-k5, 5 h ---h can process K, M, G and other unit characters # head-4/etc/passwd | sort-t:-k7, 7 ---t use Colon: As the field delimiter, sort by 7th fields # head-4/etc/passwd | sort-t:-k7, 7-k3, 3n -- sort by 7th fields first. If 7th fields have the same, sort by 3rd fields # e Cho-e "1 \ n2 \ n3" | sort-nr ---r reverse sorting # head-4/etc/passwd | sort-t:-k7, 7-k3, 3nr -- to reverse the sorting of 3rd fields, you can also reverse the two fields at the same time 7, uniq -- remove continuous repeated rows common options: -c -- calculate the number of duplicate rows # echo-e "1 \ n1 \ n2 \ n1" | uniq -- the result still contains two values, because the two 1 s are not continuous # echo-e "1 \ n1 \ n2 \ n1" | sort-u -- sort removes duplicates and does not need to be continuous # cut-d: -f7/etc/passwd | sort | uniq-c -- calculate the number of times that different logon shells appear Based on the sort command example. 8. tr -- convert, delete, common options for downgrading the same character:-d -- Delete, delete all matching letters-s -- Reduce, reduce the same character format: tr SET1 SET2 note: Converts the characters in Set 1 to the characters at the corresponding position in Set 2. Therefore, in principle, the numbers of Characters in the two sets should be the same. However, if the numbers of Characters in the two sets are not equal, the program will not go wrong. Please pay attention to the results in this case. Key points: a and tr do not care about the characters in the two sets. They simply replace the characters at the corresponding positions one by one, and B and tr Replace the characters with one character, cannot replace 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, convert to B and A # echo good people do good deeds | tr good bad -- "good" to "bad" # echo abcdefg | tr a-z AB -- the Set 2 is short, the program automatically uses the last character in Set 2 to expand the Set 2 # echo abcdefg | tr a-B A-Z -- the Set 2 is long, the program automatically truncates the set by 2. # echo Abc | tr a-zA-z a-Za-z -- returns the size of English letters. # echo hello world | tr-d ow ---d delete, all matching letters # echo 0123456789 | tr-d 13579 # tr-d '\ 012' </etc/passwd -- delete the linefeed in the file/etc/passwd, tr can be a single character in octal notation # echo aabbaacc | tr-s a ---s, reduce the number of a connected files to one # echo aabbaacc | tr-s a A -- set and then convert again 9. tac -- press volume for the content of one or more files the sequence starts, export to mark export, in each file, print the content in reverse order of the row number # echo-e "111111111 \ n2222222"> f1 # echo-e "333333333 \ n4444444"> f2 # tac f2 f110, rev -- reverse the left and right 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 10 m/dev/urandom> bigfile -- use a random device to create a 10 M file # head-c 3 m bigfile> file1 -- import the first 3 M data into file1 # tail-c 4 m bigfile> file3 -- import the last 3 M data Import file3 # head-c 6 m bigfile | tail-c 3 m> file2 -- import 4 M data in the middle to file2 # ls-lh file * # cat file1 file2 file3> newbigfile -- use cat to merge the three files into a new large file # md5sum newbigfile bigfile -- use a command to compare the md5sum value of the old large file with that of the new large file

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.