dmtsai@study ~]$ vim ~/.vimrc "这个文件的双引号 (") 是批注 set hlsearch "高亮度反白 set backspace=2 "可随时用退格键删除 set autoindent "自动缩排 set ruler "可显示最后一列的状态 set showmode "左下角那一列的状态 set nu "可以在每一列的最前面显示行号啦! set bg=dark "显示不同的底色色调 syntax on "进行语法检验,颜色显示。
范例一:查询一下 ls 这个指令是否为 bash 内建? [dmtsai@study ~]$ type ls ls is aliased to `ls --color=auto' <==未加任何参数,列出 ls 的最主要使用情况
[dmtsai@study ~]$ type cd cd is a shell builtin <==看到了吗? cd 是 shell 内建指令
[dmtsai@study ~]$ type -t ls alias <==仅列出 ls 执行时的依据 [dmtsai@study ~]$ type -a ls ls is aliased to `ls --color=auto' <==最先使用 aliase ls is /usr/bin/ls <==还有找到外部指令在 /bin/ls
显示登录用户的信息
1 2 3 4 5 6
[dmtsai@study ~]$ last root pts/1 192.168.201.101 Sat Feb 7 12:35 still logged in root pts/1 192.168.201.101 Fri Feb 6 12:13 - 18:46 (06:33) root pts/1 192.168.201.254 Thu Feb 5 22:37 - 23:53 (01:16)
# 列出你目前身份(假设为一般账号)的所有限制数据数值 [dmtsai@study ~]$ ulimit -a core file size (blocks, -c) 0 <==只要是 0 就代表没限制 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited <==可建立的单一文件的大小 pending signals (-i) 4903 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 <==同时可开启的文件数量 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 4096 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
对于设置的配额,只有注销并重新登入当前shell,才能取消配额
命令别名
1
alias [别名]='指令 选项...'
相当于创建一个指令
查看已有别名(常用别名)
1 2 3 4 5 6 7 8 9 10 11
[dmtsai@study ~]$ alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias lm='ls -al | more' alias ls='ls --color=auto' alias rm='rm -i' alias vi='vim' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde
利用 cat -A 显示出所有特殊按键,最后以 col 将 [tab] 转成空白 [dmtsai@study ~]$ cat -A /etc/man_db.conf <==此时会看到很多 ^I 的符号,那就是 tab [dmtsai@study ~]$ cat /etc/man_db.conf | col -x | cat -A | more
vim /etc/issue \S (terminal: \l) Date: \d \t Kernel \r on an \m Welcome!
CentOS Linux 7 (Core) (terminal: tty3) Date: 2015-07-08 17:29:19 Kernel 3.10.0-229.el7.x86_64 on an x86_64 Welcome!
转义字符
说明
\d
本地端时间的日期
\l
显示第几个终端机接口
\m
显示硬件的等级 (i386/i486/i586/i686…)
\n
显示主机的网络名称
\O
显示 domain name
\r
操作系统的版本 (相当于 uname -r)
\t
显示本地端时间的时间;
\S
操作系统的名称
\v
操作系统的版本
当使用telnet连接到主机时,会显示 /etc/issue.net 配置的信息
在 /etc/motd 中配置的信息,会在用户 登入后 显示提示信息
1 2 3 4 5 6 7 8 9
[root@study ~]# vim /etc/motd Hello everyone, Our server will be maintained at 2015/07/10 0:00 ~ 24:00. Please don't login server at that time. ^_^
Last login: Wed Jul 8 23:22:25 2015 from 127.0.0.1 Hello everyone, Our server will be maintained at 2015/07/10 0:00 ~ 24:00. Please don't login server at that time. ^_^
[dmtsai@study ~]$ cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then <==底下这三行在判断并读取 ~/.bashrc . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin <==底下这几行在处理个人化设定 export PATH
# 若两个标准输出都指向同一个文件,则输出的信息回交叉写入,造成次序错乱。 [dmtsai@study ~]$ find /home -name .bashrc > list 2> list <==错误 [dmtsai@study ~]$ find /home -name .bashrc > list 2>&1 <==正确 [dmtsai@study ~]$ find /home -name .bashrc &> list <==正确
输入
< 文件路径 表示用文件的内容代替键盘输入
<< 控制字符:控制字符表示输入结束的标识字符
1 2 3 4 5 6 7
[dmtsai@study ~]$ cat > catfile << "eof" > This is a test. > OK now stop > eof <==输入这关键词,立刻就结束而不需要输入 [ctrl]+d [dmtsai@study ~]$ cat catfile This is a test. OK now stop <==只有这两行,不会存在关键词那一行!
tzj@LAPTOP-070162A7:~$ last reboot system boot 6.6.87.2-microso Sun Mar 15 11:21 still running reboot system boot 6.6.87.2-microso Sun Mar 15 09:25 still running reboot system boot 6.6.87.2-microso Sun Mar 15 09:14 still running ...
wtmp begins Wed Nov 5 21:11:53 2025
范例一:使用 last 将账号列出,仅取出账号栏,进行排序后仅取出一位; tzj@LAPTOP-070162A7:~$ last | cut -d ' ' -f1 | sort | uniq
[dmtsai@study ~]$ grep -n 'the' regular_express.txt 8:I can't finish the test. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "glad". 18:google is the best tools for search keyword. # 反向搜索 [dmtsai@study ~]$ grep -vn 'the' regular_express.txt # 忽略大小写 [dmtsai@study ~]$ grep -in 'the' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good. 12:the symbol '*' is represented as start. 14:The gd software is a library for drafting programs. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "glad". 18:google is the best tools for search keyword.1
# oo前不要g [dmtsai@study ~]$ grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes!
反向选择时,也可以是个数组
1 2 3 4 5 6
# oo前不要所有小写字符 [dmtsai@study ~]$ grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only. # 等价于 [dmtsai@study ~]$ grep -n '[^[:lower:]]oo' regular_express.txt
匹配行首与行尾字符
行首 ^word
待搜索的字符串在行首
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 匹配行首为the的行 [dmtsai@study ~]$ grep -n '^the' regular_express.txt # 匹配行首为小写字符的行 [dmtsai@study ~]$ grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as start. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go. ##等价于 [dmtsai@study ~]$ grep -n '^[[:lower:]]' regular_express.txt # 匹配行首不为字母的行 grep -n '^[^a-zA-Z]' regular_express.txt
行尾 word$
待搜索的字符串在行尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#搜索行尾为.的行 [dmtsai@study ~]$ grep -n '\.$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 11:This window is clear. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "glad". 17:I like dog. 18:google is the best tools for search keyword. 20:go! go! Let's go.
[dmtsai@study ~]$ grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The world <Happy> is the same with "glad".
1 2 3 4 5 6 7 8
# 至少两个o 以上的字符串 [dmtsai@study ~]$ grep -n 'ooo*' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes!
0个或任意多个字符
1
.* 就代表零个或多个任意字符
1 2 3 4 5 6
[dmtsai@study ~]$ grep -n 'g.*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let's go.
限制RE字符的重复次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 查找出现2次o的字符串 [dmtsai@study ~]$ grep -n 'o\{2\}' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes! # 查找出现2~5次o的字符串 [dmtsai@study ~]$ grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword. # 查找出现2次以上o的字符串 [dmtsai@study ~]$ grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!
工具-sed
用于分析标准输入的工具,按行对数据增删改查
sed选项说明
选项
参数
含义
-n
在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到屏幕上。 如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出来。
root@LAPTOP-070162A7:/home/tzj# cat printf.txt -A Name Chinese English Math Average$ DmTsai 80 60 92 77.33$ VBird 75 55 80 70.00$ Ken 60 90 70 73.33$
1 2 3 4 5
root@LAPTOP-070162A7:/home/tzj# printf '%s\t %s\t %s\t %s\t %s\t \n' $(cat printf.txt) Name Chinese English Math Average DmTsai 80 60 92 77.33 VBird 75 55 80 70.00 Ken 60 90 70 73.33
使用脚本名或者 sh 脚本名 的方式,实际上,是在子进程的bash中运行,当子进程完成后,子进程的各项变量或动作就会终止,并不会传回到父进程。
1 2 3 4 5 6 7 8
[dmtsai@study bin]$ echo ${firstname} ${lastname} <==确认了,这两个变量并不存在喔! [dmtsai@study bin]$ sh showname.sh Please input your first name: VBird <==这个名字是鸟哥自己输入的 Please input your last name: Tsai Your full name is: VBird Tsai <==看吧!在 script 运作中,这两个变数有生效 [dmtsai@study bin]$ echo ${firstname} ${lastname} <==事实上,这两个变量在父程序的 bash 中还是不存在的!
在父进程执行
source <脚本名>
1 2 3 4 5 6
[dmtsai@study bin]$ source showname.sh Please input your first name: VBird Please input your last name: Tsai Your full name is: VBird Tsai [dmtsai@study bin]$ echo ${firstname} ${lastname} VBird Tsai <==嘿嘿!有数据产生喔!
source会在父进程执行脚本中的各个动作
脚本内的指令执行
指令从上至下执行,从左到右分析与执行
指令、选项、参数间多个空白会被视为一个(Tab也属于空白)
空白行会被忽略
读取到回车(CR),则执行当前行的指令
若一行指令太多,可以用 \[回车] 延伸到下一行
# 后的内容为注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 1. 让使用者输入文件名,并取得 fileuser 这个变量; echo -e "I will use 'touch' command to create 3 files." # 纯粹显示信息 read -p "Please input your filename: " fileuser # 提示使用者输入 # 2. 为了避免使用者随意按 Enter ,利用变量功能分析档名是否有设定? filename=${fileuser:-"filename"} # 开始判断有否配置文件名 # 3. 开始利用 date 指令来取得所需要的档名了; date1=$(date --date='2 days ago' +%Y%m%d) # 前两天的日期 date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期 date3=$(date +%Y%m%d) # 今天的日期 file1=${filename}${date1} # 底下三行在配置文件名 file2=${filename}${date2} file3=${filename}${date3}
read -p "Please input your first name: " firstname # 提示使用者输入 read -p "Please input your last name: " lastname # 提示使用者输入 echo -e "\nYour full name is: ${firstname} ${lastname}" # 结果由屏幕输出
3.4.3 数值运算
简单运算
$((计算式)) ,但bash shell只支持整数运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/bash # Program: # User inputs 2 integer numbers; program will cross these two numbers. # History: # 2015/07/16 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "You SHOULD input 2 numbers, I will multiplying them! \n" read -p "first number: " firstnu read -p "second number: " secnu
total=$((${firstnu}*${secnu}))
echo -e "\nThe result of ${firstnu} x ${secnu} is ==> ${total}"
对于整数运算,等价于 declare -i total=${firstnu}*${secnu}
bash shell $(()) 支持的数值运算类型为 +、-、*、/、%
bc
支持浮点数运算
因设计从标准输入读取表达式,所以通常配合 echo 与管道指令使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/bin/bash # Program: # User input a scale number to calculate pi number. # History: # 2015/07/16 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "This program will calculate pi value. \n" echo -e "You should input a float number to calculate pi value.\n" read -p "The scale number (10~10000) ? " checking num=${checking:-"10"} # 开始判断有否有输入数值
echo -e "Starting calcuate pi value. Be patient."
time echo "scale=${num}; 4*a(1)" | bc -lq
4*a(1) 是 bc 主动提供的一个计算 pi 的函数
3.4.4 逻辑判断式
test
判断符号
test指令
基本用法
test 指令执行完成后,并不会直接显示执行结果
可以通过 $?&& 或 || 间接引用执行结果
判断文件是否存在或文件类型
1
test [-选项] 文件名
选项
作用
-e
判断文件名是否存在
-f
判断类型是否为file
-d
判断是否为目录
-b
是否为块设备
-c
是否为字符设备
-S
是否为Socket设备
-p
是否为FIFO设备
-L
是否为连接文件
判断文件权限
1
test [-选项] 文件名
选项
作用
-r
判断是否有r权限
-w
是否有w权限
-x
是否有x权限
-u
是否具有SUID属性
-g
是否具有GUID属性
-k
是否具有Sticky bit属性
-s
是否为空白文件
文件对比(新旧与链接文件对比)
1
test file1 -nt file2
选项
作用
-nt
判断file1是否比file2新
-ot
判断file1是否比file旧
-ef
判断两个文件实际上是否为一个文件(inode)
两个整数间的判断
1
test n1 -eq n2
选项
作用
-eq
数值是否相等
-ne
数值是否不等
-gt
n1大于n2
-lt
n1小于n2
-ge
n1大于等于n2
-le
n1小于等于n2
字符串判断(非空或者两串是否相等)
选项
作用
test -z string
判断string是否为0,若string为空字符串,则为true
test -n string
判断string是否为非0,空则为false
test str1 == str2
是否相等
test str1 != str2
是否不等
多重条件判断
1 2 3 4
-a 两个判断同时成立,才返回true(and) eg: test -r file -a -x file file同时具有r与x权限才会回传true
1 2 3
-o 两个判断一个成立,返回true(or) eg: test -r file -o -x file
#!/bin/bash # Program: # User input a filename, program will check the flowing: # 1.) exist? 2.) file/directory? 3.) file permissions # History: # 2015/07/16 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # 1. 让使用者输入档名,并且判断使用者是否真的有输入字符串? echo -e "Please input a filename, I will check the filename's type and permission. \n\n" read -p "Input a filename : " filename test -z ${filename} && echo "You MUST input a filename." && exit 0 # 2. 判断文件是否存在?若不存在则显示讯息并结束脚本 test ! -e ${filename} && echo "The filename '${filename}' DO NOT exist" && exit 0 # 3. 开始判断文件类型与属性 test -f ${filename} && filetype="regulare file" test -d ${filename} && filetype="directory" test -r ${filename} && perm="readable" test -w ${filename} && perm="${perm} writable" test -x ${filename} && perm="${perm} executable" # 4. 开始输出信息! echo "The filename: ${filename} is a ${filetype}" echo "And the permissions for you are : ${perm}"