function in shell, array of shell, demand analysis of alarm system

Source: Internet
Author: User
Tags define function

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.

格式: function f_name() { ?? ?? ?? ?? ?? ?? ? command? ?? ?? ?? ?         }函数必须要放在最前面
    • Script Example 1 (used to define function printing parameters)
[[email protected] aming]# vim fun1.sh[[email protected] aming]# cat fun1.sh #!/bin/bashfunction INP () {echo $ $#}INP 1 a 2[[email protected] aming]# sh fun1.sh 1 a 2 fun1.sh 3[[email protected] aming]# sh-x Fu n1.sh + INP 1 a 1 a 2 fun1.sh a 2 fun1.sh 3[[email protected] aming]# vi fun1.sh [[email protected] A ming]# Cat fun1.sh #!/bin/bashfunction INP () {echo "The first par is $" echo "the second par is" echo "the TH IRD par is $ "echo" the SCRITP name is $ "echo" the number of Par is $# "}inp b a 2 3 adf[[email protected] am ing]# sh fun1.sh The first par is bthe second par are athe third par is 2the scritp name was fun1.shthe number of par is 5[[ Email protected] aming]# sh-x fun1.sh + INP b a 2 3 adf+ echo ' The first par is B ' the first par is + + echo ' the Seco Nd par is a ' the second par is a + echo ' the third par is 2 ' the third par is the "the SCRITP name is" fun1.sh ' the SCRITP Name is fun1.sh+ Echo ' tHe number of par is 5 ' the number of par is 5[[email protected] aming]#  
    • Script Example 2 (function to define an addition)
[[email protected] aming]# vim fun2.sh[[email protected] aming]# cat fun2.sh #!/bin/bashsum() {    s=$[$1+$2]    
    • Script Example 3 (used to define the display IP)
[[email protected] aming]# vim fun3.sh [[email protected] aming]# cat fun3.sh #!/bin/baship(){    ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘}read -p "Please input the eth name: " ethip $eth[[email protected] aming]# sh fun3.sh Please input the eth name: ens33172.16.111.100[[email protected] aming]# sh -x fun3.sh + read -p ‘Please input the eth name: ‘ ethPlease input the eth name: ens33+ ip ens33+ grep -A1 ‘ens33: ‘+ awk ‘/inet/ {print $2}‘+ ifconfig172.16.111.100
Array 1 in the shell in array 1.shell
    • defines the array a= (1 2 3 4 5); Echo ${a[@]}
    • echo ${#a [@]} Gets the number of elements in the array
    • echo ${a[2]} reads the third element, with the array starting from 0
    • echo ${a[*]} equals ${a[@]} displays the entire array
      array assignment
    • a[1]=100; Echo ${a[@]}
    • a[5]=2; echo ${a[@]} if the subscript does not exist, it will automatically add a Delete element
      Array
    • unset A; unset a[1]
[[email protected] aming]# b=(1 2 3)[[email protected] aming]# echo ${b[@]}1 2 3[[email protected] aming]# echo ${b[*]}1 2 3[[email protected] aming]# echo ${b[1]}2[[email protected] aming]# echo ${b[2]}3[[email protected] aming]# echo ${b[0]}1[[email protected] aming]# echo ${#b[@]}3[[email protected] aming]# b[3]=a[[email protected] aming]# echo ${b[*]}1 2 3 a[[email protected] aming]# b[3]=aaa[[email protected] aming]# echo ${b[*]}1 2 3 aaa[[email protected] aming]# unset b[3]  
Arrays in 2.shell 2
    • Array shards
    • A= ( seq 1 5 )
    • Echo ${a[@]:0:3} starts with the first element and intercepts 3
    • Echo ${a[@]:1:4} starts with the second element and intercepts 4
    • Echo ${a[@]:0-3:2} Starts from the bottom 3rd element and intercepts 2
      Array substitution
    • Echo ${a[@]/3/100}
    • A= (${a[@]/3/100})
Alarm system Requirements Analysis 1.shell Project-Alarm system
    • 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 the entire script portal, 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.
    • Subroutine: This is the real monitoring script, used to monitor each indicator.
    • Mail Engine: It is implemented by a Python program that defines the server to which the message is sent, the person who sent it, 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.

Under the bin is the main program;
Conf is the configuration file;
Shares is the various monitoring scripts;
Mail engine under mail;
Log is the journal.

function in shell, array of shell, demand analysis of alarm system

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.