Basic characteristics of bash
This article introduces the relevant knowledge of "the basic characteristics of bash". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Bash features 1. Command history: history .bash _ history: file in which the command history is saved when the user logs out (hidden file in the user's home directory) history #: show the most recent # commands; invoke the command in the command history list! #: execute the # command in the history list again;!: execute the previous command again ! STRING: execute the most recent command in the command history list that starts with STRING again; call the last parameter of the previous command: shortcut key: ESC,. (press ESC after release.) Shortcut key: Alt+. (hold down Alt and press.) string:! $(enter directly! $) [root@localhost local] # cat / tmp/script/idsum1.sh. [root@localhost local] # vi! $vi / tmp/script/idsum1.sh2. Completion: command completion: can be uniquely identified, then direct Tab completion, if not, press Tab to give the list again; external commands according to the directory set in the PATH environment variable, search the file name path completion under the directory one by one from left to right: unique identification Tab completion, if not, press Tab to give the list again; 3. Command line expansion ~: automatically expand to the user's home directory, or the specified user's home directory; {}: can host a comma-separated list of paths and expand it into multiple paths; [root@localhost tmp] # ls x _ {ymenz} x_y:afile1 afile2x_z:afile3 afile44. The status of the command was successfully executed: 0 was returned; failed: 1: 255.After the command was completed, the execution status was saved in the special variable $? of bash. When the command [root@localhost Xfolz] # useradd yc [root@localhost Xfolz] # echo $? [root@localhost Xintz] # useradd ycuseradd: user 'yc' already exists [root@localhost Xfolz] # echo $? 9 is executed normally, some commands return values, and the results vary depending on the command and its functions. Reference the execution result of the command: $(COMMAND) or ~ (COMMAND) 5. Quote strong quote: single quotation mark, in which anything is displayed directly without replacement. Weak reference: double quotation marks in which the variable is replaced by the value of the variable where it occurs. Command reference: anti-single quotation mark, which refers to the result of command execution, $(COMMAND) or ~ (COMMAND). [root@localhost Xyogz] # name= "YC" [root@localhost Xampz] # echo'$name'$name [root@localhost Xampz] # echo "$name" YC [root@localhost Xampz] # mkdir dir_ `date +% hmuri% Mmuri% S` [root@localhost Xampz] # lsafiel3 afile4 dir_Jul-48-036. Shortcut ctrl+a: skip to the beginning of the command line; ctrl+c: skip to the end of the command line; ctrl+u: delete from the beginning of the line to the cursor; ctrl+k: delete from the cursor to the end of the line; ctrl+l: clear;7. The file wildcard globbing is actually a path extension implemented by shell. When shell encounters a wildcard in parameters, it searches disk for possible matches as a path or file name. *: match any character of any length; match any single character, such as pa,p?a?; []: match any single character within the specified range There are several special formats: [amurz]; [Amurz]; [0-9] [a-z0-9] [[: upper:]]: all uppercase letters [[: lower:]]: all lowercase letters [[: digit:]]: all numbers [[: alpha:]]: all letters [: alnum:]]: all letters and numbers [[: space: ]]: space character [[: punct:]]: it doesn't matter if you don't remember all the punctuation marks. Man grep contains: Their names are self explanatory, and they are [: alnum:], [: alpha:], [: cntrl:], [: digit:], [: graph:], [: lower:], [: print:], [: punct:], [: space:], [: upper:], and [: xdigit:]. For example, [[: alnum:]] means the character class of numbers and letters in the current locale. In the C locale and ASCII character set encoding, this is the same as [0-9A-Za-z] ^ []: matches any single character outside the specified range [^ [: upper:]] [^ aMuz] example: [root@localhost xyogz] # ls * [Ff] ile*-filename afile4 myfile MyFile myfile_002 with File or file # find / tmp/x_z/-name "* [^ [: alpha:]] *"-- filename / tmp/x_z//tmp/x_ with non-letters Z/afile4/tmp/x_z/afiel3/tmp/x_z/dir_Jul-48-03/tmp/x_z/1.txt/tmp/x_z/myfile_002 [root@localhost Xampz] # find / tmp/x_z/-name "* [^ amurz] *"-File name / tmp/x_z//tmp/x_z/afile4/tmp/x_z/afiel3/tmp/x_z/ with non-lowercase letters Dir_Jul-48-03/tmp/x_z/1.txt/tmp/x_z/myfile_002/tmp/x_z/ MyFile II. A pattern written by text characters in a special character set. Some of these characters do not represent their literal meaning (to indicate the escape character /), but are used to indicate the function of control or configuration. Expression composition: literal text character + metacharacter metacharacter: refers to those special characters with special meaning in regular expressions, which make regular expressions have processing power. Grep:Global search Regular expression and print out the line text search tool, according to the user-specified pattern (filter criteria) to match the target file line by line, print matching lines; regular expression engine: metacharacters and rules vary slightly from engine to engine. Grep [OPTIONS] PATTERN [FILE...] Grep [OPTIONS] [- e PATTERN |-f FILE] [FILE...] OPIONTS:-I: case-insensitive (ignore case);-o: displays only matching characters;-v: displays lines that cannot be matched -Q: does not output any information (used when only the result status is needed)-E: supports extended regular expression-A #: after matches the last # line of the line-B #: before matches the first # line of the line-C #: context matches the line before and after the basic regular expression metacharacter: character match. Match any single character; []: match any single character in the specified range There are several special formats: [amurz]; [Amurz]; [0-9] [a-z0-9] [[: upper:]]: all uppercase letters [[: lower:]]: all lowercase letters [[: digit:]]: all numbers [[: alpha:]]: all letters [: alnum:]]: all letters and numbers [[: space: ]]: space character [[: punct:]]: all punctuation marks man grep: Their names are self explanatory And they are [: alnum:], [: alpha:], [: cntrl:], [: digit:], [: graph:], [: lower:], [: print:], [: punct:], [: space:], [: upper:], and [: xdigit:]. For example, [[: alnum:]] means the character class of numbers and letters in the current locale. In the C locale and ASCII character set encoding, this is the same as [0-9A-Za-z] ^ []: match any single character outside the specified range; [^ [: upper:]] number of times matching: after the specified number of characters, limit the number of previous characters, implement greedy mode: match as many matches as possible on the premise that the whole expression matches successfully *: match any previous character, 0 times, 1 or more times. *: match any previous character of any length;\?: match the previous character 0 or 1 times, then it is optional;\ +: match the previous character one or more times, it must exist;\ {m\}: match the previous character for m times \ {m PATTEERN$ n\}: match the preceding character at least m times, at most n times;\ {0parentional n\}: match the preceding character at most n times;\ {m,\}: match the preceding character at least m times; position anchor: ^: line beginning anchor $: line end anchor; ^ PATTEERN$: match the entire line with PATTERN ^ $: blank lines; ^ [: space:] * $: blank lines or lines containing blank characters; word anchoring:\ or\ b: suffix anchoring, placed after words;\: match complete word PATTERN; grouping and references\ (\): tie one or more characters together and treat them as a whole Enclose in parentheses, using escape characters when indicating parentheses; note: the content matched by the pattern in the grouping parentheses is automatically recorded in the internal variables by the regular expression engine, which are:\ 1: the pattern starts from the left. The character matched by the first left parenthesis and the matching right parenthesis \ 2: the character matched by the pattern from the left, the second left parenthesis and the matching right parenthesis;\ 3: latter item reference: refers to the character matched by the pattern in the preceding grouped parenthesis
Exercise:
two。 Show lines in the / etc/passwd file that do not end with / bin/bash
[root@localhost Xampz] # grep-v "/ bin/bash$" / etc/passwdbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin.
3. Find the lines in / etc/passwd that contain two or three digits
[root@localhost Xampz] # grep "\" / etc/passwdmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin.
4. Show lines in the / porc/meminfo file that begin with uppercase or lowercase S.
[root@localhost Xampz] # grep "^ [Ss]\ + *" / proc/meminfo-error demonstration, this * can be 0 times or without s MemTotal: 3868768 kBMemFree: 2927360 kB.... [root@localhost Xanthz] # grep "^ [Ss]\ +" / proc/meminfo SwapCached: 0 kBSwapTotal: 2097148 kBSwapFree: 2097148 kBShmem: 149628 kBSReclaimable: 85044 kBSUnreclaim: 64584 kB [root@localhost Xampz] # grep-I "^ [s]\ +" / proc / meminfo SwapCached: 0 kBSwapTotal: 2097148 kBSwapFree: 2097148 kBShmem: 9368 kBSlab: 149628 kBSReclaimable: 85044 kBSUnreclaim: 64584 kB [root@localhost Xeroz] # grep-E "^ (s | S) +" / proc/meminfo SwapCached: 0 kBSwapTotal: 2097148 kBSwapFree: 2097148 kBShmem: 9368 kBSlab: 149788 kBSReclaimable: 85028 kBSUnreclaim: 64760 kB egrep supports extended regular expressions Implement grep text filtering function Grep-E-regular expression metacharacter: character matching is supported by extended regular expression metacharacter. : match any single character; []: match any single character in the specified range; ^ []: match any single character outside the specified range; number of times matching: after the specified number of characters, limit the number of previous characters. Implement greedy mode greedy mode: match as many matches as possible on the premise that the whole expression matches successfully *: match any previous character, 0 times, 1 or more times. *: match any previous character of any length;?: match the preceding character 0 or 1 times, then it is optional; +: match the preceding character one or more times, it must exist; {m}: match the preceding character for m times {m PATTEERN$ n}: match the preceding character at least m times, at most n times; {0Magne n}: match the preceding character at most n times; {m,}: match the preceding character at least m times; position anchor: ^: line beginning anchor $: line end anchor; ^ PATTEERN$: match the entire line with PATTERN; ^ $: blank line ^ [[: space:]] * $: blank lines or lines containing blank characters; word anchoring:\ or\ b: anchored at the end of the word, placed after the word;\: match complete word PATTERN; grouping and quotation (): bind one or more characters together as a whole; enclose them in parentheses to indicate escape characters Note: the content of the pattern matching in the grouping parentheses is automatically recorded by the regular expression engine in the internal variables, which are:\ 1: the pattern from the left, the character matched by the pattern between the first left parenthesis and the matching right parenthesis \ 2: the character matched by the pattern from the left, the second left parenthesis and the matching right parenthesis;\ 3: the latter item reference: refers to the character matched by the pattern in the preceding grouped parentheses; or a | b such as C | cat for C or cat; (C | c) at for Cat or cat
Exercise:
5. Use echo to output an absolute path and egrep to fetch the path name, similar to the result of performing dirname / etc/passwd.
[root@localhost pp] # pwd/tmp/x_z/ pp.[ root @ localhost pp] # lsYC.sh [root@localhost pp] # dirname / tmp/x_z/pp/YC.sh / tmp/x_z/ ppp [root @ localhost pp] # echo / tmp/x_z/pp/YC.sh | grep-E ". * /\