2018-10-25

Source: Internet
Author: User
20.1 introduction to shell scripts
  • Shell is a scripting language.
  • Logic judgment, loop, and other syntaxes can be used.
  • Customizable Functions
  • Shell is a collection of system commands.
  • Shell scripts can achieve automated O & M, greatly increasing our O & M Efficiency
20.2 shell script structure and execution
  • Add script declaration in the first line#!/bin/bashTo tell the system which shell interpreter is used to execute the script.
  • To#The line at the beginning is used as an explanation, and the system will not execute
  • The script name is.shEnd of a shell script.
  • There are two script execution methods:
[[email protected] ~]# bash 01.sh
[[email protected] ~]# chmod a+x 01.sh[[email protected] ~]# ./01.sh
  • View Script Execution Process
[[email protected] ~]# bash -x 01.sh
  • Check whether the script syntax is incorrect.
[[email protected] ~]# bash -n 01.sh
20.3 date command usage

Date

[[email protected] ~]# date +%Y-%m-%d2018-10-24[[email protected] ~]# date +%y-%m-%d18-10-24[[email protected] ~]# date +%D10/24/18[[email protected] ~]# date +%F2018-10-24[[email protected] ~]# date +%hOct

Time

[[email protected] ~]# date +%H:%M:%S 20:53:59[[email protected] ~]# date +%T20:54:05

Timestamp

[[email protected] ~]# date +%s1540428984[[email protected] ~]# date +%s -d "2018-10-24 20:56:24"1540428984[[email protected] ~]# date -d @1540428984 +%T20:56:24

Define date and time

[[email protected] ~]# date -d "+1 day"  +%F2018-10-25[[email protected] ~]# date -d "-1 day"  +%F2018-10-23[[email protected] ~]# date -d "-1 month"  +%F2018-09-24[[email protected] ~]# date -d "+1 month"  +%F2018-11-24[[email protected] ~]# date -d "+1 hour"  +%T22:02:14[[email protected] ~]# date -d "+1 min"  +%T21:03:21

Calendar

[[email protected] ~]# cal    October 2018    Su Mo Tu We Th Fr Sa    1  2  3  4  5  6 7  8  9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31
20.4 variables in shell scripts
  • When the script uses a string that is frequent and has a long length, replace it with a variable.
  • When using conditional statements, the variable if [$ A-GT 1]; then...; FI is often used.
  • When referencing the result of a command, replace N = 'wc-l 1.txt 'with a variable'
  • When writing a script for user interaction, the variable is also an essential read-P "input a number:" N; echo \ (if n has not written this N, you can directly use \) Reply
  • Built-in variables $0, $1, $2... $0 indicates the script itself, $1 indicates the first parameter, \ (2 indicates the second... \) # indicates the number of parameters.
  • Mathematical operation a = 1; B = 2; C = \ (\) A + \ (B) or \) [\ (a + \) B]
20.5 logical judgment in shell scripts
Operator Function
-EQ Equal
-Ne Not equal
-GT Greater
-Lt Less
-Le Equal to or less
-Ge Greater than or equal

If condition judgment statement

If [$ A-EQ 5]; Then ECHO yesfi input variable A = 5, result Yes input variable A = 4, result none
If [$ A-EQ 5]; Then ECHO yeselse echo nofi input variable A = 5, result Yes input variable A = 4, result no
If [$ A-lt 5]; Then ECHO smallelif [$ A-GT 5] & [$ A-LT 10]; Then ECHO middleelse echo largefi input variable A = 3, RESULT Small input variable A = 6, result middle input variable A = 11, result large
20.6 file directory attribute judgment
Operator Function
-D Whether the test file is of the Directory type
-E Whether the test file exists
-F Determine whether it is a common file
-R Test whether the current user has the permission to read data.
-W Test whether the current user has the write permission
-X Test whether the current user has the permission to execute
20.7 special if usage
Operator Function
= Compare whether the string content is the same
! = Compares string content
-Z Determines whether the string content is empty.
-N Indicates that when the variable or file is not empty

A command can be used as a judgment condition.

if grep -wq ‘123‘ 1.txt; thenecho yesfi

if (($a<1)); then …Equivalentif [ $a -lt 1 ]; then…

[] Cannot be used <,>, = ,! =, >=, <=

20.8/20.9 case judgment

Format

Case variable name in value1) command; value2) command; *) commond; esac

In the case program, you can use |, or in the condition.

2|3)    command    ;;

Case

#! /Bin/bashread-P "Please input a number:" NIF [-z "$ N"] Then ECHO "Please input a number. "Exit 1fin1 = 'echo $ n | SED's/[0-9] // g'' if [-n" $ N1 "] Then ECHO" Please input a number. "Exit 1 fiif [$ n-lt 60] & [$ n-GE 0] Then tag = 1 Elif [$ n-ge 60] & [$ n-lt 80] Then tag = 2 Elif [$ n-ge 80] & [$ n-lt 90] Then tag = 3 Elif [$ n-GE 90] & [$ n- le 100] Then tag = 4 else tag = 0 ficase $ tag in 1) echo "fail"; 2) echo "pass"; 3) echo "good"; 4) echo "excellent ";;*) echo "The number range is 0-100. "; esac
20.10 for Loop

Format

For variable name in condition do commonddone

Case 1

#!/bin/bashsum=0for i in `seq 1 100`do    sum=$[$sum+$i]    echo $idoneecho $sum

Case 2

 #!/bin/bashcd /etc/for a in `ls /etc/`do    if [ -d $a ]    then        ls -d $a    fidone
20.11/20.12 WHILE LOOP

Format

While condition do commonddone

Case 1

#!/bin/bashwhile :do    load=`w|head -1|awk -F ‘load average: ‘ ‘{print $2}‘|cut -d. -f1`    if [ $load -gt 10 ]    then    top|mail -s "load is high: $load" [email protected]    fi    sleep 30done

Case 2

#!/bin/bashwhile :do    read -p "Please input a number: " n    if [ -z "$n" ]    then        echo "you need input sth."        continue    fi    n1=`echo $n|sed ‘s/[0-9]//g‘`    if [ -n "$n1" ]    then        echo "you just only input numbers."        continue    fi    breakdoneecho $n
20.13 break bounce cycle
#!/bin/bashfor i in `seq 1 5`do    echo $i    if [ $i == 3 ]    then        break    fi    echo $idoneecho aaaaaaa
20.14 continue ends this cycle

Ignore the code under the continue and directly perform the next loop

#!/bin/bashfor i in `seq 1 5`do    echo $i    if [ $i == 3 ]    then        continue    fi    echo $idoneecho $i
20.15 exit the entire script
#!/bin/bashfor i in `seq 1 5`do    echo $i    if [ $i == 3 ]    then        exit    fi    echo $idoneecho aaaaaaa

Extension
Select usage http://www.apelearn.com/bbs/thread-7950-1-1.html

Functions in Shell 20.16/20.17
  • A function sorts a piece of code into a cell and gives the cell a name. When this code is used, you can directly call the name of the cell. The function must be placed at the beginning.
  • Format
function f_name() {    command }

Example 1

#!/bin/bashinput() {    echo $1 $2 $# $0}input 1 a b

Example 2

#!/bin/bashsum() {    s=$[$1+$2]    echo $s}sum 1 2

Example 3

#!/bin/baship() {    ifconfig |grep -A1 "$1 " |tail -1 |awk ‘{print $2}‘|awk -F‘:‘ ‘{print $2}‘}read -p "Please input the eth name: " emyip=`ip $e`echo "$e address is $myip"
20.18 array in Shell

Define an array and display an array

[[email protected] ~]# a=(1 2 3 4 5)[[email protected] ~]# echo ${a[@]}1 2 3 4 5[[email protected] ~]# echo ${a[*]}1 2 3 4 5

Obtains the number of elements in an array.

[[email protected] ~]# echo ${#a[@]}5

Reads an element. The array starts from 0.

[[email protected] ~]# echo ${a[2]} 3[[email protected] ~]# echo ${a[0]} 1

Array assignment

[[email protected] ~]# a[1]=100[[email protected] ~]# a[5]=100[[email protected] ~]# echo ${a[@]}1 100 3 4 5 100

Delete Array

[[email protected] ~]# unset a[1][[email protected] ~]# echo ${a[@]}1 3 4 5 100[[email protected] ~]# unset a[[email protected] ~]# echo ${a[@]}

Array partitioning

[[Email protected] ~] # A = ('seq 1 5') [[email protected] ~] # Echo $ {A [@]: 0: 3} // capture 3 1 2 3 [[email protected] ~] starting from the first element # Echo $ {A [@]: 1: 4} // capture four 2 3 4 5 [[email protected] ~] starting from the second element # Echo $ {A [@]: 0-} // captures 2 3 4 elements starting from the last 3rd Elements

Array replacement

[[email protected] ~]# echo ${a[@]/3/100}1 2 100 4 5[[email protected] ~]# a=(${a[@]/3/100})1 2 100 4 5
20.19 Alert System Requirement Analysis
  • Requirement: You can use shell to customize various alarm tools, but you need to manage them in a unified and standardized manner.
  • Idea: specify a script package, including the main program, subroutine, configuration file, mail engine, and output log.
  • Main Program: as the entrance to the entire script, it is the lifeblood of the entire system.
  • Configuration File: a control center that is used to switch subprograms and specify related log files.
  • Subroutine: this is the real monitoring script used to monitor various indicators.
  • Mail engine: is implemented by a python program. It can define the mail server, mail recipient, and sender password.
  • Output log: log output is required for the entire monitoring system.
  • Requirements: our machine roles are diverse, but the same monitoring system must be deployed on all machines. That is to say, no matter what role, the entire program framework is consistent, the difference lies in customizing different configuration files based on different roles.
  • Program architecture
    • Bin is the main program
    • Conf is the configuration file.
    • Various monitoring scripts under shares
    • Mail is the mail engine.
    • Logs
20.20 alarm system master script
[[Email protected] ~] # Cd/usr/local/sbin/MON/bin [[email protected] bin] # Vim main. Sh #! /Bin/bash # written by aming. # Whether to send mail switch export send = 1 # filter IP address export ADDR = '/sbin/ifconfig | grep-A1 "ens33: "| awk '/inet/{print $2}'' dir = 'pwd' # Only the last-level directory name last_dir = 'echo $ dir | awk-F '/'' {print $ NF} ''# The purpose of the following judgment is, make sure that we are in the bin directory when executing the script, otherwise, if [$ last_dir = "bin"] | [$ last_dir = "bin/"]; then conf_file = ".. /CONF/mon. conf "else echo" you shoud CD bin dir "exitfiexec 1> .. /log/mon. log 2> .. /log/err. logecho "'date +" % F % t "'Load average"/bin/bash .. /shares/load. sh # first check whether the configuration file needs to monitor 502if grep-Q 'to _ mon_502 = 1' $ conf_file; then export log = 'grep' logfile = '$ conf_file | awk-F' = ''{print $2}' | SED's // G'/bin/bash .. /shares/502. shfi
20.21 Alert System Configuration File
[[Email protected] ~] # Cd/usr/local/sbin/MON/conf [[email protected] conf] # Vim mon. conf # To config the options if to monitor # define the MySQL server address, port, and user, passwordto_mon_cdb = 0 #0 or 1, default 0, 0 not monitor, 1 monitordb_ip = 10.20.3.13db _ Port = 3315db_user = usernamedb_pass = passwd # If httpd is 1, it will be monitored. If it is 0, to_mon_httpd = 0 # If PHP is 1, it will be monitored, to_mon_php_socket = 0 # http_code_502 need to define the access log path to_mon_502 = 1 logfile =/data/log/logs request_count define the Log Path and domain name token = 0req_log =/data/ log/www.discuz.net/access.logdomainname=www.discuz.net
20.22 Alarm System Monitoring Project
[[email protected] ~]# cd /usr/local/sbin/mon/shares[[email protected] shares]# vim load.sh#! /bin/bash##Writen by aming##load=`uptime |awk -F ‘average:‘ ‘{print $2}‘|cut -d‘,‘ -f1|sed ‘s/ //g‘ |cut -d. -f1`if [ $load -gt 10 ] && [ $send -eq "1" ]then    echo "$addr `date +%T` load is $load" >../log/load.tmp    /bin/bash ../mail/mail.sh [email protected] "$addr\_load:$load" `cat ../log/load.tmp`fiecho "`date +%T` load is $load"[[email protected] shares]# vim 502.sh#! /bin/bashd=`date -d "-1 min" +%H:%M`c_502=`grep :$d: $log |grep ‘ 502 ‘|wc -l`if [ $c_502 -gt 10 ] && [ $send == 1 ]; then    echo "$addr $d 502 count is $c_502">../log/502.tmp    /bin/bash ../mail/mail.sh $addr\_502 $c_502 ../log/502.tmpfiecho "`date +%T` 502 $c_502"[[email protected] shares]# vim disk.sh#! /bin/bash##Writen by aming##rm -f ../log/disk.tmpfor r in `df -h |awk -F ‘[ %]+‘ ‘{print $5}‘|grep -v Use`do    if [ $r -gt 90 ] && [ $send -eq "1" ]then    echo "$addr `date +%T` disk useage is $r" >>../log/disk.tmpfiif [ -f ../log/disk.tmp ]then    df -h >> ../log/disk.tmp    /bin/bash ../mail/mail.sh $addr\_disk $r ../log/disk.tmp    echo "`date +%T` disk useage is nook"else    echo "`date +%T` disk useage is ok"fi
20.23/20.24/20.25 alarm system email Engine
[[Email protected] ~] # Cd/usr/local/sbin/MON/Mail [[email protected] Mail] # Vim mail. shlog = $ 1t_s = 'date + % s' T _ s2 = 'date-d "2 hours ago" + % s' if [! -F/tmp/$ log] Then ECHO $ t_s2>/tmp/$ logfit_s2 = 'Tail-1/tmp/$ log | awk '{print $1} ''echo $ t_s>/tmp/$ logv = $ [$ t_s-$ t_s2] echo $ VIF [$ V-GT 3600] then. /mail. PY $1 $2 $3 Echo "0">/tmp/logs log.txt else if [! -F/tmp/mongolog.txt] Then ECHO "0">/tmp/mongolog.txt fi Nu = 'cat/tmp/mongolog.txt 'nu2 = $ [$ nu + 1] echo $ nu2> /tmp/cmdlog.txt if [$ nu2-GT 10] then. /mail. PY $1 "trouble continue 10 min $2" "$3" Echo "0">/tmp/mongolog.txt Fifi [[email protected] Mail] # Vim mail. PY #! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import OS, sysreload (sys) sys. setdefaultencoding ('utf8') Import getoptimport smtplibfrom email. mimetext import mimetextfrom email. mimemultipart import mimemultipartfrom subprocess import * def sendqqmail (username, password, mailfrom, mailto, subject, content): gserver = 'smtp .qq.com 'gport = 25 try: # MSG = mimetext (Unicode (content ). encode ('utf-8') // If the sent email has garbled characters, you can try to change this line to the following: MSG = mimetext (content, 'plan ', 'utf-8 ') MSG ['from'] = mailfrom MSG ['to'] = mailto MSG ['reply-to'] = mailfrom MSG ['subobject'] = subject SMTP = smtplib. SMTP (gserver, gport) SMTP. set_debuglevel (0) SMTP. EHLO () SMTP. login (username, password) SMTP. sendmail (mailfrom, mailto, MSG. as_string () SMTP. close () failed t exception, err: Print "send mail failed. error: % s "% errdef main (): To = sys. argv [1] Subject = sys. argv [2] content = sys. argv [3] sendqqmail ('[email protected]', 'aaaaaaaaaaa', '[email protected]', to, subject, content) if _ name _ = "_ main _": Main ()
20.26 run the alarm system
[[email protected] ~]# crontab -e1 * * * * cd /usr/local/sbin/mon/bin; bash main.sh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.