Shell Script Basics (iv)

Source: Internet
Author: User

I. 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.

1. function format
function f_name() {       command}

The function must be placed at the front of the script.

2. Shell function Instance 1:
[[email protected] shell]# vim func01.sh#! /bin/bashfunction input(){    echo $1 $2 $3 $0 $#}input 4 5 6## $0   脚本名称## $1-9脚本执行时的参数1到参数9## $?   脚本的返回值    ## $#   脚本执行时,输入的参数的个数[[email protected] shell]# sh func01.sh 4 5 6 func01.sh 3
Example 2:
[[email protected] shell]# vim func02.sh #!/bin/bashfunction sum(){    sum=$[$1+$2]    echo $sum}sum 1 2[[email protected] shell]# sh func02.sh 3
Example 3: Check the network card IP
[[email protected] shell]# ifconfig | grep -A1 ens    //查看网卡IPens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 192.168.242.128  netmask 255.255.255.0  broadcast 192.168.242.255--ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 192.168.248.128  netmask 255.255.255.0  broadcast 192.168.248.255[[email protected] shell]# vim func03.sh #! /bin/bashfunction checkip(){## awk -F分隔符用得空格,也可以用另一种写法## ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘    ifconfig | grep -A1 "$1:" | tail -1 | awk -F ‘ ‘ ‘{print $2}‘}read -p "请输入网卡名称:" ethcheckip $eth[[email protected] shell]# sh func03.sh 请输入网卡名称:ens33192.168.242.128[[email protected] shell]# sh func03.sh 请输入网卡名称:ens37192.168.248.128
Ii. Arrays in the shell
定义数组 a=(1 2 3 4 5); echo ${a[@]}echo ${#a[@]} 获取数组的元素个数echo ${a[2]} 读取第三个元素,数组从0开始echo ${a[*]} 等同于 ${a[@]} 显示整个数组

Array Assignment

a[1]=100; echo ${a[@]}a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素数组的删除unset a; unset a[1]

Array shards

a=(seq 1 5)echo ${a[@]:0:3} 从第一个元素开始,截取3个echo ${a[@]:1:4} 从第二个元素开始,截取4个echo ${a[@]:0-3:2} 从倒数第3个元素开始,截取2个数组替换echo ${a[@]/3/100}a=(${a[@]/3/100})
Three, the alarm system needs 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 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.

Program Architecture

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.

Shell Script Basics (iv)

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.