SHELL脚本------基础知识

一、shell的简单了解

一、什么是shell?

shell也是操作系统中的一个软件,它包在linux内核的外面,为用户和内核之间的交互提供了一个接口,系统中的命令用shell去解释shell接受系统回应的输出并显示其到屏幕中。

二、什么是shell脚本?

1.简单的说,当命令或者程序不在命令行执行,而是通过一个程序文件来执行,这个程序就被称为shell脚本。

2.也就是在shell脚本里内置了多条命令,语句,循环控制,然后将这些命令一次性执行完毕,这种通过文件执行命令的方式称为非交互式。

三、为什么使用shell脚本?

1.适合处理操作系统底层的业务,有众多系统命令为其做支撑(还有文本处理三兄弟grep,sed,awk)

2.适合处理纯文本文件,linux中许多服务配置文件,启动脚本,都是纯文本(httpd,nfs,mysql,nginx,lvs)

3.linux系统脚本用shell开发更简单

四、如何查看系统默认shell?

五、脚本的开发规范

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 结尾

六、 脚本执行方法

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

2.path/script.sh | ./script.sh    绝对路径,当前目录下(绝对路径,需要给脚本添加执行权限)

3.source script.sh | . script.sh    这种方式会使用source或.号来读如指定shell文件,并会把其他shell中的变量值或函数返回给父shell继续使用

前两种方式,在执行脚本的时候,会默认打开一个新的shell,而新shell的变量值和函数不会返回给父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


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

2. 将命令的结果赋值给变量:
  变量名 = 命令
   b=ls或 b=$(ls)

二、特殊变量

$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.sh

$n(>0):

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

$#

[root@server2 mnt]# vim pash.sh 
[root@server2 mnt]# sh pash.sh {1..100}
1 2 3 4 5 6 7 8 9 10
100
[root@server2 mnt]# cat pash.sh 
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#

三、read用法

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

四、打包日志

三、变量的数值计算

一、expr命令

注意 * 直接用不能识别,需要进行转译,才可以执行,即在*前加\。

[root@server2 mnt]# a=123
[root@server2 mnt]# expr $a + 10
133
[root@server2 mnt]# expr $a - 10
113
[root@server2 mnt]# expr $a \* 10
1230
[root@server2 mnt]# expr $a / 10
12
[root@server2 mnt]# expr $a % 10
3

二、$[]$(())表达式

[root@server2 mnt]# a=10
[root@server2 mnt]# echo $[a+10]
20
[root@server2 mnt]# echo $[a-10]
0
[root@server2 mnt]# echo $[a*10]
100
[root@server2 mnt]# echo $[a/10]
1
[root@server2 mnt]# echo $[a%10]
0
[root@server2 mnt]# echo $((a+10))
20
[root@server2 mnt]# echo $((a-10))
0
[root@server2 mnt]# echo $((a*10))
100
[root@server2 mnt]# echo $((a/10))
1
[root@server2 mnt]# echo $((a%10))
0

三、 let命令

注意:let命令在执行后会保存新的值

[root@server2 mnt]# let a+=10
[root@server2 mnt]# echo $a
20
[root@server2 mnt]# let a-=10
[root@server2 mnt]# echo $a
10
[root@server2 mnt]# let a*=10
[root@server2 mnt]# echo $a
100
[root@server2 mnt]# let a/=10
[root@server2 mnt]# echo $a
10
[root@server2 mnt]# let a%=10
[root@server2 mnt]# echo $a
0

四、小数运算工具bc

[root@server2 mnt]# echo 1.2+3.4 | bc
4.6
[root@server2 mnt]# echo 1.23+4.56 | bc
5.79
[root@server2 mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@server2 mnt]# echo "scale=2;1.23*4.56" | bc
5.60

五、练习题

写脚本计算两个数的加减乘除

四、文本处理

一、grep,egrep

Grep定义

  • grep 命令是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行
  • 由正则表达式或者字符及基本文本字符所编写的过滤条件
  • 全面搜索研究正则表达式并显示出来

Grep参数

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

-q:

[root@server2 mnt]# grep '172.25.254.254' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
YES
[root@server2 mnt]# grep -q '172.25.254.254' /etc/hosts && echo 'YES' || echo 'NO'
YES

-c:

[root@localhost mnt]# egrep -c '/sbin/nologin' /etc/passwd
34

基本元字符:^ $

[root@server2 mnt]# egrep '^root' /etc/passwd   匹配以字符串 root 开头
root:x:0:0:root:/root:/bin/bash
[root@server2 mnt]# vim wsp
[root@server2 mnt]# cat wsp 
root sbin
root sbin root
root sbin sbin
[root@server2 mnt]# egrep -m10 'sbin$' wsp      匹配以字符串 sbin 结尾
root sbin
root sbin sbin

基本元字符:'.' 过滤非空行

[root@server2 mnt]# vim wsp 
[root@server2 mnt]# egrep '.' wsp 
root sbin
root sbin root
root sbin sbin
root
awd
awd
awd

过滤空行

[root@server2 mnt]# egrep -v '.' wsp 过滤空行
[root@server2 mnt]# egrep '^$' wsp   过滤空行  (开始结束)

基本元字符: + ? *

[root@server2 mnt]# vim 1.sh
[root@server2 mnt]# cat 1.sh 
color color color
colorful,color
color color.
colorfulful?
stuf
stuff
stufff
stuffff
stufawd

we adw  dfg  awda
xcvb  wewe  asdawd
weawe IPADDR
rere wewewe
westos wewewewe Shell
wea web wef

[root@server2 mnt]# egrep 'f+' 1.sh 输出包括f,ff,fff...,即至少出现一次
colorful,color
colorfulful?
stuf
stuff
stufff
stuffff
stufawd
we adw  dfg  awda
wea web wef

[root@server2 mnt]# egrep 'color(ful)?' 1.sh  末尾的ful最多出现一次,也可以没有
color color color
colorful,color
color color.
colorfulful?

元字符:{}

[root@server2 mnt]# egrep '(we){3}' 1.sh     精确匹配三个we所在行
rere wewewe
westos wewewewe Shell
[root@server2 mnt]# egrep '(we){2,4}' 1.sh   匹配2-4个we所在行
xcvb  wewe  asdawd
rere wewewe
westos wewewewe Shell
[root@server2 mnt]# egrep '(we){3,}' 1.sh    匹配3个及以上we所在行
rere wewewe
westos wewewewe Shell
[root@server2 mnt]# egrep '(we)[ab]' 1.sh    匹配we后为a或者b所在行
weawe IPADDR
wea web wef
[root@server2 mnt]# 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@localhost mnt]# ifconfig eth0 | grep "inet " | awk '{print $2}'
172.25.254.102
[root@localhost mnt]# ifconfig eth0 | grep "inet " | cut -d " " -f 10
172.25.254.102

检测网络

[root@server2 mnt]# vim ping.sh
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
[root@server2 mnt]# sh ping.sh 102
172.25.254.102 is up

四、sort命令:排序

参数:
-n    纯数字排序
-r    倒序
-u   去掉重复数字
-o    输出到指定文件中
-t    指定分隔符
-k    指定要排序的列

注意: 默认以第一个数字排序

[root@server2 mnt]# sort wsp 

12
12
2
234
3
3
4
45
5
5
67
89
[root@server2 mnt]# sort  -n wsp 

2
3
3
4
5
5
12
12
45
67
89
234
[root@server2 mnt]# sort -u wsp 

12
2
234
3
4
45
5
67
89
[root@server2 mnt]# sort -nu wsp 

2
3
4
5
12
45
67
89
234
[root@server2 mnt]# sort -nt : -k 2 wsp 

2:2
3:3
3:4
4:4
5:7
67:23
12:34
45:34
234:56
12:87
5:88
89:89
[root@server2 mnt]# sort -nt : -k 2 wsp -o /mnt/file

五、uniq命令:对重复字符处理

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

[root@server2 mnt]# sort -n wsp | uniq -c
      1 
      1 2
      2 3
      1 4
      2 7
      2 8
      3 23
      2 34
      1 56
      1 87
      1 89
[root@server2 mnt]# sort -n wsp | uniq -d
3
7
8
23
34
[root@server2 mnt]# sort -n wsp | uniq -u

2
4
56
87
89

六、练习

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

[root@localhost mnt]# ll /tmp/
total 0
drwx------. 2 root root 23 Dec 25 22:39 ssh-2XD2n4VLLlQy
drwx------. 3 root root 16 Dec 25 22:39 systemd-private-JdjClc
drwx------. 3 root root 16 Dec 25 22:38 systemd-private-oH8LTq
drwx------. 3 root root 16 Dec 25 22:38 systemd-private-u2FkuC
[root@localhost mnt]# ll /tmp | sort -nk 5 | cut -d " " -f 9 | tail -n 1
ssh-2XD2n4VLLlQy
[root@localhost mnt]# ls -Sl /tmp/ | head -2 | cut -d " " -f 9

ssh-2XD2n4VLLlQy

五、条件判断

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" ]     -o条件至少满足一个
[ -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

#!/bin/bash

[ -z "$1" ] && {
    echo "please input a number!"
    exit 1
}

[ "$1" -gt "0" -a "$1" -lt "10" ] && {
    echo "YES"
}||{
    echo "NO"
}

判断文件类型

#!/bin/bash

[ -z "$1" ] && {
        echo "Please input a file name after script!!"
        exit 1
}
[ -e "$1" ]||{
        echo "$1 is not exit!!"
        exit 1
}
[ -f "$1" ] &&{
        echo "$1 is a common file"
        exit 0
}
[ -L "$1" ] &&{
        echo "$1 is a link "
        exit 0
}
[ -b "$1" ] &&{
        echo "$1 is a block file"
        exit 0
}
[ -S "$1" ] &&{
        echo "$1 is a taojiezi"
        exit 0
}
[ -c "$1" ] &&{
        echo "$1 is a zifushebei"
        exit 0
}
[ -d "$1" ] &&{
        echo "$1 is a directory"
        exit 0
}

猜你喜欢

转载自blog.csdn.net/excellent_L/article/details/85259684