Demand analysis of array warning system in Shell function shell

Source: Internet
Author: User
Tags get ip


I. Functions in the shell

[Email protected] aming]# cd/root/shell/aming
[Email protected] aming]# vim fun1.sh//need to be aware that function names cannot conflict with some keywords in the shell
#!/bin/bash
function InP () {
Echo $ $ $ $#
}





INP 1 a 2
[Email protected] aming]# sh fun1.sh
1 a 2 fun1.sh 3//$0 is the script name, 3 is the number of arguments



Continue to improve the script:
[Email protected] aming]# vim fun1.sh
#!/bin/bash
function InP () {
echo "The first par is $"
echo "The second par is $"
echo "The third par is $"
echo "The SCRITP name is $"
echo "The number of par is $#"
}



INP B a 2 3 ADF
[[Email protected] aming]# sh fun1.sh//Execute script
The first par is b
The second par is a
The third par is 2
The SCRITP name is fun1.sh
The number of par is 5



To modify a script:
[Email protected] aming]# vim fun1.sh
#!/bin/bash
function InP () {
echo "The first par is $"
echo "The second par is $"
echo "The third par is $"
echo "The SCRITP name is $"
echo "The number of par is $#"
}



INP $ $
[[Email protected] aming]# sh fun1.sh 1//If you write a parameter here, see the results of the operation
The first par is 1
The second par is
The third par is
The SCRITP name is fun1.sh
The number of par is 1

Define a function for addition, the function defined in the shell must be placed above
[Email protected] aming]# vim fun2.sh
#!/bin/bash
Sum () {
S=$[$1+$2]//s is a variable, s=$1+$2
Echo $s
}



Sum 1 10//Sum 1+10
[[Email protected] aming]# sh fun2.sh//Execute script
11
[Email protected] aming]# sh-x fun2.sh


    • Sum 1 10
    • s=11
    • Echo 11
      11

      This function is specifically used to display the IP
      [Email protected] aming]# vim fun3.sh
      #!/bin/bash
      IP ()
      {
      Ifconfig |grep-a1 "$:" |awk '/inet/{print $} '
      }


Read-p "Please input the ETH name:" ETH
IP $eth
[[email protected] aming]# sh-x fun3.sh//Execute script


    • Read-p ' Please input the ETH name: ' ETH
      Please input the ETH name:ens33:0//Enter parameter
    • IP ens33:0
    • Ifconfig
    • Grep-a1 ' ens33:0: '
    • awk '/inet/{print $} '
      192.168.238.150//Get IP for ens33:0 NIC


Improved script: Need to determine whether the input card is the network card in the system, if the network card exists, IP does not exist, how to judge


What we need now is to look at the IP information of ENS33 this NIC:
[[email protected] aming]# ifconfig |grep-a1 "Ens33"//-A1 indicates the display of keywords, including the following line, but it sees the information of two network cards, including the virtual network card information, continue to let it only display the ENS33 network card IP
Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.238.128 netmask 255.255.255.0 broadcast 192.168.238.255


ens33:0: Flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.238.150 netmask 255.255.255.0 broadcast 192.168.238.255



[[email protected] aming]# ifconfig |grep-a1 "ENS33:"//can find two network card name is not the same place
Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.238.128 netmask 255.255.255.0 broadcast 192.168.238.255
You have new mail in/var/spool/mail/root
[[email protected] aming]# ifconfig |grep-a1 "ens33:" |grep ' inet '//filter out inet this line
inet 192.168.238.128 netmask 255.255.255.0 broadcast 192.168.238.255
[[email protected] aming]# ifconfig |grep-a1 "ens33:" |awk '/inet/{print $} '//Use this command to filter IP
192.168.238.128
[[email protected] aming]# ifconfig |grep-a1 "ens33:" |grep ' inet ' |awk ' {print $} '//two commands can be
192.168.238.128
Write shell scripts need to constantly debug, constantly to seek results, to achieve the preset, learning shell scripts must be more practice,


Ii. Arrays in the shell

[Email protected] aming]# b= (1 2 3)//define Array
[[email protected] aming]# echo ${b[@]}//Get the number of elements of an array, get the entire array
1 2 3
[Email protected] aming]# echo ${b[]}
1 2 3
[[email protected] aming]# echo ${b[0]}//square brackets The number indicates subscript, indicating the element is the first
1
[[email protected] aming]# echo ${#b [@]}//#Represents a number
3
Array Assignment
[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]//delete array
[Email protected] aming]# unset b
[Email protected] aming]# echo ${b[
]}//Erase the value of the array

[Email protected] aming]# a= (seq 1 10)
[[email protected] aming]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
Demand: Intercept 4-7 of these four numbers, in fact, from the beginning of the 3rd element to intercept 4 numbers
Echo ${a[@]:0:3} where the colon is the delimiter, 0 means starting from the first element, followed by a number of colons to intercept several
Then the above requirements can be written like this:
[[email protected] aming]# echo ${a[@]:3:4}//Intercept 4 numbers from the beginning of the 3rd element
4 5 6 7
[[email protected] aming]# echo ${a[@]:0-3:2}//Start with the last third element and intercept 2 digits
8 9


Array substitution
[[email protected] aming]# echo ${A[@]/8/6}//change 8 to 6
1 2 3 4 5 6 7 6 9 10



Three, the alarm system needs analysis



Demand analysis of array warning system in Shell function 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.