2018-4-20 17 weeks 3 lessons Shell function, array, alarm demand analysis

Source: Internet
Author: User

20.16/20.17 functions in the shell


• The function is to organize a piece of code into a small unit, and give the small unit a name, when the code is used to call the name of the small unit directly.

Format: function F_name () {

Command

}

The function has to be on the front


• Example 1

#!/bin/bashinput () {# #定义f_name最好不要和shell里面的关键词冲突 echo $ $# $0}input 1 A B

$ A first, the second parameter

$# Script Name

Number of parameters

[[email protected] shell]# sh-x fun1.sh+ input 1 A B + echo 1 a 3 fun1.sh1 a 3 fun1.sh

You can also write parameters outside of the script

[

[Email protected] shell]# sh-x fun1.sh 1 A D # #参数写在脚本外, after the name, also line + input 1 A d+ echo 1 a 3 Fun1.sh1 a 3 fun1. Sh


Example 2

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


[Email protected] shell]# sh-x fun2.sh+ sum 1 s=3+ echo 33


As in Example 1, an external call to the parameter

[[email protected] shell]# sh-x fun2.sh 3 4+ sum 3 4+ s=7+ echo 77[[email protected] shell]# sh-x fun2.sh 3421 4352+ sum 3421 4352+ s=7773+ Echo 77737773

Which function you want to call, define the function before calling the statement


Example 3

#!/bin/baship () {ifconfig |grep-a1 "$" |tail-1 |awk ' {print $} ' |awk-f ': ' {print $} '}read-p ' please input the E Th name: "emyip= ' IP $e ' echo ' $e address is $myip"

The following can also be used to derive IP:

Ifconfig | GREP-A1 "$:" | awk '/inet/{print $} '


• After-school assignments:

Enter the network card name, determine whether it is empty, is not a network card in the system

Idea: First solve the problem of NULL input, if the input is empty, you are prompted to enter the content and re-cycle,

Second, if the system exists in the network card, and the network card configuration file under the/etc/sysconfig/network-scripts/, and all start with ifcfg-, so long can be used to determine the name of the network card entered "ifcfg-Network card name" file exists, Allow next step if present, otherwise cycle back



20.18 Arrays in the shell


• Define Array a= (1 2 3 4 5); Echo ${a[@]} array is not necessarily a number

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


· echo ${#a [@]} Gets the number of elements in the array

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


Echo ${a[2]} reads the third element, array starting from 0

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


echo ${a[*]} equivalent to ${a[@]} displays the entire array

[[email protected] shell]# echo ${a[@]}1 2 3 4 5[[email protected] shell]# echo ${a[*]}1 2 3 4 5


• Assigning values to arrays

• Add elements

a[5]=100; Echo ${a[@]}

[[email protected] shell]# A[5]=100[[email protected] shell]# echo ${a[*]}1 2 3 4 5 100[[email protected] shell]# a[5]=aaa [[email protected] shell]# echo ${a[*]}1 2 3 4 5 AAA

a[6]=bbb; Echo ${a[@]} An element is added automatically if the subscript does not exist

[[email protected] shell]# A[6]=bbb[[email protected] shell]# echo ${a[*]}1 2 3 4 5 AAA BBB

• Deletion of arrays

unset A; Unset A[1]

[[email protected] shell]# unset a[5]             # #删除第6个值 [[email protected] shell]#  echo ${a[*]}1 2 3 4 5 bbb[[email protected] shell]# unset a[ 5]            # #再次删第6个元素, there is no change, so the subscript does not change [[email  protected] shell]# echo ${a[*]}1 2 3 4 5 bbb[[email  protected] shell]# unset a[6]             # #下标没变, or the 7th element that was deleted [[email protected] shell]# echo ${a[*]}1 2 3 4  5[[email protected] shell]# unset a                 # #清空数组 [[email protected] shell]# echo ${a[*]} 


• Array shards

A= (' seq 1 10 ')

Echo ${a[@]:0:3} starts with the first element and intercepts 3

[[email protected] shell]# a= (' seq 1 ') [[email protected] shell]# echo ${a[*]}1 2 3 4 5 6 7 8 9 10[[email protected] She ll]# Echo ${a[*]:0:3}1 2 3


Echo ${a[@]:1:4} starts with the second element and intercepts 4

[[email protected] shell]# echo ${a[*]:1:4}2 3 4 5


Echo ${a[@]:0-3:2} Starts from the bottom 3rd element and intercepts 2

[Email protected] shell]# echo ${a[@]:0-3:2}8 9


• Array substitution

Echo ${a[@]/3/100} replaced with a value

A= (${a[@]/3/100})

[[email protected] shell]# echo ${a[*]/3/100}1 2 4 5 6 7 8 9 10[[email protected] shell]# a= (${a[*]/3/100}) [Email pro Tected] shell]# echo ${a[*]}1 2 100 4 5 6 7 8 9 10


20.19 Alarm system Requirements Analysis


• Requirements: Use the shell to customize a variety of personalized alarm tools, but the need for unified management, standardized management.

• Idea: Specify a script package that contains the main program, subroutine, configuration file, mail engine, output log, and so on.

• Main program: as an entry for the entire script, it is the lifeblood of the entire system.

• Configuration file: is a control center that uses it to switch individual subroutines, specifying each associated log file.

• Subroutines: This is the real monitoring script that monitors each indicator.

• Mail engine: It is implemented by a Python program that defines the server to which the message is sent, the sender's email, and the sender's password

• Output log: The entire monitoring system should have a log output.


Requirements: Our machine roles are varied, but the same monitoring system is deployed on all machines, and the entire program framework is consistent, regardless of the role of all machines, depending on the role of the different configuration files.


Program Architecture:

Under Bin is the main program

Conf is the configuration file

Shares is the various monitoring scripts

Mail engine under Mail

Log is the journal


2018-4-20 17 weeks 3 lessons Shell function, array, alarm demand analysis

Related Article

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.