Functions, arrays in 71.shell

Source: Internet
Author: User

One, the function in 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} function must be placed at the front
    • Example 1 output the specified parameters
#!/bin/bashinput()  {    echo $1 $2 $# $0}input 1 a b

    • Example 2 summation \
#!/bin/bashsum() {      s=$[$1+$2]      echo $s}sum 1 2

    • Example 3 viewing network card IP
      #!/bin/baship() {ifconfig |grep -A1 "$1"|tail -1 |awk ‘{print $2}‘}read -p "Please input the eth name: " emyip=`ip $e`echo "$e address is $myip"

Ii. Arrays in the shell
    • Define array a= (1 2 3 4 5);
      echo ${a[@]}                 显示整个数组echo ${#a[@]}              获取数组的元素个数 echo ${a[2]}                  读取第三个元素,数组从0开始echo ${a[*]} 等同于 ${a[@]}    显示整个数组数组赋值a[1]=100; echo ${a[@]}a[5]=2; echo ${a[@]}         如果下标不存在则会自动添加一个元素数组的删除unset a[1] ;echo ${a[*]}   //删除数组中第1个元素unset a;echo ${a[*]}       //清空数组


    • 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中的3替换成100(只显示到屏幕,并不更改数组)a=(${a[@]/3/100})   //将数组a中的3替换成100

      Three, Shell project-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 should have a log output.

Functions, arrays

in 71.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.