BashShell Script Programming-Basic Features
Bash ShellBasic features:
Command history:
History: shell built-in commands
The environment variable HISTFILE defines the location where the user exits the command history list.
# Echo $ HISTFILE/root /. bash_history # echo $ HISTFILESIZE historical command entries of the files pointed to by HISTFILE # echo $ HISTSIZE number of historical command entries retained by the current bash process # history-d Delete the first command in the history List # history-c Clear history command # history display the latest command # history-a append the new history command of the current bash process to the command history file echo $ HISTFILESIZEhistoryhistory-dhistoryhistory #! Call the command echo $ HISTFILESIZE #! In the history list #!! Run the previous command echo $ HISTFILESIZE #! Again #! Lsb quickly calls the command lsb_releaseLSBVersion: base-4.0-amd64: base-4.0-noarch: core-4.0-amd64: core-4.0-noarch: graphics-4.0-amd64: graphics-4.0-noarch: printing-4.0-amd64: printing-4.0-noarch
# Ls/etc/passwd # Press esc in cat, and then press. To call/etc/passwd #! $/Etc/passwd-bash:/etc/passwd: Permission denied
How to control command history:
Environment variable: HISTCONTROL # echo $ HISTCONTROL ignore repeated commands (continuous and identical) ignoredupsignorespace ignore commands starting with spaces # export HISTCONTROL = ignorespace # echo $ HISTCONTROLignoredupsignoreboth: the preceding two functions take effect at the same time: exportHISTCONTROL = the parameter revokes the control of HISTCONTROL: unsetHISTCONTROL
Command completion:
Bash can search for commands starting with a user-specified String Based on built-in or external commands and directly supplement the complete mechanism.
The given headers can already uniquely identify a command and directly complete it.
A single command cannot be uniquely identified. You can enter the TAB key again to list all related commands.
Find the executable file name in the PATH specified by the PATH environment variable for completion
Path completion:TABKey
The file name completion mechanism is implemented for the path starting with the string given by the user.
Perform the completion operation in the specified path
Command Line Expansion
~ : Expand to the user's home directory
{}: A list separated by commas (,) can be carried and expanded into multiple paths.
/Tmp/{a, B}
/Tmp/a,/tmp/B
/Tmp/{a, B}/hello:
/Tmp/a/hello
/Tmp/a/hello
Exercise: Create/tmp/x/y1,/tmp/x/y2,/tmp/x/y1/a,/tmp/x/y1/B, /tmp/x/y2/a,/tmp/x/y2/B
# mkdir -pv /tmp/x/{y1,y2}/{a,b}/mkdir:created directory `/tmp/x'mkdir:created directory `/tmp/x/y1'mkdir:created directory `/tmp/x/y1/a/'mkdir:created directory `/tmp/x/y1/b/'mkdir:created directory `/tmp/x/y2'mkdir:created directory `/tmp/x/y2/a/'mkdir:created directory `/tmp/x/y2/b/'
Exercise: how to create four directories a_d, a_c, B _d, and B _c?
#mkdir -pv /tmp/{a,b}_{c,d}/mkdir:created directory `/tmp/a_c/'mkdir:created directory `/tmp/a_d/'mkdir:created directory `/tmp/b_c/'mkdir:created directory `/tmp/b_d/'
# tree /tmp//tmp/├──a3├──a_c├──a_d├──b_c├──b_d├──ks-script-ILxZQr├──ks-script-ILxZQr.log├──test├──test1├──x│ ├── y1│ │ ├── a│ │ └── b│ └── y2│ ├── a│ └── b└──yum.log14directories, files
-L #: How many levels of content are displayed
Exercise: Create
# tree /tmp/mylinux//tmp/mylinux/├──bin├──etc│ └── sysconfig│ └── etwork-scripts├──sbin├──usr│ ├── bin│ ├── lib│ ├── lib64│ ├── local│ │ ├── bin│ │ ├── lib│ │ ├── lib64│ │ └── sbin│ ├── sbin│ └── share└── var├── lib├── log└── run20directories, files
# mkdir -pv /tmp/mylinux/{bin,sbin,etc/sysconfig/etwork-scripts,usr/{bin,sbin,local/{bin,sbin,lib,lib64},share,lib,lib64}, var /{log,run,lib}}mkdir:created directory `/tmp/mylinux'mkdir:created directory `/tmp/mylinux/bin'mkdir:created directory `/tmp/mylinux/sbin'mkdir:created directory `/tmp/mylinux/etc'mkdir:created directory `/tmp/mylinux/etc/sysconfig'mkdir:created directory `/tmp/mylinux/etc/sysconfig/etwork-scripts'mkdir:created directory `/tmp/mylinux/usr'mkdir:created directory `/tmp/mylinux/usr/bin'mkdir:created directory `/tmp/mylinux/usr/sbin'mkdir:created directory `/tmp/mylinux/usr/local'mkdir:created directory `/tmp/mylinux/usr/local/bin'mkdir:created directory `/tmp/mylinux/usr/local/sbin'mkdir:created directory `/tmp/mylinux/usr/local/lib'mkdir:created directory `/tmp/mylinux/usr/local/lib64'mkdir:created directory `/tmp/mylinux/usr/share'mkdir:created directory `/tmp/mylinux/usr/lib'mkdir:created directory `/tmp/mylinux/usr/lib64'mkdir:created directory `/tmp/mylinux/ var 'mkdir:created directory `/tmp/mylinux/ var /log'mkdir:created directory `/tmp/mylinux/ var /run'mkdir:created directory `/tmp/mylinux/ var /lib'
Create a file name for one month, one day, one hour, one minute, and one second
# mkdir -pv $(date "+%Y-%m-%d-%H-%M-%S" )mkdir:created directory ` - - - - - '[root@localhosttmp]# ls- - - - -
Command Execution result
Each Command is executed in two states:
Success 0
Failure 1-127
Bash uses special variables $? Saves the status of the recently executed command.
Echo $?
# Echo $?
0
Data returned by the program itself
Status result after the program is executed
# Rpm-rf/tmp/mylinux
Rpm: arguments to -- root (-r) must begin with/
# Echo $?
1
Command alias
# Alias
# aliasaliascp= 'cp -i'aliasl.= 'ls -d .* --color=auto'aliasll= 'ls -l --color=auto'aliasls= 'ls --color=auto'aliasmv= 'mv -i'aliasrm= 'rm -i'aliaswhich= 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-ti
Use the original command: \ command
Note: The alias defined in the command line is only valid for the current shell process.
For continuous effectiveness, You need to edit the configuration file:
Valid only for the current user :~ /. Bashrc
Valid for global users:/etc/profile
Note: directly editing the configuration file does not apply to the current shell process.
Cancel alias: unaliasNAME
GlobbingWildcard
Wildcard file name: metacharacters
*: Match any character of any length
Pa *, * pa *, * pa, p * a, * p * a (the file name ends with a and contains p in the middle)
? : Match any single character
# Ls-d/etc/p ??
[]: Match any single character in a specified set
[0-9]
[A-z]
A-Z
[0-9a-zA-Z]
# Ls-d/etc/p [mn] *
[^]: Match any single character outside the specified set
# Ls-d/etc/p [^ m-z] *
Character Set combination:
[: Space:]: All blank characters
[[: Space:]: removes all spaces
[: Punct:]: All punctuation marks
[: Lower:]: All lowercase letters
[: Upper:]: all uppercase letters
[: Digit:]: All numbers
[: Alpha:]: All letters
[: Alnum:]: All letters and numbers
Exercise 1: show all files or directories starting with l in the/var directory and ending with a lowercase letter with a digit in the middle;
|
# ls-d /var/l*[[:digit:]]*[[:lower:]]
|
Exercise 2: display a file or directory that starts with any number and ends with a non-number in the/etc/directory;
1 |
# ls-d /etc/[0-9]*[^0-9]
|
Exercise 3: show a file or directory with a letter or any other characters following it starting with a non-letter under the/etc/directory;
1 |
# ls-d /etc/[^[:alpha:]][[:alpha:]]*
|
Exercise 4: Copy all files or directories starting with m and ending with non-numbers under/etc/to the/tmp/test1 directory;
#mkdir test1# cp-a /etc/m*[^ - ] /tmp/test1
Exercise 5: Copy all files or directories ending with. d under the/etc directory to the/tmp/test2 directory.
#mkdir test2# cp-r /etc/*.d /tmp/test2
Exercise 6: Copy all files or directories ending with. conf and starting with m, n, r, and p under the/etc directory to the/tmp/test3 directory;
#mkdir test3# cp-r /etc/[mnrp]*.conf /tmp/test3
Shortcut Key
# Cdd/etc/sysconfig/n
# Ctrl + a: Jump to the Command Header
# Ctrl + e: jump to the end of the command
# Ctrl + l: clear screen
# Ctrl + c: Abort or cancel
# Ctrl + u: delete all characters from the beginning of the command line to the cursor
# Ctrl + k: delete all characters at the end of the optical Ratio
Input/Output redirection
The program requires input and output.
Program: Command + Data
The storage location of the results generated after the program processes the data is output.
Standard Input: keyboard, 0
Standard output: Monitor, 1
Error output: Display, 2
Fd: file descriptor, file descriptor. windows is called a handle.
0, 1, 2
Output redirection: output the file to other places
>: Content in the target file is cleared.
>>: The new content is appended to the end of the file.
# Cat issue>/tmp/issue. out
# Ls/var>/tmp/issue. out
Use set-C to disable overwrite redirection and set + C to enable
# Ls/etc> |/tmp/issue this is mandatory Overwrite
Error output for output redirection: Output Error information to other places
#ls / var r > /tmp/ var .err#ls / var > /tmp/ var .out#ls / var r >>/tmp/ var /err
Merge the data streams of standard output and error output to the same place: &>
#ls / var &> /tmp/ var .out#ls / var &>> /tmp/ var .outCOMMAND > /path/to/somefile > &
Input redirection: <
<: The document is generated here.
2 <
2 <