Analysis of function, array and alarm system requirement in shell

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: Print a parameter, which represents the first argument, and $ A for the file name $ #表示参数的个数

#!/bin/bashfunction inp(){                                   #定义函数inp    echo "The first par is $1"    echo "The second par is $2"    echo "The third par is $3"    echo "The scritp name is $0"    echo "The number of par is $#"}inp a l k 3 bm

Script Run Results

[[email protected] shell]# sh hs1.shThe first par is aThe second par is lThe third par is kThe scritp name is hs1.shThe number of par is 5

Example 2: function to define addition

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

Example 3: Enter the name of the network card to display the IP address of the network card

#!/bin/baship(){    ifconfig |grep -A1 "$1: " |awk ‘/inet/ {print $2}‘}read -p "Please input the eth name: " ethip $eth
Arrays in the shell

? Define array a= (1 2 3 4 5); Echo ${a[@]}
? echo ${a[2]} reads the third element, array starting from 0
? echo ${a[*]} equals ${a[@]} displays the entire 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[*]}1 2 3 4 5[[email protected] shell]# echo ${a[0]}1[[email protected] shell]# echo ${a[2]}3

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

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

? Array assignments and changes, and an element is automatically added if the subscript does not exist

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

? Array of deletions, using unset

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

? Array shards
Echo ${a[@]:3:4} starts with the first element and intercepts 3
Echo ${a[@]:0-3:2} Starts from the bottom 3rd element and intercepts 2

[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${a[@]:3:4}4 5 6 7[[email protected] shell]# echo ${a[@]:0-3:2}8 ```9? 数组替换   echo ${a[@]/8/ss}  把8替换为ss

[[email protected] shell]# echo ${a[@]/8/ss}
1 2 3 4 5 6 7 SS 9 1

? a=(${a[@]/8/cc})   也可以直接复制

[[email protected]]# a= (${A[@]/8/CC})
[[email protected] shell]# echo ${a[@]}
1 2 3 4 5 6 7 cc 9

-----# Alarm System Requirements analysis? Requirements: Use 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 entrance, 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 sender and the sender's password. Output log: The entire monitoring system to have the log output. Requirements: Our machine roles are diverse, but all machines have to deploy the same monitoring system, it is said that all machines regardless of role, the entire program framework is consistent, the different place is based on different roles, different configuration file customization. Program Architecture: (main directory Mon) ____________________|___________________                   _________________________________                |                      |                                |                                  |               |                   Bin conf shares mail log |                      |                                |                                  |           | [main.sh] [mon.conf] [load.sh 502.sh] [mail.py mail.sh] [mon.log err.log]bin under the main program conf under is the configuration file shares under the various monitoring scripts mail engine L ogBelow is the log. 

Functions, arrays, alarm system requirements analysis in the shell

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.