shell脚本的应用
A. shell脚本是什么
当执行命令或程序语句是通过程序文件而不是命令行,那这个程序被称为Shell脚本。如果Shell脚本内置很多命令、语句及循环控制,然后一次性执行完毕,这种通过文件执行脚本的方式称为非交互方式。用户可以在Shell脚本中输入一系列命令及命令语句组合。这些命令、变量和流程控制语句等有机地结合在一起,就形成一个功能强大的Shell脚本。
B. shell脚本具体是干什么的,是在用什么的时候会用得到
Shell遵从经典UNIX哲学:把复杂的问题分解成简单的小问题,然后再把各部分功能组合起来解决复杂问题。
例如我可以用shell脚本来监控服务器整体的性能,当CPU负载超过我预设的警戒线,磁盘空间的阀值超过我预设的标准,服务器宕机,这些都可以通过shell编写脚本做到自动给我发告警邮件或者发短信通知我。简化每日繁琐的工作步骤,数据的备份,日志的搜集整理。太多太多了。..
C. shell脚本的常用循环
在简单介绍shell脚本(shell script)之前,先简单介绍一下shell。
在计算机科学中, shell (壳层)指“为用户提供用户界面”的软件,通常指的是命令行界面的解析器。一般来说,这个词是指操作系统中,提供访问内核所提供之服务的程序。Shell也用于泛指所有为用户提供操作界面的程序,也就是程序和用户交互的接口。因此与之相对的是程序内核(英语:Kernel),内核不提供和用户的交互功能。
不过这个词也拿来指应用软件,或是任何在特定组件外围的软件,例如浏览器或电子邮件软件是HTML排版引擎的shell。Shell这个词是来自于操作系统(内核)与用户界面的外层界面。
通常将shell分为两类:命令行与图形界面。命令行壳层提供一个命令行界面(CLI);而图形壳层提供一个图形用户界面(GUI)。
shell脚本 (英语:shell script),又称shell命令稿、程序化脚本,是一种电脑程序与文本文件,内容由一连串的shell命令组成,经由Unix Shell直译其内容后运作。被当成是一种脚本语言来设计,其运作方式与解释型语言相当,由Unix shell扮演命令行解释器的角色,在读取shell脚本之后,依序运行其中的shell命令,之后输出结果。利用shell脚本可以进行系统管理,文件操作等。
在Unix及所有的类Unix系统中,如Linux、FreeBSD等操作系统,都存在shell脚本。依照Unix shell的各种不同类型,shell脚本也有各种不同方言,如 Bourne shell (sh), Bourne-Again shell (bash), C shell (csh)等等。在DOS、OS/2、Microsoft Windows中的批处理文件,跟shell脚本有类似的功能。
常见的shell脚本循环语法包括for、while和until。除此之外,还有一些其他语法,能够实现循环(遍历)功能。和循环相关的还有循环控制符break和continue,用于控制流程转向。
for循环属于“当型循环”,即“当满足条件的时候执行”。
for循环有三种类型:
常见的数值for循环语法还有:
for file in $( ls )语法的等效语法有很多,如下:
或者
类C风格的for循环,和C没有太大的区别,这里只进行示例,不再赘述。
或者
结果:
也称为前测试循环语句,重复次数是利用一个条件来控制是否继续重复执行这个语句。为了避免死循环,必须保证循环体中包含循环出口条件即表达式存在退出状态为非0的情况。例如,计算1~100以内所有的奇数之和:
当然,条件可以使字符串,可以是输入值,可以是标记等,此处不再赘述。
until命令和while命令类似,while能实现的功能until也可以实现。但区别是until循环的退出状态是为0(与while刚好相反),即whie循环在条件为真时继续执行循环,而until则在条件为假时执行循环。
例如,计算1~100以内所有的奇数之和:
select结构从技术角度看不能算是循环结构,只是相似而已,它是bash的扩展结构用于交互式菜单显示,功能类似于case结构,但是比case的交互性要好。在遍历列表功能中,select结构可以实现循环的功能。
例如,以下选择,只有选择white的时候,退出循环
在循环语法中,经常需要根据条件控制循环退出或跳过本次执行,这时候就需要用到循环控制符。循环控制符主要包括两个:break和continue。
循环控制符和C以及Java等常见语法功能类似,在理不再赘述,只给出常用示例。
在for、while和until循环中break可强行退出循环。
注意 :break语句仅能退出当前的循环,如果是两层循环嵌套,则需要在外层循环中使用break。
例如,计算1~100以内所有的奇数之和:
在for、while和until中用于让脚本跳过其后面的语句,执行下一次循环。
例如,显示10以内能被3整除的正整数。
以上是自己在学习Linux的过程中的一部分总结,和大家共享。同时,也参考了其他人的博客,在参考列表中列出来了。
D. Shell脚本语言优势怎样
Shell脚本语言的优势在于处理偏操作系统底层的业务,例如:Linux系统内部的很多应用(有的是应用的一部分)都是使用Shell脚本开发的,因为有1000多个Linux系统命令为它做支撑,特别是Linux正则表达式及三剑客grep、sed、awk等命令。
对于一些常见的系统脚本使用Shell开发会更简单、更快速,例如:让软件一键自动化安装、优化,监控报警脚本,软件启动脚本,日志分析脚本等,虽然PHP/Python语言也能够做到这些,但是,考虑到掌握难度、开发效率、开发习惯等因素,它们可能就不如Shell脚本语言流行及有优势了。对于一些常规的业务应用,使用Shell更符合Linux运维简单、易用、高效的三大基本原则。
PHP语言的优势在于小型网站系统的开发;Python语言的优势在于开发较复杂的运维工具、Web界面的管理工具和Web业务的开发(例如:CMDB自动化运维平台、跳板机、批量管理软件SaltStack、云计算OpenStack软件)等。我们在开发一个应用时应根据业务需求,结合不同语言的优势及自身擅长的语言来选择,扬长避短,从而达到高效开发及易于自身维护等目的。
E. Shell脚本有几种运行方式,如何使用
shell脚本 都是解释执行的。
一种是通过 .命令来执行。比如 ./t.sh
一种是通过source命令来执行,source等同于.
F. Shell脚本有几种运行方式,如何使用
1. 输入定向到Shell脚本,具体来讲就是利用输入重定向的机制,让Shell解释器顺次读取每一行脚本命令,进行执行。bash > test.sh 2. 类似方法一,只不过它是以脚本文件名为参数的。bash bash.sh 3. 将脚本文件设置成执行文件,直接在提示符下调用。
G. 标准的shell脚本应该包含什么
一个标准的shell脚本包括bin、conf、log三个文件夹。bin包含脚本的具体内容;conf包含脚本需要获取的配置;log包含脚本执行过程有无错误的log和跟业务相关的log。在计算机科学中,Shell俗称壳,是指“为使用者提供操作界面”的软件(命令解析器)。它用于接收用户命令,然后调用相应的应用程序。同时它又是一种程序设计语言。作为命令语言,它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。在排序算法中,Shell是希尔排序的名称。
H. 什么是shell脚本
操作系统与外部最主要的接口就叫做shell。shell是操作系统最外面的一层。shell管理你与操作系统之间的交互:等待你输入,向操作系统解
释你的输入,并且处理各种各样的操作系统的输出结果。shell提供了你与操作系统之间通讯的方式。这种通讯可以以交互方式(从键盘输
入,并且可以立即得到响应),或者以shellscript(非交互)方式执行。shellscript是放在文件中的一串shell和操作系统命令,它们可以被
重复使用。本质上,shellscript是命令行命令简单的组合到一个文件里面。Shell基本上是一个命令解释器,类似于DOS下的command.com
。它接收用户命令(如ls等),然后调用相应的应用程序。较为通用的shell有标准的Bourneshell(sh)和Cshell(csh)。
交互式shell和非交互式shell交互式模式就是shell等待你的输入,并且执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交
互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、签退。当你签退后,shell也终止了。shell也可以运行在另外一种模式:非
交互式模式。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,shell也就终止了。
shell的类型在UNIX中主要有两大类shellBourneshell(包括sh,ksh,andbash)Bourneshell(sh)Kornshell(ksh)BourneAgain
shell(bash)POSIXshell(sh)Cshell(包括cshandtcsh)Cshell(csh)TENEX/TOPSCshell(tcsh).
附:LINUX系统的shell原理
Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口。它是命令语言、命令解释程序及程序设计语言的统称。
shell是用户和Linux内核之间的接口程序,如果把Linux内核想象成一个球体的中心,shell就是围绕内核的外层。当从shell或其他程序向
Linux传递命令时,内核会做出相应的反应。shell是一个命令语言解释器,它拥有自己内建的shell命令集,shell也能被系统中其他应用
程序所调用。用户在提示符下输入的命令都由shell先解释然后传给Linux核心。
I. shell脚本上
| 对于初学者而言,因为没有实战经验,写不出来 Shell 脚本 很正常,如果工作了几年的运维老年还是写不出来,那就是没主动找需求,缺乏练习,缺乏经验。针对以上问题,总结了30个生产环境中经典的 Shell 脚本 ,通过这些需求案例,希望能帮助大家提升Shell编写思路,掌握编写技巧。 |
先了解下编写Shell过程中注意事项:
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">开头加解释器:#!/bin/bash
语法缩进,使用四个空格;多加注释说明。
命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用。
默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作用域。
有两个 命令 能帮助我调试脚本:set -e 遇到执行非0时退出脚本,set-x 打印执行过程。
写脚本一定先测试再到生产上。
</pre>
1、获取随机字符串或数字
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">获取随机8位字符串:
方法1:
471b94f2
方法2:
vg3BEg==
方法3:
ed9e032c
获取随机8位数字:
方法1:
23648321
方法2:
38571131
方法3:
69024815
cksum:打印CRC效验和统计字节
</pre>
2、定义一个颜色输出字符串函数
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:
function echo_color() {
if [ 2 33[0m"
elif [ 2 33[0m"
fi
}
方法2:
function echo_color() {
case 2[0m"
;;
red)
echo -e "[31;40m$2[0m"
;;
*)
echo "Example: echo_color red string"
esac
}
使用方法:echo_color green "test"
function关键字定义一个函数,可加或不加。
</pre>
3、批量创建用户
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
DATE= 1 == "green" ]; then
echo -e "[32;40m 1 == "red" ]; then
echo -e "[31;40m$2[0m"
fi
}
if [ -s USER_FILE {DATE}.bak
echo_color green " {USER_FILE}- USER_FILE
echo "----------------" >> USER &>/dev/null; then
PASS= RANDOM |md5sum |cut -c 1-8)
useradd PASS |passwd --stdin USER USER_FILE
echo " USER User already exists!"
fi
done
</pre>
4、检查软件包是否安装
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
</pre>
5、检查服务状态
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
PORT_C= (ps -ef |grep ntpd |grep -vc grep)
if [ PS_C -eq 0 ]; then
echo "内容" | mail -s "主题" [email protected]
fi
</pre>
6、检查主机存活状态
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:将错误IP放到数组里面判断是否ping失败三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in NUM -le 3 ]; do
if ping -c 1 IP Ping is successful."
break
else
# echo " NUM"
FAIL_COUNT[ IP
let NUM++
fi
done
if [ {FAIL_COUNT[1]} Ping is failure!"
unset FAIL_COUNT[*]
fi
done
方法2:将错误次数放到FAIL_COUNT变量里面判断是否ping失败三次
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP >/dev/null; then
echo " IP Ping is failure FAIL_COUNT -eq 3 ]; then
echo "$IP Ping is failure!"
fi
done
方法3:利用for循环将ping通就跳出循环继续,如果不跳出就会走到打印ping失败
ping_success_status() {
if ping -c 1 IP Ping is successful."
continue
fi
}
IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
for IP in IP Ping is failure!"
done
</pre>
7、监控CPU、内存和硬盘利用率
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)CPU
借助vmstat工具来分析CPU统计信息。
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (( SY))
if [ DATE
Host: USE
" | mail -s "CPU Monitor" $MAIL
fi
2)内存
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (free -m |awk /Mem/{print (free -m |awk /Mem/{print 6- (( USE))
if [ DATE
Host: TOTAL,Use= FREE
" | mail -s "Memory Monitor" $MAIL
fi
3)硬盘
DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (fdisk -l |awk -F [: ]+ BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,", 3} )
PART_USE= 1,int( 6} )
for i in (echo (echo (echo USE -gt 80 ]; then
echo "
Date: IP
Total: PART= MOUNT)
" | mail -s "Disk Monitor" $MAIL
fi
done
</pre>
8、批量主机磁盘利用率监控
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">前提监控端和被监控端SSH免交互登录或者密钥登录。
写一个配置文件保存被监控主机SSH连接信息,文件内容格式:IP User Port
HOST_INFO=host.info
for IP in 1} (awk -v ip= 1{print HOST_INFO)
PORT= IP ip== 3} PORT IP df -h > (awk BEGIN{OFS="="}/^/dev/{print 5)} USE_RATE_LIST; do
PART_NAME= {USE_RATE#*=}
if [ PART_NAME Partition usage $USE_RATE%!"
fi
done
done
</pre>
9、检查网站可用性
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)检查URL可用性
方法1:
check_url() {
HTTP_CODE= 1)
if [ 1 Access failure!"
fi
}
方法2:
check_url() {
if ! wget -T 10 --tries=1 --spider $1 >/dev/null 2>&1; then
}
使用方法:check_url www..com
2)判断三次URL可用性
思路与上面检查主机存活状态一样。
方法1:利用循环技巧,如果成功就跳出当前循环,否则执行到最后一行
check_url() {
HTTP_CODE= 1)
if [ URL_LIST; do
check_url URL
check_url URL Access failure!"
done
方法2:错误次数保存到变量
URL_LIST=" www..com www.agasgf.com "
for URL in (curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" HTTP_CODE -ne 200 ]; then
let FAIL_COUNT++
else
break
fi
done
if [ URL Access failure!"
fi
done
方法3:错误次数保存到数组
URL_LIST=" www..com www.agasgf.com "
for URL in NUM -le 3 ]; do
HTTP_CODE= URL)
if [ NUM]= NUM下标, {#FAIL_COUNT[ ]} -eq 3 ]; then
echo "Warning: $URL Access failure!"
unset FAIL_COUNT[ ] #清空数组
fi
done
</pre>
10、检查MySQL主从同步状态
<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS= USER -p 0} ) #gsub去除冒号后面的空格
for i in {i%:*}
THREAD_STATUS= THREAD_STATUS" != "Yes" ]; then
echo "Error: MySQL Master-Slave THREAD_STATUS!"
fi
done
</pre>
动手练一练,让你的Shell功底上升一个段位!