• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

shell语法

武飞扬头像
尘世凡间
帮助1

一、变量和判断应用

1、匹配从前往后匹配

1)匹配第一个点字段后数据

[root@centos01 ~]# echo ${url#*.}

benet.com

2)匹配第一个到第二个点字段后数据

[root@centos01 ~]# echo ${url#*.*.}

com

3)贪婪匹配匹配最后一个字段

[root@centos01 ~]# echo ${url##*.}

com

4)将www替换为aa

[root@centos01 ~]# echo $url | sed 's#www#aaa#g'

aaa.benet.com

[root@centos01 ~]# echo ${url/www/aaa}

aaa.benet.com

5)将w替换为a

[root@centos01 ~]# echo ${url/w/a}

aww.benet.com

6)将所有的w替换为a

[root@centos01 ~]# echo ${url//w/a}

aaa.benet.com

7)测试

[root@centos01 ~]# vim test.sh

#!/bin/bash

dir="/etc/hosts"

read -p "请输入一个文件名称:" dir1

echo $dir

echo $dir1

[root@centos01 ~]# sh test.sh

请输入一个文件名称:/etc/passwd

/etc/hosts

/etc/passwd

2、从后往前匹配

1)删除倒数一个点

[root@centos01 ~]# echo ${url%.*}

2)删除从倒数第一个点到第二个点

[root@centos01 ~]# echo ${url%.*.*}

www

3)贪婪匹配

[root@centos01 ~]# echo ${url%%.*}

www

3、条件判断测试磁盘空间

1)需求

统计磁盘使用率,弱国大于80%发邮件警报,小于则抱ok

2)取值磁盘已用空间

[root@centos01 ~]# df -Th| grep /$|awk '{print $(NF-1)}'

3)过滤掉已用空间%号

[root@centos01 ~]# disk=`df -Th| grep /$|awk '{print $(NF-1)}'`

[root@centos01 ~]# echo $disk

5%

[root@centos01 ~]# echo ${disk%\%}

5

4)编写过滤磁盘使用脚本

[root@centos01 ~]# vim usedisk.sh

#!/bin/bash

disk=`df -h|grep /$|awk '{print $(NF-1)}'`

[ ${disk%\%} -gt 80 ] && echo "磁盘空间不足,当前磁盘使用率为$disk!!" || echo "有足够磁盘可用空间,易用空间为$disk!!"

5)验证

[root@centos01 ~]# dd if=/dev/zero of=/1.iso bs=100M count=100

[root@centos01 ~]# sh usedisk.sh

有足够磁盘可用空间,易用空间为18%!!

4、判断内存使用率

1)需求

内存使用率超过5%警报内存空间不足尽快扩展,当小于5%提示有足够的内存使用空间

2)获取内存已经使用的百分比

[root@centos01 ~]# usefree=`free|awk 'NR==2{print $3/$2*100}'`

[root@centos01 ~]# echo $usefree

12.9687

3)判断是否够有足够可用空间

[root@centos01 ~]# [ ${usefree%.*} -gt 4 ] && echo "内存空间不足!!" || echo "有足够内存空间!!"

内存空间不足!!

5、正则比较

1)修改IP地址和计算机名脚本

[root@centos02 ~]# cat hostname.sh

#!/bin/bash

read -p "亲输入要更改的主机名:" name

[ -z $name ] && echo "请输入主机名称:" && exit

hostnamectl set-hostname $name

read -p "请输入更改的IP地址主机位[10-254]:" IP

[[ ! $IP =~ ^[0-9] ]] && echo "请输入整数" && exit

ens="/etc/sysconfig/network-scripts/ifcfg-ens32"

SIP=`cat $ens| grep IPADDR|awk -F. '{print $NF}'`

sed -i "s#$SIP#$IP#g" $ens

2)查看是否修改

[root@centos02 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32

TYPE=Ethernet

BOOTPROTO=static

DEFROUTE=yes

NAME=ens32

DEVICE=ens32

ONBOOT=yes

IPADDR=192.168.200.20

NETMASK=255.255.255.0

6、配置路由

1)添加路由表

[root@centos01 ~]# for i in {100..200};do ip addr add 192.168.$i/24 dev ens32;done

2)删除路由表

[root@centos01 ~]# for i in {100..200};do ip addr del 192.168.$i/24 dev ens32;done

二、for循环使用

1、获取IP地址

1)编写获取系统信息脚本

[root@centos01 ~]# cat os.sh

#!/bin/bash

System=$(hostnamectl |grep System|awk '{print $3,$4,$5}')

Kernel=$(hostnamectl |grep Kernel|awk -F: '{print $2}')

vt=$(hostnamectl |grep Virtualization|awk '{print $2}')

Statichostname=$(hostnamectl|grep "Static hostname"|awk '{print $3}')

Lo=$(ifconfig lo|awk 'NR==2{print $2}')

Ens32=$(ifconfig ens32|awk 'NR==2{print $2}')

echo "当前系统版本是:$System"

echo "当前系统内核是:$Kernel"

echo "当前虚拟化平台是:$vt"

echo "当前静态主机名是:$Statichostname"

echo "当前lo地址是:$Lo"

echo "当前IP地址是:$Ens32"

2)显示脚本

[root@centos01 ~]# sh os.sh

当前系统版本是:CentOS Linux 7

当前系统内核是: Linux 3.10.0-693.el7.x86_64

当前虚拟化平台是:vmware

当前静态主机名是:centos01

当前lo地址是:127.0.0.1

当前IP地址是:192.168.100.10

2、使用for循环测试网络连通性

1)创建for循环

[root@centos01 ~]# cat ping

#!/bin/bash

for i in {10..50}

do

IP=192.168.100.$i

{

ping -c2 -W1 $IP > /dev/null

[ $? -eq 0 ] && echo $IP

} &

done

wait

echo "在线IP地址完成"

2)测试ping

[root@centos01 ~]# sh ping

192.168.100.10

在线IP地址完成

3、使用for批量创建用户

1)需求

前缀是user1、user2、user3..10

输入用户处添加判断是否为空

创建用户个数判断是否是证书

用户使用统一的密码pwd@123

用户创建成功提醒用户创建完成

创建失败提示用户创建失败

2)创建脚本

#!/bin/bash

read -p "输入用户前缀:" pre

[ -z $pre ] && echo "请输入用户前缀:" && exit

read -p "请输入用户编号:" num

[[ ! $num =~ ^[0-9] $ ]] && echo "请输入整数:" && exit

for i in `seq $num`

do

useradd $pre$i > /dev/null

[ $? -eq 0 ] && echo "$pre$i 创建成功" || echo "用户创建失"

echo pwd@123|passwd --stdin $pre$i > /dev/null

done

3)查看用户脚本执行过程

[root@centos01 ~]# sh useradd.sh

输入用户前缀:aa

请输入用户编号:10

aa1 创建成功

更改用户

passwd:所有的身份验证令牌已经成功更新。

aa2 创建成功

更改用户

4、更新yum,NF表示每一行的总列数,NF-1表示倒数第二列数据

1)菜单需求

按照不同的Centos版本,安装不同的yum源

当前什么版本系统,如何提取

if判断多分枝取系统版本安装不同的源

2)取Linux系统版本

[root@centos01 ~]# cat /etc/redhat-release | awk '{print $(NF-1)}'

7.4.1708

3)编写根据判断脚本更新yum源

[root@centos01 ~]# cat osversion.sh

#!/bin/bash

os=`cat /etc/redhat-release | awk '{print $(NF-1)}'`

if [ ${os%%.*} -eq 7 ];then

which wegt > /dev/null 2>&1

[ $? -eq 0 ] && yum -y install wget

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

echo "yum install is don........"

elif [ ${os%%.*} -eq 6 ];then

which wegt > /dev/null 2>&1

[ $? -ne 0 ] && yum -y install wget

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

else

which wegt > /dev/null 2>&1

[ $? -ne 0 ] && yum -y install wget

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

fi

5、菜单使用

1)第一种菜单方法

#!/bin/bash

echo -e "\t\t\t##########################"

echo -e "\t\t\t####1. PHP INSTALL 5.5####"

echo -e "\t\t\t####1. PHP INSTALL 5.7####"

echo -e "\t\t\t####1. PHP INSTALL 7.1####"

echo -e "\t\t\t####1. PHP INSTALL 7.2####"

echo -e "\t\t\t##########################"

2)第二种菜单

#!/bin/bash

cat <<EOF

1. INSTALL PHP5.5

2. INSTALL PHP5.6

3. INSTALL PHP7.1

4. INSTALL PHP7.2

EOF

三、case使用

1、case批量创建用户

1)case需求

删除用户的前缀

跑删除用户数量

2)编写脚本

[root@centos01 ~]# cat case.sh

#!/bin/bash

read -p "请输入用户前缀:" pre

read -p "请输入用户编号:" num

for i in `seq $num`

do

echo $pre$i

done

read -p "确定要删除以上用户吗?[y|Y|yes|n|N|no]" re

for n in `seq $num`

do

user=$pre$n

case $re in

y|Y|yes)

id $user > /dev/null 2>&1

if [ $? -eq 0 ];then

userdel -r $user

[ $? -eq 0 ] && echo "$user用户删除成功!!"

else

echo "$user:没有删除的用户!!"

fi

;;

n|N|no)

echo "不删除,玩什么哪!!"

exit

esac

done

[root@centos01 ~]#

3)脚本创建用户

[root@centos01 ~]# for i in `seq 10`;do useradd bob$i;done

2、使用case显示菜单

1)case需求

1.help帮助打印菜单

2.显示内存使用

3.显示磁盘使用

4.显示系统使用

5.显示系统负载

6.显示登录用户

7.查看IP地址

8.退出

2)编写脚本

[root@centos01 ~]# vim test.sh

[root@centos01 ~]# cat test.sh

#!/bin/bash

menu(){

cat<<EOF

1.help帮助打印菜单

2.显示内存使用

3.显示磁盘使用

4.显示系统使用

5.显示系统负载

6.显示登录用户

7.查看IP地址

8.退出

EOF

}

menu

while true

do

read -p "请输入查看系统编号:" num

case $num in

1)

help

;;

2)

free

;;

3)

df -Th

;;

4)

uptime

;;

5)

w

;;

6)

hostname -I

;;

7)

cat cat /etc/redhat-release |awk '{print $(NF-1)}'

;;

8)

exit

;;

*)

esac

done

3、使用case管理nginx服务

1)nginx管理服务

nginx 启动服务

nginx -s stop 停止服务

nginx -s reload 重新启动

2)管理nginx脚本

[root@centos01 ~]# cat nginx.sh

#!/bin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

start(){

/usr/local/sbin/nginx

[ $? -eq 0 ] && action "Nginx is start" /bin/true

}

stop(){

/usr/local/sbin/nginx -s stop

[ $? -eq 0 ] && action "Nginx is stop" /bin/true

}

restart(){

/usr/local/sbin/nginx -s stop

sleep 2

start

[ $? -eq 0 ] && action "Nginx is restart" /bin/true

}

reload(){

/usr/local/sbin/nginx -s reload

[ $? -eq 0 ] && action "Nginx is reload" /bin/true

}

status(){

#echo `/usr/bin/netstat -anptu | grep [n]ginx`

echo "当前Nginx监听端口:`netstat -anptu | grep [n]ginx|awk '{print $4}'`"

echo "当前NginxPID为:`ps aux | grep [n]ginx|grep master |awk '{print $2}'`"

}

case $1 in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

reload)

reload

;;

status)

status

;;

*)

echo "USAGE $0 [start|stop|reload|restart|status]"

esac

[root@centos01 ~]#

3)管理脚本

[root@centos01 ~]# sh nginx.sh status

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3949/nginx: master

二、function函数

1、funcation应用

1)定义funcation脚本

[root@centos01 ~]# cat function.sh

#!/bin/bash

test(){

echo "第一种函数定义方式"

}

function test1(){

echo "第二种函数定义方式"

}

function test2 { test2后边有空格

echo "第三种函数定义方式"

}

test //函数名字是test

test1

test2

2)调用函数脚本

[root@centos01 ~]# sh function.sh

第一种函数定义方式

第二种函数定义方式

第三种函数定义方式

2、函数传递参数

1)创建函数传递参数,$1传递参数

[root@centos01 ~]# cat function.sh

#!/bin/bash

test(){

echo $1

}

function test1(){

echo "第二种函数定义方式"

}

function test2 {

echo "第三种函数定义方式"

}

test $1

test1

test2

2)使用函数传递参数

[root@centos01 ~]# sh function.sh benet

benet

第二种函数定义方式

第三种函数定义方式

3)脚本传递参数使用

[root@centos01 ~]# cat function.sh

#!/bin/bash

test(){

echo $1

}

function test1(){

echo $1

}

function test2 {

echo $1

}

test $1

test1 $2

test2 $3

[root@centos01 ~]#

4)验证脚本传递参数

[root@centos01 ~]# sh function.sh benet accp shx

benet

accp

shx

3、函数传参数

1)编写传参数脚本

[root@centos01 ~]# cat funaction.sh

#!/bin/bash

t_file(){

if [ -f $1 ];then

echo "$1 exists"

else

retrun 1

fi

}

t_file $1

2)执行脚本

[root@centos01 ~]# sh funaction.sh /etc/hosts

/etc/hosts exists

4、传参计算

1)编写脚本

[root@centos01 ~]# cat funaction1.sh

#!/bin/bash

count01(){

num=10

for i in `seq 10`

do

total=$[$i $num ]

done

echo "计算结果是: $total"

}

count01

2)计算

[root@centos01 ~]# sh funaction1.sh

计算结果是: 20

5、计算机传参

1)编写脚本

[root@centos01 ~]# cat fun3.sh

#!/bin/bash

count01(){

num=$1

for i in `seq $num`

do

total=$[$i $num]

done

echo "计算结果是:$total"

}

count01 $1

count01 $2

count01 $3

2)结果

[root@centos01 ~]# sh fun3.sh 10 20 30

计算结果是:20

计算结果是:40

计算结果是:60

6、判断返回状态

1)编辑脚本

#!/bin/bash

fun4(){

echo 100

return 1

}

result=`fun4`

echo "函数的状态码:$?"

echo "函数的执行结果是:$result"

2)执行脚本

[root@centos01 ~]# sh fun4.sh

函数的状态码:1

函数的执行结果:100

7、判断文件是否存在

1)编写脚本

[root@centos01 ~]# cat fun5.sh

#!/bin/bash

file=/etc/hosts

t_file(){

if [ -f $file ];then

return 0

else

return 1

fi

}

2)执行脚本

[root@centos01 ~]# sh fun5.sh

[root@centos01 ~]# echo $?

0

3)完善脚本

[root@centos01 ~]# cat fun5.sh

#!/bin/bash

file=/etc/hosts

t_file(){

if [ -f $file ];then

return 0

else

return 1

fi

}

t_file

if [ $? -eq 0 ];then

echo "$file文件存在!!"

else

echo "$file文件不存在!!"

fi

4)显示执行结果

[root@centos01 ~]# sh fun5.sh

/etc/hostsa文件不存在!!

8、统计行

1)统计行脚本

[root@centos01 ~]# vim countline.sh

#!/bin/bash

file=$1

count(){

local i=0

while read line

do

let i

done<$file

echo $i

}

count

2)测试脚本

[root@centos01 ~]# sh countline.sh /etc/passwd

41

9、调用其它脚本的行数

1)查看脚本

[root@centos01 ~]# cat

#!/bin/bash

count01(){

num=10

for i in `seq 10`

do

total=$[$i $num ]

done

echo "计算结果是: $total"

}

count01

[root@centos01 ~]# sh funaction1.sh

计算结果是: 20

2)编写脚本

[root@centos01 ~]# cat test.sh

#!/bin/sh

. /root/funaction1.sh

echo "我在另外脚本调用你的结果 `count01`"

3)执行结果

[root@centos01 ~]# sh test.sh

计算结果是: 20

我在另外脚本调用你的结果计算结果是: 20

三、数组

1、数组类型

1)普通数组

只能使用整数作为数组索引

2)关联数组

可以使用字符串作为数组索引

2、创建数组

1)指定数组名字是arry索引值是0赋值时linux

[root@centos01 ~]# arry[0]=linux

2)显示复制

[root@centos01 ~]# echo ${arry[0]}

linux

3)显示复制

[root@centos01 ~]# echo ${arry}

linux

3、数组赋多个值

1)赋多个值

[root@centos01 ~]# arry[1]=mysql

[root@centos01 ~]# arry[2]=apache

[root@centos01 ~]# arry[3]=ngix

2)查看不同的复制

[root@centos01 ~]# echo ${arry[1]}

mysql

[root@centos01 ~]# echo ${arry[0]}

linux

[root@centos01 ~]# echo ${arry[2]}

apache

[root@centos01 ~]# echo ${arry[3]}

nginx

3)查看所有的赋值

[root@centos01 ~]# echo ${arry[*]}

linux mysql apache nginx

4)查看索引

[root@centos01 ~]# echo ${arry[*]}

linux mysql apache ngix

[root@centos01 ~]# echo ${!arry[*]}

0 1 2 3

[root@centos01 ~]# echo ${!arry[@]}

0 1 2 3

5)取消定义的数组

[root@centos01 ~]# unset array

4、数组连续复制

1)创建数组赋值

[root@centos01 ~]# benet=([0]=benet [1]=accp [2]=xsh)

2)调用数组

[root@centos01 ~]# echo ${benet[0]}

benet

[root@centos01 ~]# echo ${benet[1]}

accp

[root@centos01 ~]# echo ${benet[2]}

xsh

3)显示数组中所有值

[root@centos01 ~]# echo ${benet[*]}

benet accp xsh

4)查看数组索引

[root@centos01 ~]# echo ${!benet[*]}

0 1 2

5、连续数组赋值第三种方式

1)创建数组赋值

[root@centos01 ~]# array=(Linux Nginx PHP Mysql)

2)查看数组索引序列

[root@centos01 ~]# echo ${!array[*]}

3)查看数组赋值

[root@centos01 ~]# echo ${array[*]}

Linux Nginx PHP Mysql

6、普通数组使用数组方式ping测试主机

1)使用ping测试

[root@centos01 ~]# cat ping.sh

#!/bin/bash

IP=(

192.168.100.10

192.168.100.20

192.168.100.30

)

for i in ${IP[*]}

do

ping -c 1 -W1 $i >/dev/null

[ $? -eq 0 ] && echo "主机已开机$i"

done

2)执行脚本

[root@centos01 ~]# sh ping.sh

主机已开机192.168.100.10

7、关联数组

1)声明关联数组,awk支持关联数组不需要指定关联数组

[root@centos01 ~]# declare -A arry //声明是关联数组

2)创建关联数组传递参数

[root@centos01 ~]# arry=([index]=Linux [index1]=Shell [index2]=Mysql)

3)查看数组

[root@centos01 ~]# echo ${arry[*]}

Shell Mysql Linux

4)查看索引

[root@centos01 ~]# echo ${!arry[*]}

index1 index2 index

8、关联数组使用

1)判断男和女出现次数

[root@centos01 ~]# declare -A array

[root@centos01 ~]# let array[男]

[root@centos01 ~]# let array[男]

[root@centos01 ~]# let array[男]

[root@centos01 ~]# let array[男]

[root@centos01 ~]# let array[女]

[root@centos01 ~]# let array[女]

[root@centos01 ~]# echo ${!array[*]}

男女

2)取出循环列表

[root@centos01 ~]# for i in ${!array[*]};do echo $i;done

3)取出男和女的次数

[root@centos01 ~]# for i in ${!array[*]};do echo ${array[$i]};done

4

2

4)统计输入数组次数

[root@centos01 ~]# for i in ${!array[*]};do echo $i ${array[$i]};done

4)设置脚本

#!/bin/bash

declare -A arry

while read line

do

let arry[$line]

done<sex.txt

for in in ${!arry[*]}

do

echo "$i 出现了$arry[$i]次"

done

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibbihe
系列文章
更多 icon
同类精品
更多 icon
继续加载