First, shell overview
1.Shell Overview:
Shell: Shell, a shell is a command-line interpreter that provides users with an interface-level program that sends requests to the Linux kernel to run programs, and users can start, suspend, stop, or even write programs with the shell. Similar to translation, the Windows operating interface is quite
Shell is also a very powerful programming language, easy to write, easy to debug, flexible. The shell is the scripting language that interprets execution, and the Linux command can be called directly in the shell.
Classification of 2.Shell:
Bourne Shell: The Bourne shell that started using UNIX from 1979
Bourne Shell's main file name is sh
C Shell:c Shell is mainly used in the BSD version of UNIX system, its syntax and C language similar to the name
The shell has two main syntaxes: Bourne and C, which are incompatible with each other.
Bourne family mainly include: sh, ksh, Bash (Standard shell), PSH, zsh;
The C family mainly includes: CSH, tcsh; mainly for Unix
Bash:bash is compatible with SH, and Linux is now used as the basic shell for users using bash.
3.Linux supported Shell
Save in/etc/shells directory
[Email protected] ~]# Vi/etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
[[Email protected] ~]# sh
sh-4.1# exit
Exit
[Email protected] ~]# CSH
[[Email protected] ~]# exit
Exit
[Email protected] ~]#
#那么如何判断我们进入的当前shell类型呢, use the Echo $SHELL to view the current shell
[Email protected] ~]# echo $SHELL
/bin/bash
Parent Shell: Shell entered at the beginning
Child Shell: The shell that is called later
Second, the script execution mode
1.echo Output command
echo [options] [Output CONTENT]
Options:
-E: Character conversions that support backslash control
Control characters:
\a: Output Warning tone
\b: Backspace key, that is, delete key to the left
\ n: Line break
\ r: Enter
\ t: tab, which is the TAB key
\v: Vertical tab
\0NNN: Output characters According to the Octal ASCLL code table. Where 0 is the number 0,nnn is the three-bit octal number
\XHH: Output characters according to the hexadecimal ASCLL code table. Where HH is a two-digit hexadecimal number
[Email protected] ~]# echo "What does want to do?"
What does want to do?
[Email protected] ~]# echo-e "What does want to\b do?"
What does want t do?
[Email protected] ~]# echo-e "\x68\t\x65\t\x6c\n\x6c\t\x6f\t"
#按照十六进制ASC Ⅱ code can also be output
H e L
L O
#注意, exclamation marks have special meanings in the echo command.
[Email protected] ~]# echo-e "\e[1;31m do you want do \e[0m"
#输出颜色:
#30m = black, 31m= red, 32m= Green, 33m=***
#34m = blue, 35m= magenta, 36m= cyan, 37m= White
#\e[1;: Turn on color
#\e[0m: Turn off color
2. First Script
#!/bin/bash
#The First Shell Program
Echo-e "\e[1;34m in the sky to the next Lin sister \e[0m"
Wq
[Email protected] ~]# VI hello.sh
#!/bin/bash nominal the script written below is standard, the pure shell is not error, but the other program is called error
#学会写注释
3. Script execution
(1). Give execute permission to run directly
chmod 755 hello.sh
./hello.sh Relative paths
(2). Execute script via bash call
Bash hello.sh #用bash直接解释命令
If the script syntax has errors, you can pass:
[[email protected] ~]# vim hello.sh for return modification
Pay attention to writing symbols in English
[Email protected] ~]# bash hello.sh
Called directly, or:
[Email protected] ~]# chmod 755 hello.sh
[email protected] ~]# LL
Total Dosage 48
-rwxr-xr-x. 1 root root 91 July 11:25 hello.sh
The permissions become executable
[Email protected] ~]#./hello.sh
In the sky the next sister Lin
[Email protected] ~]#/root/hello.sh
In the sky the next sister Lin
Iii. Basic functions of Bash
1. Command aliases and shortcut keys
Command alias = = nickname, another salutation
A. Some of the commands are particularly long, so we can take the individual names and operate them conveniently.
B. After the operating system upgrade, the command format has changed, then we can use the old command to map the new command
(1). Alias
#查看系统中所有的命令别名
[[email protected] ~]# alias
Alias cp = ' Cp-i '
Alias l.= ' ls-d. *--color=auto '
...
(2). Alias alias = ' Original Command '
#设定命令别名
[Email protected] ~]# vi/etc/passwd #不支持颜色
[Email protected] ~]# vim/etc/passwd #支持颜色
We can:
[[email protected] ~]# alias vi= ' Vim '
(3). Aliases are permanently active and delete aliases
VI ~/.BASHRC
#写入环境变量配置文件 (defines the configuration file for the operating environment)
Go to root. BASHRC Impromptu configuration can be
Alias rm = ' Rm-i ' Where-I is used to ask whether to delete the
(4). Unalias aliases
#删除别名
(5). Order of effective Order
First Order: Executes a command executed with an absolute or relative path
Second order: Performing aliases
Third Order: Execute bash's internal command, Linux comes with the
Order four: Perform the first command found in the order of directory lookups defined by the $PATH environment variable
[Email protected] ~]# echo $PATH
(6). Common shortcut keys
CTRL + C forces the current command to terminate
Ctrl+l Clear Screen
CTRL + A cursor moves to the beginning of the command
Ctrl+e cursor moves to the end of the command line
Ctrl+u Delete from the cursor position to the beginning of the line
CTRL + Z put the command in the background
Ctrl+r Search in the elapsed command
[Email protected] ~]# ^c
2. Historical order
(1). Historical order
History [Options] [Historical command Save file]
Options:
-C: Clear History command
-W: Write the History command in the cache to the history command to save the file ~/,bash_history
Saving text actually takes up a little
[Email protected] ~]# LS-ALH
Total dosage 104K
-RW-------. 1 root root 7.2K October 02:27 bash history
It can be saved.
Also: History commands are saved by default by 1000, and can be modified in the environment variable configuration file/etc/profile.
[Email protected] ~]# vi/etc/history
histsize=1000
can be directly modified
(2). Call of the history command
Use the up and down arrows to invoke previous history commands
Repeat nth History command with "!n"
Use "!!" Repeat the previous command
Use "! string" to repeat the last command that starts with the string
[[email protected] ~]# history# can view all historical command records that have occurred in the valid range
[Email protected] ~]# service network restart
Shutting down interface eth0: [OK]
Close Loopback interface: [OK]
Eject loopback interface: [OK]
Popup interface eth0: [OK]
This time we can use it again to enter the command:
[Email protected] ~]#!ser
Service Network restart
(3). command and file completion
In bash, command and file completion are very handy and common functions, as long as we enter commands or files,
Click the TAB key to auto-complete if there are multiple related commands
We can: press the tab twice to pop up all the relevant commands
In fact, the command completion relies on the $path command, the file completion dependency and the Operation path
3. Output redirection
(1). Overview:
Output redirection and input redirection:
The results are written to the computer called input, and the results are displayed as output.
Device device file name file descriptor type
Keypad/dev/stdin 0 Standard input
Display/dev/sdtout 1 standard output
Display/dev/sdterr 2 standard error output
(2). Output redirection:
Type symbol function
Standard output REDIRECT command > file to overwrite the correct output of the command to the specified file or device
Command >> file to append the correct output of the command to the specified file or device
Standard error Output redirect error command 2> file to overwrite the error output of the command to the specified file or device
Error command 2>> file to append the error output of the command to the specified file or device
Correct output and Error output command > file 2>&1 to overwrite the correct and error output output of the command to the specified file
Command >> file 2>&1 to append the correct and error output of the command to the specified file
Command &> file to overwrite the correct output and error output in the same file
Command &>> file to append the correct and error output of the command to the specified file
Command >> file 1 2>> file 2 Append the correct output of the command to file 1, save the error output to file 2
With output redirection, the output can be saved for unattended
[Email protected] ~]# ifconfig > Test.log
[email protected] ~]# cat Test.log
#输出结果: ...
As long as the command has output, can be saved to the specified location, note that the > greater than will overwrite the previous results, do not want to overwrite the use of >>, if the target file does not exist will be automatically created.
If the command does not exist, or if there is an error, we should use 2> or 2>> to save the error message, the error message and the correct information should be in a different redirection format, note > or >> left and right side no space
Attention:
[[email protected] ~]# ls &>/dev/null
Where/dev/null is a blank, the files inside will be deleted directly, then the saved log will be destroyed, can not find the
Role: When writing a script, no process is required and only the results are viewed
(3). Enter redirection
[[email protected] ~]# WC [option] [filename]
Options:
-C Statistics bytes count
-W Statistics word count word
-L Count Rows list
[Email protected] ~]# WC
Abd
Ert
#ctrl +d
2 2 6
Separate words by spaces
Commands < files take the file as input to the command, count the number of bytes in the file, the number of words
Commands << identifiers
[[email protected] ~]# WC << dy (equivalent to end tag)
>asd
>
>erty
>dy
4 3 11
...
Identifiers use the content between identifiers as input to the command
4. Pipe break
(1). Sequential execution of MULTIPLE commands
Multi-command Executor Format function
; command 1; Command 2 Multiple command order execution, no logical connection between commands
&& Command 1&& Command 2 logic with (no data passing)
Command 2 does not execute when command 1 is executed correctly
Command 2 does not execute when command 1 is not executed correctly
|| Command 1| | Command 2 logical OR (no data passing)
Command 2 does not execute when command 1 is executed incorrectly
Command 2 does not execute when command 1 is executed correctly
Note that the semicolon is in English;
[[email protected] ~]# ls && echo yes | | Echo No
Acces.log ....
Yes
[[Email protected] ~]# LSA && echo yes | | Echo No
-bash:lsa:command not found
No
Order cannot be reversed: && echo yes | | Echo No
Here Command 1 can have more than one command, if one of the commands executed, then command 1 is the existence of the correct,&& will
Execution, | | Command 2 does not execute
[[email protected] ~]# ls | | echo No && echo Yes
Yes
[[Email protected] ~]# LSA | | echo No && echo Yes
No
Yes
(2) Pipe symbol
Command format:
[[Email protected] ~]# Command 1 | Command 2
#命令1的正确输出作为命令2的操作对象
Example:
[Email protected] ~]# Ls-l/etc | More
Display the files of etc in Split screen
[Email protected] ~]# Netstat-an | grep establishen
TCP 0 192.168.0.156:22 192.168.0.155:1459 Establishen
#提取网络监听端口情况
[Email protected] ~]# Netstat-an | grep Establishen | Wc-l
1 #统计连接结果个数
5. Wildcard characters
(1). Overview: Characters that can be used to match other symbols
Wildcard action
? Match any one character
* Matches 0 or more arbitrary characters, which can match any content
[] matches any one of the characters in the brackets. For example: [ABC] represents a certain match for one of the characters: A or B
or C
[-] matches any one of the characters in the brackets-represents a range, for example: [A-z] represents a
lowercase letters
[^] logical non, indicating that the match is not a character within the brackets. For example: [^0-9] represents a match that is not a
Characters of a number of words
Note: In addition to * can match multiple characters, the other can only match one
Example:
[Email protected] ~]# mkdir dy
[Email protected] ~]# Cd/root/dy
[Email protected] dy]# pwd
/root/dy
[email protected] dy]# Touch DZP
[email protected] dy]# Touch DZP2
[email protected] dy]# Touch DZP3
[email protected] dy]# Touch dzp34
[email protected] dy]# Touch Dzpcang
[email protected] dy]# Touch Dzpbols
[email protected] dy]# Touch DZPA
[[email protected] dy]# ls
Dzp dzp2 dzp3 dzp34 Dzpcang dzpbols
[[email protected] dy]# ls DZP
Dzp
[[email protected] dy]# ls dzp*
Dzp dzp2 dzp3 dzp34 Dzpcang dzpbols
[[email protected] dy]# ls DZP?
DZP2 DZP3
[[email protected] dy]# ls dzp[0-9]
DZP2 DZP3
[[email protected] dy]# ls dzp[0-9][0-9]
Dzp34
[[email protected] dy]# ls dzp[^0-9]
Dzpa
Note: Wildcards are primarily used to match filenames or file directories, and regular expressions are required if you want to match the contents of a file
(3). Other special symbols in bash:
Symbolic effect
"Single quotes, all special symbols in single quotes, such as" $ "and" ' "and" \ "have no special meaning
"" double quotation marks. Special symbols do not have special meanings in double quotes, but "$" and "'" and "\" exceptions
Has the special meaning of "value of the calling variable", "Reference command" and "escape character"
' Anti-quotes. The contents of the inverted quotation mark are system commands, which are executed first in bash, and the $ () function one
Yes, but it is recommended to use $ () and the reverse quotation mark is easy to read
$ () As with the anti-quote function, used to refer to system commands
# in the shell script, #开头的行代表注释
$ is used to invoke the value of a variable, such as when the value of the variable name needs to be called, you need to use the $name method to get changed
The value of the amount
\ escape character, followed by the special symbol after \ will lose meaning, into ordinary characters, such as \$ will output "$
"Symbol, not as a variable reference
[Email protected] dy]# a=123
[Email protected] dy]# echo $a
123
[Email protected] dy]# echo ' $a '
$a
[Email protected] dy]# echo "$a"
123
[Email protected] dy]# A=ls
[Email protected] dy]# echo "$a"
Ls
[[email protected] dy]# a= ' ls '
[Email protected] dy]# echo "$a"
Dzp
Dzp2
Dzp3
Dzp34
Dzpcang
Dzpbols
Dzpa
It is not usually confused with ' easy and ' single quotes, we can use $ ()
This article is from the "8626774" blog, please be sure to keep this source http://8636774.blog.51cto.com/8626774/1680182
The Shell Foundation of Linux Learning