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

Source: Internet
Author: User

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/bash
INP () {
echo "The first is $ $"
echo "The second is $"
echo "The third is $"
echo "The script is $"
echo "The number is $#"
}
INP a b c d E
This function is used to print the parameters of the

[Email protected] shell]# sh fun1.sh
The first is a
The second is B
The third is C
The script is fun1.sh
The number is 5
What we need to note here is that our function name is not the same as the command in the shell. If you are going to call a function, the generic name at the end of the script will be the same as the function name, so it can be called.

Example 2
#!/bin/bash
Sum () {
S=$[$1+$2]
Echo $s
}
Sum 1 11
Run the result as
[Email protected] shell]# sh fun2.sh
12
This is a function that defines addition, S is a variable, equals two numbers, where $ and $ are the first parameter of the function and the second argument is added.
$2 means 1,$2, $s is 1+11.

Example 3
#!/bin/bash
IP () {
Ifconfig |grep-a1 "ens33:" |grep ' inet ' |awk ' {print $} '
}
Read-p "Please input the ETH name:" ETH
IP $eth
This is a script that specifically displays the IP. A1 is the first line that displays the keywords and keywords below.
Please input the ETH name: means to display the name of the NIC.
Echo ' $e address is $myip ' is the IP that displays the network card

Run the result as
[Email protected] shell]# sh-x fun3.sh

    • Read-p ' Please input the ETH name: ' ETH
      Please input the ETH name:ens33
    • IP ens33
    • Grep-a1 ' Ens33: '
    • Ifconfig
    • grep inet
    • awk ' {print $} '
      192.168.218.130
Arrays in the shell

A string of strings or a string of numbers formed by a variable, called an array, we can operate on the array.
For example:
Defines the array b= (a string of numbers or strings);
Show results echo ${b[]}
[Email protected] shell]# b= (1 2 3)
[[email protected] shell]# echo ${b[
]}
1 2 3

We can also view the value of an element
For example:
We want to see the value of an element
Echo ${a[2]}
[[email protected] shell]# echo ${b[2]}
3
Here is a special, array starting from 0, that is, 0 is the first element.

We can also display the number of elements
echo ${#b []} Gets the number of elements in the array, #表示元素个数.
For example:
[[email protected] shell]# echo ${#b [
]}
3

Array Assignment
We can also assign values to one of the elements of an array.
[Email protected] shell]# B[3]=a
[[email protected] shell]# echo ${b[]}
1 2 3 A
We have added a new element A in this, directly after the printing time, this is because the subscript does not exist will automatically add an element
can also be replaced
[Email protected] shell]# B[0]=AAA
[[email protected] shell]# echo ${b[
]}
AAA 2 3 A

We can also delete the value of the array, using the unset command
[[email protected] shell]# echo ${b[]}
AAA 2 3 A
[Email protected] shell]# unset b[3]
[[email protected] shell]# echo ${b[
]}
AAA 2 3

Array shards
Sometimes we want to intercept part of the array
[[email protected] shell]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
For example
We're going to intercept from 4 to 7.

4 5 6 7
Intercept from behind

8 9
What we need to note here is that the interception from behind must be written in 0-or else it will not succeed.

Substitution of arrays
[[email protected] shell]# echo ${a[]/8/6}
1 2 3 4 5 6 7 6 9 10
We can also assign values directly
[[email protected] shell]# echo ${a[
]/3/100}
1 2 100 4 5 6 7 8 9 10
[[email protected] shell]# echo ${A[*]/100/3}
1 2 3 4 5 6 7 8 9 10

Demand analysis of 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 to have log output

function in shell, array in 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.