1.Bash variables
User-defined variables #局部变量
environment variable #全局变量
Positional parameter variables
Pre-defined variables
X=val #赋值
$x #调用
Variable defaults to character type and cannot be directly calculated
"$x" ${x} variable overlay, that is, the string
Set #查看系统所有变量
-U when variable does not exist times wrong
Unset #清空变量
Pstree #查看进程树
Export x #把x变成环境变量
Path= "$PATH":/root
PS1 variable
\d Display Date "Day of the Week"
\h displaying the full host name
\ t "HH:MM:SS"
\a "hh:mm"
\u Current user Name
\w the full name of the current directory
\w the last directory in your current directory
\$ prompt
Language variables
Echo $LANG #当前语系环境
locale-a | More #所有语系环境
/etc/sysconfig/i18n #默认语系环境, next boot Enabled
Positional parameter variables
$n N is a number, and $ A represents the command itself, $1-$9 represents the first to the Nineth parameter, more than 10 ${10}
$* represents all parameters in the command line, a whole
[Email protected] command line all parameters, see separately
$# number of command-line arguments
Pre-defined variables
$? The return status of the last command executed is 0 correct
$$ Current Process PID
$! PID of the last process running in the background
read [options] [variable name]
-P "prompt message": Output prompt waiting for input
-T number of seconds: Specifies the time to wait for input
-N characters: Only the input of the specified number of characters is accepted
-S: input does not echo
2. Operators
Declare [+/-][option] Variable name #变量类型声明
+ Cancel Variable Type property
-Set type properties
-a declares an array
-I integer
-X environment variable
-R read-only #声明为只读的变量完全不能操作!!!
-P Display Type
Declaring an array
Movie[0]=zp
Movie[1]=tp
Declare-a moive[2]=live
View arrays
Echo ${movie}
Echo ${movie[2]}
Echo ${movie[*]}
Export variable name <==> declare-x variable name
Declare-p #列出系统所有变量类型
Numeric operations
Expr let
dd=$ (expr $aa + $bb)
$ ((expression))
ff=$ (($aa + $bb))
$[-expression]
gg=$[$aa + $BB]
Variable test
#在脚本优化时使用
#具体访问www. imooc.com/video/6832
3. Environment variable configuration file (loaded sequentially at startup)
source Config file #. Configuration file
/etc/profile
/etc/profile.d/*.sh
~/.bash_profile
~/.bashrc
/ETC/BASHRC #里面定义了PS1, which is the login prompt
Umask #查看系统默认权限----w--w-
The maximum file permissions are 666 #rw-rw-rw-
The maximum directory permission is 777 #rwxrwxrwx
Permissions cannot be converted using numbers, but must use letters
Umask defined permissions are the permissions that are ready to be discarded in the system default permissions ~
environment variable profile in effect when logging off
~/.bash_logout #登出配置
~/.bash_history #保存历史命令, the current login history command is stored in memory
#/etc/profile histsize Set History command to save the number of bars
Login File #只对本地终端有效
/etc/issue #登录警告信息
Escape characters that can be displayed
\d Current system date
\s Operating system name
\l Native Terminal
\m Hardware Architecture
\ nthe host name
\o Domain Name
\ r Kernel Version
\ t Current system time
\u the serial number of the currently logged-on user
Remote Terminal welcome Information #不支持转义符
/etc/issue.net #在/etc/ssh/sshd_config added "Banner/etc/issue.net" to display (restart sshd)
Display information after login #本地远程均支持
/etc/motd
4. Conditional judgment
The file type is judged, yes it is true
-B Block Device
-C character device
-D Directory
-E exists as true
-F Normal File
-L Link File
-P Pipe File
-S non-NULL is True
-S Socket file
TEST-E/root/install.log
[-e/root/install.log]
[-d/root] && echo yes | | Echo No
File Permission judgment
-R Read
-W Write
-X execution
-U SUID
-G SGID
-K Sbit
Compare between two files
F1-nt F2 to determine whether F1 modified time than F2 new
F1-ot F2 Judge F1 modified time than F2 old
F1-EF F2 determine if the inode is consistent (judging hard links)
Two integers
-eq-ne-gt-lt-ge-le
String judgments
-Z String #为空则真
-N String #非空则真
String 1 = = string 2 #相等则真
String 1! = String 2 #不等则真
Conditional statements
If [Conditional judgment type];then
Program
Fi
#或者
If [conditional judgment 1]
Then
conditions established, Procedure 1
elif [conditional judgment 2]
Then
Conditions are not established, procedure 2
Else
Program
Fi
Case statement
Case $ variable name in
"Val1")
Part1
;;
"Val2")
Part2
;;
*)
Part default
;;
Esac
For loop
For variable in val1 val2 val3 ...
Do
Program
Done
For ((i=1;i<100;i=i+1))
Do
Program
Done
While&until Cycle
While [conditional-judging]
Do
Program
Conditional Increment #如 i=$ (($i +1))
Done
The conditional judgement of until is opposite to while
5. Regular expressions
grep awk sed contains matching #正则
LS find CP exactly matches #通配符
* Any of the preceding characters
Match date
[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}
Matching IP
[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}
Cut [Options] file name #字段提取
-F Field
-D delimiter #默认制表符
printf ' output type output format ' output content
%ns output several characters
%ni several numbers
%m.nf several points of floating point
awk ' Condition 1{action 1} Condition 2{action 2} ... ' File name #复杂
#df-H | grep "/dev/sda2" | awk ' {print $} ' | cut-d "%"-F 1
#awk ' Begin{print ' ... "} {...} '
FS Specifies delimiter
#cat/etc/passwd | Grep/bin/bash | awk ' begin{fs= ': "}{print" \ T "$"
#cat Student.txt | Grep-v Name | awk ' $4>=70{print '
sed [options] ' [action] ' file name
#动作
-A Append
-C Line Substitution
-I insert before line
-P Printing
-S
-D Delete Row
Character processing
Sort [Options] file name
-F ignores case
-N Sort by numeric
-R Reverse Sort
-T Specify delimiter
-K n[,m] Sort by specified field, n start m end
WC [option] File name
-L Line
-W Word
-M character Count
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Shell Programming Notes