1. shell概述
shell是一个命令行解释器,接收用户操作指令,然后调用操作系统内核.
shell还是一个功能丰富的编程语言
2. shell解析器
cat /etc/shells
有 sh bash 等
3. 入门操作
写shell脚本, 文件首行 #!/bin/bash 指定解析器
脚本的执行 sh + 绝对或相对路径
如果赋予了脚本可执行权限,则可以直接使用相对路径和绝对路径执行脚本文件
4. 变量
系统变量直接 $JAVA_HOME
显示当前shell变量: set
$# 显示脚本输入参数个数
$n n为数字,获取脚本第n个参数,$0代表脚本名称,10以上的参数用大括号 ${10}
$* 获取所有参数,当做一个整体
$@ 获取所有参数,分开对待
$? (小朋友你是否有很多问号???) 判断上一个命令是否正确执行,0正确,非0错误
5. 加减乘除
(2+3)*4=20
-sh-4.2$ expr `expr 2 + 3` \* 4
20
-sh-4.2$ echo $[(2+3)*4]
20
6. 买大买小买定离手
[ 条件 ] : 判断true 和false,条件前后有空格
字符串比较: =
整数比较: -lt (less than) 小于 -le (less equal) 小于等于
-eq( equal) 等于 -gt (greater than) 大于
-ge( greater equal) 大于等于 -ne (not equal) 不等
权限判断: -r 读 -w 写 -x 执行
文件判断: -f 文件 -d 目录 -e 文件存在
7. 流程一条龙
if [ 条件 ];then
xxx
fi
if [ 条件 ]
then
xxx
fi
case $变量 in
"p1")
xxx
;;
"p2")
xxx
;;
*)
xxx
;;
for(( 初始值;循环条件:变量控制 ))
do
xxx
done
for((变量 in v1 v2 v3))
do
xxx
done
while [ 条件 ]
do
xxx
done
8. 增删改查
8.1 cut
-sh-4.2$ cut --help
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.
Mandatory arguments to long options are mandatory for short options too.
-b, --bytes=LIST select only these bytes
-c, --characters=LIST select only these characters
-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter
-f, --fields=LIST select only these fields; also print any line
that contains no delimiter character, unless
the -s option is specified
-n with -b: don't split multibyte characters
--complement complement the set of selected bytes, characters
or fields
-s, --only-delimited do not print lines not containing delimiters
--output-delimiter=STRING use STRING as the output delimiter
the default is to use the input delimiter
--help display this help and exit
--version output version information and exit
Use one, and only one of -b, -c or -f. Each LIST is made up of one
range, or many ranges separated by commas. Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:
N N'th byte, character or field, counted from 1
N- from N'th byte, character or field, to end of line
N-M from N'th to M'th (included) byte, character or field
-M from first to M'th (included) byte, character or field
With no FILE, or when FILE is -, read standard input.
GNU coreutils online help:
For complete documentation, run: info coreutils 'cut invocation'
cut [参数] 文件名
默认分隔符为制表符
-f 列号,提取第几列
-d 分隔符,自定义分隔符分隔列
-sh-4.2$ echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0
-sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 1
-sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 2
usr
-sh-4.2$ echo $JAVA_HOME|cut -d '/' -f 3
lib
8.2 sed
sed 一次处理一行数据,处理时把当前处理的行存储在临时缓冲区,sed 开始处理缓冲区中的内容,处理完了输出
-sh-4.2$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
-b, --binary
does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
open files in binary mode (CR+LFs are not treated specially))
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help
display this help and exit
--version
output version information and exit
-e 在指令模式上进行编辑
a 新增,a后面可以接一行字符串在下一行出现
d 删除
s 查找替换
-sh-4.2$ cat test.txt
aaaa bbb
cccd dsd
fsdf fsdf fsdf fsdfs
-sh-4.2$ sed 'a 6666' test.txt
aaaa bbb
6666
cccd dsd
6666
fsdf fsdf fsdf fsdfs
6666
-sh-4.2$ sed '1a 6666' test.txt
aaaa bbb
6666
cccd dsd
fsdf fsdf fsdf fsdfs
-sh-4.2$ sed '/aa/d' test.txt
cccd dsd
fsdf fsdf fsdf fsdfs
-sh-4.2$ sed 's/aa/666/' test.txt
666aa bbb
cccd dsd
fsdf fsdf fsdf fsdfs
-sh-4.2$ sed 's/aa/666/g' test.txt
666666 bbb
cccd dsd
fsdf fsdf fsdf fsdfs
8.3 awk
把文件逐行读入,空格Wie默认分隔符进行切片,切开部分再进行分析处理
awk [参数] ‘pattern{action}’ filename
-F 指定输入文件分隔符
-v 赋值一个用户定义变量
-sh-4.2$ cat test.txt
aaaa bbb
cccd dsd
ff fd fsf fqq
666
777 888
-sh-4.2$ awk -F' ' '/^f/{print $2}' test.txt
fd
-sh-4.2$ cat test.txt
aaaa bbb
cccd dsd
ff fd fsf fqq
666
777 888
-sh-4.2$ awk -F' ' 'BEGIN{print "xxx,ddd"} {print} END{print "end 9999"}' test.txt
xxx,ddd
aaaa bbb
cccd dsd
ff fd fsf fqq
666
777 888
end 9999
-sh-4.2$ cat test.txt
111 aaaa bbb
222 cccd dsd
333 ff fd fsf fqq
666
777 888
-sh-4.2$ awk -F' ' -v i=10 '/[0-9]*/{print $1*i}' test.txt
1110
2220
3330
6660
7770
awk内置变量: FILENAME 文件名 NR 已读的记录数 NF 浏览记录的域个数(切割后,列的个数)
-sh-4.2$ cat test.txt
111 aaaa bbb
222 cccd dsd
333 ff fd fsf fqq
666
777 888
-sh-4.2$ awk -F ' ' '{print "filename:" FILENAME " readnum:" NR " columns:" NF}' test.txt
filename:test.txt readnum:1 columns:3
filename:test.txt readnum:2 columns:3
filename:test.txt readnum:3 columns:5
filename:test.txt readnum:4 columns:1
filename:test.txt readnum:5 columns:2
8.4 sort
sort 参数
-n 按照数值的大小排序
-r 以相反的顺序来排序
-t 设置排序时所用的分隔字符
-k 指定需要排序的列
-sh-4.2$ cat test.txt
2 3
3 1
5 4
1 5
3 0
4 2
-sh-4.2$ sort -n test.txt
1 5
2 3
3 0
3 1
4 2
5 4
-sh-4.2$ sort -nr test.txt
5 4
4 2
3 1
3 0
2 3
1 5
-sh-4.2$ sort -nk 2 test.txt
3 0
3 1
4 2
2 3
5 4
1 5
-sh-4.2$ sort -nrk 2 test.txt
1 5
5 4
2 3
4 2
3 1
3 0