天天看点

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

SHELL简介:

  • 什么是shell?

shell是linux的一外壳,包在linux内核的外面,为用户和内核的交互提供了一个接口,当用户下达指令给操作系统的时候,实际上是把指令告诉shell,经过shell的解释,处理后让内核做出相应的动作,系统的回应和输出的信息也由shell处理,然后显示在用户的屏幕上。

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令
  • 什么是shell脚本?

简单来说,当命令或者程序不在命令行执行,而是通过一个程序文件来执行,这个程序就被称为shell脚本,也就是在shell脚本里设置了多条命令,语句,循环控制,然后将这些命令一次性执行完毕,这种通过文件执行命令的方式称为非交互式。

  • 为什么使用shell脚本?

  1. 适合处理操作系统的底层业务,有众多系统命令为其做支撑(还有文本处理三兄弟 grep,sed,awk)
  2. 适合处理纯文本文件,linux中许多服务配置文件,启动脚本,都是纯文本(httpd,nfs,nginx,lvs)
  3. linux系统脚本用shell开发更简单
  • 如何查看系统默认shell?

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令
  • 脚本的开发规范

1.第一行:#!/bin/bash

指定解释器:由哪个程序来执行脚本内容

#!:幻数

2.脚本信息

#!/bin/bash

#Date:2018-12-25

#Author: westos-wsp

#Connect: [email protected]

#Desc: This scripts is for...

#Version:1.0

3.脚本名最好以 .sh 结尾

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令
  • 脚本执行方法

sh script.sh | bash script.sh     没有执行权限时

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

path/script.sh | ./script.sh         绝对路径,当前目录下

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

source script.sh | . script.sh     这种方式会使用source或.号来读如指定shell文件,并会把其他shell中的变量值或函数返回给父shell继续使用,前两种方式,在执行脚本的时候,会默认打开一个新的shell,而新shell的变量值和函数不会返回给父shell

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

练习:写一个安装,启动并开机自启的apache脚本。

SHELL变量:

定义变量

  • 环境变量
初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令
  • 普通变量

变量名=hello         不能有空格

变量名=‘hello’       对单引号内内容不解释,可以有空格(如果需要原样输出就用单引号)

变量名=“hello”       对双引号内内容解释,可以有空格

[[email protected] ~]# a=hello
[[email protected] ~]# echo $a
hello
[[email protected] ~]# b='hello'
[[email protected] ~]# echo $b
hello
[[email protected] ~]# c="hello"
[[email protected] ~]# echo $c
hello
[[email protected] ~]# a=westos-$a
[[email protected] ~]# echo $a
westos-hello
[[email protected] ~]# b='westos-$a'
[[email protected] ~]# echo $b
westos-$a
[[email protected] ~]# c="westos-$a"
[[email protected] ~]# echo $c
westos-westos-hello
[[email protected] ~]# a=westos hello
bash: hello: command not found...
[[email protected] ~]# a="westos hello"
[[email protected] ~]# echo $a
westos hello
           

注意:建议没有特别要求时,字符串都加双引号,需要原样输出就加单引号

  • 将命令的结果赋值给变量:
[[email protected] mnt]# CMD=`ls -l`
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh

[[email protected] mnt]# CMD=$(ls -l)
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh
           
  • 特殊变量

$0:获取shell脚本文件名,如果执行时包含路径,则输出脚本路径

$n(>0):获取脚本的第n个参数

$#:获取脚本参数的总个数

$*:获取所有参数

[email protected]:获取所有参数

$?:

表示上条命令执行结果的返回值,0表示执行成功,非0表示执行失败

$$:获取当前shell进程号

$0:
[[email protected] mnt]# cat westos.sh
#!/bin/bash
echo $0
[[email protected] mnt]# sh westos.sh
westos.sh
[[email protected] mnt]# /mnt/westos.sh
/mnt/westos.
           
$n(n>0):
[[email protected] mnt]# cat westos.sh
#!/bin/bash
echo $1 $2

[[email protected] mnt]# sh westos.sh hello westos
hello westos
[[email protected] mnt]# sh westos.sh hello redhat
hello redhat

[[email protected] mnt]# echo \${1..10} > westos.sh
[[email protected] mnt]# cat westos.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[[email protected] mnt]# sh westos.sh  {1..10}
1 2 3 4 5 6 7 8 9 10
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i a0
[[email protected] mnt]# cat 01.sh 
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#
# ${10}
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i j
           
$#:
[[email protected] mnt]# cat westos.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[[email protected] mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100
           
$?:
表示上条命令执行结果的返回值
0表示执行成功
非0表示执行失败
           

read用法:

[[email protected] mnt]# read str
westos hello
[[email protected] mnt]# echo $str
westos hello
[[email protected] mnt]# read -p "请输入一个整数:" i
请输入一个整数:10
           

打包日志:

[[email protected] mnt]# tar zcf log_$(date +%F).tar.gz /var/log/
tar: Removing leading `/' from member names
[[email protected] mnt]# ls
log_2018-12-22.tar.gz  test.sh  westos.sh

           

变量的数值计算

  • expr命令(注意 * 直接用不能识别,需要进行转译,才可以执行,即在*前加\)
[[email protected] mnt]# a=123
[[email protected] mnt]# expr $a + 10
133
[[email protected] mnt]# expr $a - 10
113
[[email protected] mnt]# expr $a * 10
expr: syntax error
[[email protected] mnt]# expr $a \* 10
1230
[[email protected] mnt]# expr $a / 10
12
[[email protected] mnt]# expr $a % 10
3
           
  • $[ ]和$(())表达式
[[email protected] mnt]# echo $[a+10]
20
[[email protected] mnt]# echo $[a-10]
0
[[email protected] mnt]# echo $[a*10]
100
[[email protected] mnt]# echo $[a/10]
1
[[email protected] mnt]# echo $[a%10]
0
[[email protected] mnt]# echo $((a+10))
20
[[email protected] mnt]# echo $((a-10))
0
           
  • let命令(let命令在执行后会保存新的值)
[[email protected] mnt]# let a+=10
[[email protected] mnt]# echo $a
20
[[email protected] mnt]# let a-=10
[[email protected] mnt]# echo $a
10
[[email protected] mnt]# let a*=10
[[email protected] mnt]# echo $a
100
[[email protected] mnt]# let a/=10
[[email protected] mnt]# echo $a
10
[[email protected] mnt]# let a%=10
[[email protected] mnt]# echo $a
0
           
  • 小数运算工具bc
[[email protected] mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[[email protected] mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[[email protected] mnt]# echo 1.2+3.4 | bc
4.6
[[email protected] mnt]# echo 1.23+4.56 | bc
5.79
           

练习:计算两个数的加减乘除

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

文本处理

  • grep,egrep

grep 命令是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行

由正则表达式或者字符及基本文本字符所编写的过滤条件全面搜索研究正则表达式并显示出来

正则表达式:

由一类特殊字符及文本字符所编写的模式,其中有些字符不表示字符的字面意思,而表示控制或通配的功能^

^:匹配开头

$:匹配结尾

.:匹配任意字符

[ ]:匹配括号中的任意字符

[^]:匹配不在括号中的任意字符

grep

    -i     ##忽略字母大小写

    -v    ##条件取反

    -c    ##统计匹配行数

    -q    ##静默,无任何输出

    -n    ##显示匹配结果所在的行号

-q:
[[email protected] mnt]# grep '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.250 content.example.com
YES
[[email protected] mnt]# grep -q '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
YES
-c:
[[email protected] mnt]# egrep -c '/sbin/nologin' /etc/passwd
35
           
  • 基本元字符:^ $
[[email protected] mnt]#egrep -m10 '/sbin/nologin' /etc/passwd   ##匹配前10行
[[email protected] mnt]# egrep -m10 'sbin$'   wsp                          ##匹配前10行中以sbin结尾的行
root sbin
root sbin sbin
[[email protected] mnt]# cat wsp
root sbin
root sbin root
root sbin sbin
           
  • 基本元字符:.    ##过滤非空行
[[email protected] mnt]# egrep '.' wsp
root sbin
root sbin root
root sbin sbin
root
awd
awd
awd


[[email protected] mnt]# egrep -v '.' wsp    ##过滤空行
[[email protected] mnt]# egrep  '^$' wsp    ##过滤空行
           
  • 基本元字符: + ? *
[[email protected] ~]# egrep 'f+' 1.sh     ##输出包括f,ff,fff....,即至少出现一次
colorful,color
colorfulful?
stuf
stuff
stufff
stuffff
stufawd
we adw dfg awda
wea web wef

[[email protected] ~]# egrep 'color(ful)?' 1.sh     ##末尾的ful最多出现一次,也可以没有
color color color
colorful,color
color color.
colorfulful?
           
  • 元字符:{ }
[[email protected] ~]# egrep '(we){3}' 1.sh      ##匹配出现字符"we"3次的行
rere wewewe
westos wewewewe Shell
[[email protected] ~]# egrep '(we){2,4}' 1.sh   ##匹配出现字符"we"2~4次的行
xcvb wewe asdawd
rere wewewe
westos wewewewe Shell
[[email protected] ~]#
[[email protected] ~]# egrep '(we){3,}' 1.sh     ##匹配出现字符"we"3次及3次以上的行
rere wewewe
westos wewewewe Shell
[[email protected] ~]# egrep '(we)[ab]' 1.sh    ##匹配字符"we"后有a或者b的行
weawe IPADDR
wea web wef
[[email protected] ~]# egrep '[A-Z]' 1.sh         ##匹配含有大写字母的行
weawe IPADDR
westos wewewewe Shell
           
  • cut命令
cut -d    ##指定分隔符
cut -d : -f 1-3 /etc/passwd    ##指定分隔符为:,显示第1到3列
cut -c 1,4 /etc/passwd        ##显示第一和第四个字符
           

练习:获取主机IP

[[email protected] ~]# ifconfig eth0 | grep "inet " | awk '{print $2}'

172.25.254.100

[[email protected] ~]# ifconfig eth0 | grep "inet " | cut -d " " -f 10

172.25.254.100

练习:检测网络

ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down

sort命令:排序

sort

    -n    ##纯数字排序

    -r     ##倒序

    -u     ##去掉重复数字

    -o     ##输出到指定文件中

    -t      ##指定分隔符

    -k     ##指定要排序的列

[[email protected] ~]# sort westos         ##默认第1列排序

1
12
123
2
3
32
5
51
6
7

[[email protected] ~]# sort -n westos    ##数字大小排序

1
2
3
5
6
7
12
32
51
123

[[email protected] ~]# sort -u westos    ##去除重复数字
1
12
123
2
3
32
5
51
6
7

[[email protected] ~]# sort -t : -k 2 westos     ##指定分隔符为:,第二列默认排序
2:0
12:10
2:12
3:2
51:20
5:21
123:22
32:31
5:4
6:4
1:5
51:55
123:66
7:79

[[email protected] ~]# sort -nt : -k 2 westos   ##指定分隔符为:,第二列按数字大小排序
2:0
3:2
5:4
6:4
1:5
12:10
2:12
51:20
5:21
123:22
32:31
51:55
123:66
7:79


[[email protected] ~]# sort -nt : -k 2 westos -o /mnt/file      ##将排序后的保存到/mnt/file
           

uniq命令:对重复字符处理

uniq

    -u    ##显示唯一的行

    -d    ##显示重复的行

    -c    ##每行显示一次并统计重复次数

[[email protected] ~]# sort -n westos | uniq -c
      1 0
      1 1
      2 2
      1 4
      1 6
      1 9
      2 10
      1 20
      1 22
      2 31
      1 55


[[email protected] ~]# sort -n westos | uniq -d
2
10
31


[[email protected] ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55

           

练习:将/tmp目录中的文件取出最大的

[[email protected] ~]$ ls -Sl /tmp/ | head -2 | cut -d " " -f 9

条件判断--test命令

test "$a" == "$b" 等同于 [ "$a" == "$b" ]

[ "$a" = "$b" ]        ##等于

[ "$a" != "$b" ]     ##不等于

[ "$a" -eq "$b" ]    ##等于

[ "$a" -ne "$b" ]    ##不等于

[ "$a" -le "$b" ]    ##小于等于

[ "$a" -ge "$b" ]    ##大于等于

[ "$a" -gt "$b" ]    ##大于

[ "$a" -lt "$b" ]    ##小于

[ "$a" -ne "$b" -a "$a" -gt "$b" ]    ##-a必须条件都满足

[ "$a" -ne "$b" -o"$a" -gt "$b" ]    ##-a条件至少满足一个

[ -z "$a" ]        ##是否为空

[ -e "file" ]        ##是否存在

[ -f "file" ]        ##普通文件

[ -b "file" ]        ##块设备

[ -S "file" ]        ##套接字

[ -c "file" ]        ##字符设备

[ -L "file" ]        ##软链接

练习

判断输入的数字是否在10以内

1.输入是否为空

2.是否在10以内

3.1<$a<10 --> yes

4.$a<1 $a>10 --> no

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

判断文件类型:

初识SHELL脚本SHELL简介:SHELL变量:变量的数值计算文本处理sort命令:排序uniq命令:对重复字符处理条件判断--test命令

继续阅读