read
在Bash中,当希望获取用户输入的内容时,可以用到【read】命令,有点类似 c++的cin 或 Java的System.in;
read [-pt] variable
-p: 在输入的时候给予用户提示;
-t: 等待输入超时的时间
variable: 输入的内容赋值到的变量名
如: read -p "Input Your Name:" -t 30 name
变量
变量在系统中分为自定义变量(局部) 和 系统环境变量(全局),自定义变量仅可以在当前进程中使用(子进程中无法访问,输入bash可以进入子进程);
声明变量的方法: [变量名]=[值]
自定义变量可以通过export设置为环境变量,但是当当前的进程结束(即exit)后,之前export为系统变量的自定义变量也会消亡;
除此之外还可以使用declare来设置变量:
declare [-aixrp] variable
-a 将变量设置为数组(array)的形式;
-i 将变量设置为整形(int)的形式;
-x 变量的export操作;
-r 将变量设置为只读,无法修改,删除;
-p 查看变量的状态
例: declare total=200+350
echo $total ==> 200+350 ==> 因为没有 -i,所以默认以字符串的形式存储变量值;
declare -i total=total
echo $total ==> 550
declare -r total
declare -p total ==> declare -ir total="550" ==> 可以看到目前已设置的属性(-ir);
declare -x total ==> 虽然total设置为了只读,但还是可以export;
declare -p total ==> declare -irx total="550"
declare +x total ==> +x 表示取消export
declare -p total ==> declare -ir total="550"
declare -a total
declare -p total ==> declare -air total='([0]="550")' ==> 变成了array
数据流重定向
首先系统的输出分为:Standard Out 以及 Standard Error Out,而重定向需要分别处理这两种情况
- 正常情况的系统输出由 > 或 >> 来指定重定向的文件;
- 出错的情况使用 2> 或 2>> 来指定错误信息的去向;
什么是错误情况:例如在某目录下的普通用户,在不具备 w 权限时尝试创建文件,系统则会出现无权限的提示(Permission Denied),这种就称之为错误信息,需要用 2> 或 2>> 来处理数据重定向的问题;
> : 输入命令行命令后返回的结果可以将其重定向到文件中去,若文件不存在,则创建一个;
例:ls -al > forwardFile
>> : 若文件存在,则会在该文件下追加信息,而不是覆盖;
例: last >> forwardFile
2> 以及 2>> 同理,用于处理系统错误信息的重定向;
同时使用 >> 和 2>> :
例: rm /home/.bashrc >> successInfo 2>> errorInfo
<: 输入重定向,即将文件中的信息代替键盘的输入;
例:
$ echo 'ls -al' > file # 创建file文件,内容为 ls -al
$ $(< file) # 等同于直接在键盘上输入 ls -al
<< : 指定输入以什么字符串作为结束符号;
例 :
$ cat > catfile << "ed"
> First Line.
> Second Line.
> ed # 结束输入
执行多条命令:( ; ), ( && ), ( || )
例:
$ cmd1; cmd2; cmd3 # 从左到右依次执行
&& 与 || 则需要一点判断,与命令执行结果挂钩( $? ),0 ==> 执行成功, 其他 ==> 不成功
例:
$ cmd1 && cmd2 # 只有cmd1执行成功了,cmd2才会执行
$ cmd1 || cmd2 # 只有cmd1执行失败了,cmd2才会执行
$ cmd1 && cmd2 || cmd3 # 类似if-else语句,cmd1成功则执行cmd2,否则执行cmd3
管道:( | )
作为命令行结果的筛选;
- cut [-df]
例: $ echo $PATH | cut -d ":" -f 2 #意为将last的执行结果使用 “ :” 分割,并取得分割后的第2个元素
$ echo $PATH | cut -d ":" -f 3,6 #分割后取第3和第6个元素,若不存在则返回空行
- grep [-acinv] [–color=auto]
例: $ last | grep "root" # 筛选last命令的结果,将存在"root" 的行信息返回
$ last | grep -v "root" # 筛选last命令的结果,将不存在“root”的行信息返回
$ last | grep -c "root" # 将结果存在root的行署计算返回(count)
-i : 忽略查找字符串的大小写差异;
-n : 输出结果时顺便输出各行的行号;
–color=auto: 结果中的 查找字符串 使用特殊颜色显示;
-a : 将binary以text文件的形式输出;
grep 搭配正则表达式使用(重要):
拿 cat /etc/man* 的 stdout 来当例子:
(注意区分 系统通配符号 和 正则表达式符号 ,两者不可相提并论)
[cocan@bogon etc]$ cat /etc/man*
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
# Lines beginning with `#' are comments and are ignored. Any combination of
# tabs or spaces may be used as `whitespace' separators.
#
# There are three mappings allowed in this file:
# --------------------------------------------------------
# MANDATORY_MANPATH manpath_element
# MANPATH_MAP path_element manpath_element
# MANDB_MAP global_manpath [relative_catpath]
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
#---------------------------------------------------------
# set up PATH to MANPATH mapping
# ie. what man tree holds man pages for what binary directory.
#
# *PATH* -> *MANPATH*
#
MANPATH_MAP /bin /usr/share/man
MANPATH_MAP /usr/bin /usr/share/man
MANPATH_MAP /sbin /usr/share/man
MANPATH_MAP /usr/sbin /usr/share/man
MANPATH_MAP /usr/local/bin /usr/local/man
MANPATH_MAP /usr/local/bin /usr/local/share/man
MANPATH_MAP /usr/local/sbin /usr/local/man
MANPATH_MAP /usr/local/sbin /usr/local/share/man
MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
MANPATH_MAP /usr/games /usr/share/man
MANPATH_MAP /opt/bin /opt/man
MANPATH_MAP /opt/sbin /opt/man
#---------------------------------------------------------
# For a manpath element to be treated as a system manpath (as most of those
# above should normally be), it must be mentioned below. Each line may have
# an optional extra string indicating the catpath associated with the
# manpath. If no catpath string is used, the catpath will default to the
# given manpath.
#
# You *must* provide all system manpaths, including manpaths for alternate
# operating systems, locale specific manpaths, and combinations of both, if
# they exist, otherwise the permissions of the user running man/mandb will
# be used to manipulate the manual pages. Also, mandb will not initialise
# the database cache for any manpaths not mentioned below unless explicitly
# requested to do so.
#
# In a per-user configuration file, this directive only controls the
# location of catpaths and the creation of database caches; it has no effect
# on privileges.
#
# Any manpaths that are subdirectories of other manpaths must be mentioned
# *before* the containing manpath. E.g. /usr/man/preformat must be listed
# before /usr/man.
#
# *MANPATH* -> *CATPATH*
#
MANDB_MAP /usr/man /var/cache/man/fsstnd
MANDB_MAP /usr/share/man /var/cache/man
MANDB_MAP /usr/local/man /var/cache/man/oldlocal
MANDB_MAP /usr/local/share/man /var/cache/man/local
MANDB_MAP /usr/X11R6/man /var/cache/man/X11R6
MANDB_MAP /opt/man /var/cache/man/opt
#
#---------------------------------------------------------
# Program definitions. These are commented out by default as the value
# of the definition is already the default. To change: uncomment a
# definition and modify it.
#
#DEFINE pager less -s
#DEFINE cat cat
#DEFINE tr tr '\255\267\264\327' '\055\157\047\170'
#DEFINE grep grep
#DEFINE troff groff -mandoc
#DEFINE nroff nroff -mandoc -c
#DEFINE eqn eqn
#DEFINE neqn neqn
#DEFINE tbl tbl
#DEFINE col col
#DEFINE vgrind
#DEFINE refer refer
#DEFINE grap
#DEFINE pic pic -S
#
#DEFINE compressor gzip -c7
#---------------------------------------------------------
# Misc definitions: same as program definitions above.
#
#DEFINE whatis_grep_flags -i
#DEFINE apropos_grep_flags -iEw
#DEFINE apropos_regex_grep_flags -iE
#---------------------------------------------------------
# Section names. Manual sections will be searched in the order listed here;
# the default is 1, n, l, 8, 3, 0, 2, 5, 4, 9, 6, 7. Multiple SECTION
# directives may be given for clarity, and will be concatenated together in
# the expected way.
# If a particular extension is not in this list (say, 1mh), it will be
# displayed with the rest of the section it belongs to. The effect of this
# is that you only need to explicitly list extensions if you want to force a
# particular order. Sections with extensions should usually be adjacent to
# their main section (e.g. "1 1mh 8 ...").
#
SECTION 1 1p 8 2 3 3p 4 5 6 7 9 0p n l p o 1x 2x 3x 4x 5x 6x 7x 8x
#
#---------------------------------------------------------
# Range of terminal widths permitted when displaying cat pages. If the
# terminal falls outside this range, cat pages will not be created (if
# missing) or displayed.
#
#MINCATWIDTH 80
#MAXCATWIDTH 80
#
# If CATWIDTH is set to a non-zero number, cat pages will always be
# formatted for a terminal of the given width, regardless of the width of
# the terminal actually being used. This should generally be within the
# range set by MINCATWIDTH and MAXCATWIDTH.
#
#CATWIDTH 0
#
#---------------------------------------------------------
# Flags.
# NOCACHE keeps man from creating cat pages.
#NOCACHE
例1: 取出所有以‘MAN’为行首的行,并打印行号:
[cocan@bogon etc]$ cat /etc/man* | grep -n '^MAN'
20:MANDATORY_MANPATH /usr/man
21:MANDATORY_MANPATH /usr/share/man
22:MANDATORY_MANPATH /usr/local/share/man
29:MANPATH_MAP /bin /usr/share/man
30:MANPATH_MAP /usr/bin /usr/share/man
31:MANPATH_MAP /sbin /usr/share/man
32:MANPATH_MAP /usr/sbin /usr/share/man
33:MANPATH_MAP /usr/local/bin /usr/local/man
34:MANPATH_MAP /usr/local/bin /usr/local/share/man
35:MANPATH_MAP /usr/local/sbin /usr/local/man
36:MANPATH_MAP /usr/local/sbin /usr/local/share/man
37:MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
38:MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
39:MANPATH_MAP /usr/games /usr/share/man
40:MANPATH_MAP /opt/bin /opt/man
41:MANPATH_MAP /opt/sbin /opt/man
66:MANDB_MAP /usr/man /var/cache/man/fsstnd
67:MANDB_MAP /usr/share/man /var/cache/man
68:MANDB_MAP /usr/local/man /var/cache/man/oldlocal
69:MANDB_MAP /usr/local/share/man /var/cache/man/local
70:MANDB_MAP /usr/X11R6/man /var/cache/man/X11R6
71:MANDB_MAP /opt/man /var/cache/man/opt
例2:承接上例,在上例基础上筛选出行尾为man结尾的行,并打印行号:
- “.” 在正则表达式中,代表一个任意字符,它是确实存在此处的,以"."来代替罢了;
- “*” 在正则表达式中,描述一个字符出现的次数为 0 或 多次;
- “.*” 则代表匹配任意字符,这些字符出现了0次或多次均可;
- “$” 代表行尾, “^” 代表行首;
[cocan@bogon etc]$ cat /etc/man* | grep -n '^MAN.*man$'
20:MANDATORY_MANPATH /usr/man
21:MANDATORY_MANPATH /usr/share/man
22:MANDATORY_MANPATH /usr/local/share/man
29:MANPATH_MAP /bin /usr/share/man
30:MANPATH_MAP /usr/bin /usr/share/man
31:MANPATH_MAP /sbin /usr/share/man
32:MANPATH_MAP /usr/sbin /usr/share/man
33:MANPATH_MAP /usr/local/bin /usr/local/man
34:MANPATH_MAP /usr/local/bin /usr/local/share/man
35:MANPATH_MAP /usr/local/sbin /usr/local/man
36:MANPATH_MAP /usr/local/sbin /usr/local/share/man
37:MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
38:MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
39:MANPATH_MAP /usr/games /usr/share/man
40:MANPATH_MAP /opt/bin /opt/man
41:MANPATH_MAP /opt/sbin /opt/man
67:MANDB_MAP /usr/share/man /var/cache/man
例3:承上例,筛选以 “MAN” 行首,以 “bin” 为结尾的行,并输出行号;(注意是以bin结尾,而不是行尾)
[cocan@bogon etc]$ cat /etc/man* | grep -n '^MAN.*bin'
29:MANPATH_MAP /bin /usr/share/man
30:MANPATH_MAP /usr/bin /usr/share/man
31:MANPATH_MAP /sbin /usr/share/man
32:MANPATH_MAP /usr/sbin /usr/share/man
33:MANPATH_MAP /usr/local/bin /usr/local/man
34:MANPATH_MAP /usr/local/bin /usr/local/share/man
35:MANPATH_MAP /usr/local/sbin /usr/local/man
36:MANPATH_MAP /usr/local/sbin /usr/local/share/man
37:MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
38:MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
40:MANPATH_MAP /opt/bin /opt/man
41:MANPATH_MAP /opt/sbin /opt/man
例4: 输出 “MAN“ 出现了一次或多次的行,并输出行号;
- 使用 “{” 和 “}” 包裹住数字,用来描述前面的字符串的出现次数,若仅有一个数字,则规定了其出现下限(至少 出现几次),而不规定其上限;
- “{” 与 “}” 符号有特殊意义,所以在使用的时候需要转义"\"
[cocan@bogon etc]$ cat /etc/man* | grep -n 'MAN\{1\}'
12:# MANDATORY_MANPATH manpath_element
13:# MANPATH_MAP path_element manpath_element
14:# MANDB_MAP global_manpath [relative_catpath]
16:# every automatically generated MANPATH includes these fields
18:#MANDATORY_MANPATH /usr/src/pvm3/man
20:MANDATORY_MANPATH /usr/man
21:MANDATORY_MANPATH /usr/share/man
22:MANDATORY_MANPATH /usr/local/share/man
24:# set up PATH to MANPATH mapping
27:# *PATH* -> *MANPATH*
29:MANPATH_MAP /bin /usr/share/man
30:MANPATH_MAP /usr/bin /usr/share/man
31:MANPATH_MAP /sbin /usr/share/man
32:MANPATH_MAP /usr/sbin /usr/share/man
33:MANPATH_MAP /usr/local/bin /usr/local/man
34:MANPATH_MAP /usr/local/bin /usr/local/share/man
35:MANPATH_MAP /usr/local/sbin /usr/local/man
36:MANPATH_MAP /usr/local/sbin /usr/local/share/man
37:MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
38:MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
39:MANPATH_MAP /usr/games /usr/share/man
40:MANPATH_MAP /opt/bin /opt/man
41:MANPATH_MAP /opt/sbin /opt/man
64:# *MANPATH* -> *CATPATH*
66:MANDB_MAP /usr/man /var/cache/man/fsstnd
67:MANDB_MAP /usr/share/man /var/cache/man
68:MANDB_MAP /usr/local/man /var/cache/man/oldlocal
69:MANDB_MAP /usr/local/share/man /var/cache/man/local
70:MANDB_MAP /usr/X11R6/man /var/cache/man/X11R6
71:MANDB_MAP /opt/man /var/cache/man/opt
注意: 此处使用的grep的普通正则匹配字符串出现次数时,在大括号 { } 前后用了转义符号 \ ,而在扩展正则:egrep 中,在使用 { } 指定出现次数时候就不需要转义,切记切记。
例5:筛选不是以MAN为行首的行,输出行号
[cocan@bogon etc]$ cat /etc/man* | grep -vn '^MAN'
1:#
2:#
3:# This file is used by the man-db package to configure the man and cat paths.
4:# It is also used to provide a manpath for those without one by examining
5:# their PATH environment variable. For details see the manpath(5) man page.
6:#
7:# Lines beginning with `#' are comments and are ignored. Any combination of
8:# tabs or spaces may be used as `whitespace' separators.
9:#
...后续省略...
或者:
- "^” 符号在引号( ‘ ’ ) 内部表示行首, 在中括号 ( [ ] ) 内部代表取反(非)
- 中括号 ( [ ] ) 内部一般描述出现的字符,例如 [0-9] 代表出现0-9之间的任意数字,[a-zA-z] 则代表全体字母;
[cocan@bogon etc]$ cat /etc/man* | grep -n '^[^MAN]'
1:#
2:#
3:# This file is used by the man-db package to configure the man and cat paths.
4:# It is also used to provide a manpath for those without one by examining
5:# their PATH environment variable. For details see the manpath(5) man page.
6:#
7:# Lines beginning with `#' are comments and are ignored. Any combination of
8:# tabs or spaces may be used as `whitespace' separators.
9:#
...后续省略...
补充: 如何利用正则表达式来匹配 空行?
使用 ‘^$’ ,意为行首下一个匹配到的即为行尾,代表此行为空;
[cocan@bogon etc]$ cat /etc/man* | grep -n '^$'
1:
egrep 配合扩展正则表达式
扩展正则表达式主要多出了以下操作符:
操作符 | 意义 |
---|---|
+ | 代表其需要出现1或多次;如: egrep -n ‘go+d’ filename , 代表’o’将会出现一到多次 |
? | 代表其需要出现0或1次 如:egrep -n ‘a?dd’ ,则代表 ‘a’ 将出现1次或不出现 |
| | 意为或 (Or) ,可用在多个正则判断间的连接,如我要同时找good和dog两个字符串:egrep -n ‘good|dog’ filename |
() | 意为组,如我要匹配good或gd,则:egrep -n ‘g(oo)+d’ filename, 组内部的字符(串)同时也可以使用上述符号进一步修饰 |