Shell script functions, array in shell, Shell project-alarm system

Source: Internet
Author: User
Tags get ip


Functions in a shell script
    • 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


}
Functions must be placed at the front, function can omit the direct write function name

Show Column 1, print the shell parameters

[[email protected] shell] # cat fun1.sh
#! / bin / bash
function inp () {
echo "the first par is $ 1"
echo "the sedcond par is $ 2"
echo "the third par is $ 3"
echo "the scritp name is $ 0"
echo "the number of par is $ #"
}
inp b a 2 3 sjkdj
[[email protected] shell] # sh fun1.sh
the first par is b
the sedcond par is a
the third par is 2
the scritp name is fun1.sh
the number of par is 5
The first parameter is b, the second is a, the third is 2, the script name is fun1.sh, and the number of parameters is 5.
You can also define the parameters of the function
[[email protected] shell] # cat fun1.sh
#! / bin / bash
function inp () {
echo "the first par is $ 1"
echo "the sedcond par is $ 2"
echo "the third par is $ 3"
echo "the scritp name is $ 0"
echo "the number of par is $ #"
}
inp $ 1 $ 2 $ 3
[[email protected] shell] # sh fun1.sh 1 2 3
the first par is 1
the sedcond par is 2
the third par is 3
the scritp name is fun1.sh
the number of par is 3
Show column 2, the first parameter and the second parameter are added and printed

[[email protected] shell] # cat fun2.sh
#! / bin / bash
sum () {
s = $ [$ 1 + $ 2]
echo $ s
}
sum 1 10
[[email protected] shell] # sh fun2.sh
11
Show column 3, enter the network card name, get ip
Function core command deduction
[[email protected] shell] # ifconfig | grep -A1 "ens33"
ens33: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 192.168.21.128 netmask 255.255.255.0 broadcast 192.168.21.255
ens33: 0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 192.168.21.133 netmask 255.255.255.0 broadcast 192.168.21.255
[[email protected] ux01 shell] # ifconfig | grep -A1 "ens33:"
ens33: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 192.168.21.128 netmask 255.255.255.0 broadcast 192.168.21.255
[[email protected] shell] # ifconfig | grep -A1 "ens33:" | awk ‘/ inet /‘
        inet 192.168.21.128 netmask 255.255.255.0 broadcast 192.168.21.255
[[email protected] shell] # ifconfig | grep -A1 "ens33:" | awk ‘/ inet /‘ | awk -F ‘‘ ‘{print $ 2}’
192.168.21.128

[[email protected] shell] # cat fun3.sh
#! / bin / bash
ip ()
{
 ifconfig | grep -A1 "$ 1:" | awk ‘/ inet /‘ | awk -F ‘‘ ‘{print $ 2}’
}
while:
do
  read -p "Please input the eth name:" eth
  n = `ifconfig | grep" $ eth "`
  if [-z "$ eth"]
    then
      echo "You must enter a network card name"
    elif [-z "$ n"]
      then
        echo "Please enter the correct network card name"
        continue
    else
      break
   fi
done
ip_ = `ip $ eth`
if [-z "$ ip_"]
  then
    echo "$ eth: this network card is not configured with ip"
  else
    echo "$ eth: $ ip_"
fi
[[email protected] shell] # sh fun3.sh
Please input the eth name:
You must enter a network card name
Please input the eth name: dada
Please enter the correct network card name
Please input the eth name: ens33
ens33: 192.168.21.128
[[email protected] shell] # sh fun3.sh
Please input the eth name: ens37
ens37: This network card is not configured with ip
Array in shell
Define the array, a = (1 2 3 4); echo $ {a [@]}. The numbers here can also be written as strings
[[email protected] shell] # a = (as b cd)
[[email protected] shell] # echo $ {a [@]}
as b cd
[[email protected] shell] # echo $ {a [*]}
as b cd
echo $ {a [2]} read the third element, the array starts at 0
[[email protected] shell] # echo $ {a [2]}
cd
[[email protected] shell] # echo $ {a [1]}
b
[[email protected] shell] # echo $ {a [0]}
as
echo $ {# a [@]} Get the number of elements in the array
[[email protected] shell] # echo $ {# a [*]}
3
Array assignment and change, if the subscript does not exist, it will automatically add an element
[[email protected] shell] # a [5] = 4
[[email protected] shell] # echo $ {# a [*]}
4
[[email protected] shell] # echo $ {a [*]}
as b cd 4
[[email protected] shell] # a [1] = 2
[[email protected] shell] # echo $ {a [*]}
as 2 cd 4
Array deletion
[[email protected] shell] # unset a [0]
[[email protected] shell] # echo $ {a [*]}
2 cd 4
[[email protected] shell] # unset a
[[email protected] shell] # echo $ {a [*]}
Several pieces
Intercept 3 from the first
[[email protected] shell] # a = (seq 1 10)
[[email protected] shell] # echo $ {a [*]}
1 2 3 4 5 6 7 8 9 10
1 2 3

Take 2 from the third last
8 9
Array replacement
[[email protected] shell] # echo $ {a [@] / 7/5}
1 2 3 4 5 6 5 8 9 10
[[email protected] shell] # a = ($ {a [@] / 8/3})
[[email protected] shell] # echo $ {a [@]}
1 2 3 4 5 6 7 3 9 10
Shell project-analysis of alarm system requirements
Requirements: Use the shell to customize various personalized alarm tools, but unified management and standardized management are required.
Idea: Specify a script package, including main program, subprogram, configuration file, mail engine, output log, etc.
Main program: as the entrance of the entire script, it is the lifeblood of the entire system.
Configuration file: It is a control center, which is used to switch various subprograms and specify each associated log file.
Subroutine: This is the real monitoring script used to monitor various indicators.
Mail engine: It is implemented by a python program, which can define the server, the sender and the sender password
Log output: The entire monitoring system must have log output.
Requirements: Our machine roles are diverse, but the same monitoring system must be deployed on all machines, that is to say, all machines no matter what role, the entire program framework is consistent, the difference is that according to different roles, customized Configuration file.
Program architecture:
                                   (Home directory mon)
                                               |
 -------------------------------------------------- ------------------------------------
 | | | | |
bin conf shares mail log
[main.sh] [mon.conf] [load.sh 502.sh] [mail.py mail.sh] [mon.log err.log]
Under bin is the main program
conf is the configuration file
Under the shares are various monitoring scripts
Under mail is the mail engine
Under the log is the log.
Functions in shell scripts, arrays in shells, shell items-alarm systems

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.