初识SHELL脚本

SHELL简介:

  • 什么是shell?

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

  • 什么是shell脚本?

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

  • 为什么使用shell脚本?

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

  • 脚本的开发规范

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

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

2.脚本信息

#!/bin/bash
#Date:2018-12-25
#Author: westos-wsp
#Connect: 1278961488@qq.com
#Desc: This scripts is for...
#Version:1.0

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

  • 脚本执行方法

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

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

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

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

SHELL变量:

定义变量

  • 环境变量

  • 普通变量

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

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

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

  • 将命令的结果赋值给变量:
[root@server mnt]# CMD=`ls -l`
[root@server 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

[root@server mnt]# CMD=$(ls -l)
[root@server 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个参数
$#:获取脚本参数的总个数
$*:获取所有参数
$@:获取所有参数
$?:表示上条命令执行结果的返回值,0表示执行成功,非0表示执行失败
$$:获取当前shell进程号

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

[root@server mnt]# sh westos.sh hello westos
hello westos
[root@server mnt]# sh westos.sh hello redhat
hello redhat

[root@server mnt]# echo \${1..10} > westos.sh
[root@server mnt]# cat westos.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@server mnt]# sh westos.sh  {1..10}
1 2 3 4 5 6 7 8 9 10
[root@server mnt]# sh westos.sh  {a..z}
a b c d e f g h i a0
[root@server mnt]# cat 01.sh 
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#
# ${10}
[root@server mnt]# sh westos.sh  {a..z}
a b c d e f g h i j

$#:
[root@server mnt]# cat westos.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[root@server mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100
$?:
表示上条命令执行结果的返回值
0表示执行成功
非0表示执行失败

read用法:

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

打包日志:

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

变量的数值计算

 

  • expr命令(注意 * 直接用不能识别,需要进行转译,才可以执行,即在*前加\)
[root@server mnt]# a=123
[root@server mnt]# expr $a + 10
133
[root@server mnt]# expr $a - 10
113
[root@server mnt]# expr $a * 10
expr: syntax error
[root@server mnt]# expr $a \* 10
1230
[root@server mnt]# expr $a / 10
12
[root@server mnt]# expr $a % 10
3
  • $[ ]和$(())表达式
[root@server mnt]# echo $[a+10]
20
[root@server mnt]# echo $[a-10]
0
[root@server mnt]# echo $[a*10]
100
[root@server mnt]# echo $[a/10]
1
[root@server mnt]# echo $[a%10]
0
[root@server mnt]# echo $((a+10))
20
[root@server mnt]# echo $((a-10))
0

 

  • let命令(let命令在执行后会保存新的值)
[root@server mnt]# let a+=10
[root@server mnt]# echo $a
20
[root@server mnt]# let a-=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a*=10
[root@server mnt]# echo $a
100
[root@server mnt]# let a/=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a%=10
[root@server mnt]# echo $a
0

 

  • 小数运算工具bc
[root@server mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@server mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[root@server mnt]# echo 1.2+3.4 | bc
4.6
[root@server mnt]# echo 1.23+4.56 | bc
5.79

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

文本处理

  • grep,egrep

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

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

正则表达式:

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

^:匹配开头

$:匹配结尾

.:匹配任意字符

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

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

grep
    -i     ##忽略字母大小写
    -v    ##条件取反
    -c    ##统计匹配行数
    -q    ##静默,无任何输出
    -n    ##显示匹配结果所在的行号


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


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

[root@server ~]# egrep 'color(ful)?' 1.sh     ##末尾的ful最多出现一次,也可以没有
color color color
colorful,color
color color.
colorfulful?
  • 元字符:{ }
[root@server ~]# egrep '(we){3}' 1.sh      ##匹配出现字符"we"3次的行
rere wewewe
westos wewewewe Shell
[root@server ~]# egrep '(we){2,4}' 1.sh   ##匹配出现字符"we"2~4次的行
xcvb wewe asdawd
rere wewewe
westos wewewewe Shell
[root@server ~]#
[root@server ~]# egrep '(we){3,}' 1.sh     ##匹配出现字符"we"3次及3次以上的行
rere wewewe
westos wewewewe Shell
[root@server ~]# egrep '(we)[ab]' 1.sh    ##匹配字符"we"后有a或者b的行
weawe IPADDR
wea web wef
[root@server ~]# 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
[root@server ~]# ifconfig eth0 | grep "inet " | awk '{print $2}'
172.25.254.100

[root@server ~]# 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     ##指定要排序的列

[root@server ~]# sort westos         ##默认第1列排序

1
12
123
2
3
32
5
51
6
7

[root@server ~]# sort -n westos    ##数字大小排序

1
2
3
5
6
7
12
32
51
123

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

[root@server ~]# 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

[root@server ~]# 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


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

 

uniq命令:对重复字符处理

uniq
    -u    ##显示唯一的行
    -d    ##显示重复的行
    -c    ##每行显示一次并统计重复次数

[root@server ~]# 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


[root@server ~]# sort -n westos | uniq -d
2
10
31


[root@server ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55

练习:将/tmp目录中的文件取出最大的
[kiosk@foundation0 ~]$ 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


判断文件类型:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值