Shell functions and arrays

Source: Internet
Author: User

[TOC]

Shell functions and arrays one, Shell function 1.1 function format 1
function name {    commands}

Example 1:

#! /bin/bashfunction inp(){             //定义一个inp的函数echo $1 $2 $3 $0 $#         }inp 1 a 2 b                 //传入参数             //传入参数

Run results

[[email protected] ~]# sh function1.sh1 a 2 function1.sh 4
    • [] $: The first parameter is the "2" above
    • [] $: The second parameter is the "B" above
    • [] $ $: The third parameter is the "3" above
    • [] $: The script itself is named "function1.sh" on the
    • [] $#: In fact, there are several parameters on this side is "2 B 3 C" that is $# = 4
    • [] [email protected]: On behalf of all parameters 2 B 3 C
1.2 Function Format 2
neme() {    commands}
    • Example 1
#!/bin/bashsum() {             //定义的函数名为sum    s=$[$1+$2]    echo $s}sum 1 2

Run

[[email protected] ~]# sh function2.sh3
    • Example 2
Task: Enter the name of the network card and check the IP address of the network card:

Start with the normal command debug first:

Finally, the valid commands are determined as:

ifconfig |grep -A1 "ens33: " |awk ‘/inet/ {print $2}‘

Function:

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

Operation Result:

[[email protected] ~]# sh funciton3.shplease input the eth name: eth33192.168.72.130192.168.72.150127.0.0.1192.168.122.1

Modified Complete:

vim funciton3.sh#!/bin/baship(){  ifconfig |grep -A1 "$eth " |awk ‘/inet/ {print $2}‘}read -p "please input the eth name: " ethUseIp=`ip $eth`echo "$eth adress is $UseIp"

Operation Result:

[[email protected] ~]# sh funciton3.shplease input the eth name: ens33: ens33: adress is 192.168.72.130[[email protected] ~]# sh funciton3.shplease input the eth name: ens33:0:ens33:0: adress is 192.168.72.150
Array variables and Functions 2.1 array operation (array note the first is actually a a[0], which is not the same as awk)
[[email protected] ~]# b=(1 2 3 4) //定义一个数组a并赋值 1 2 3[[email protected] ~]# echo ${b[*]} //注意输出a的值的格式1 2 3 4[[email protected] ~]# echo ${b[0]} //注意第一个其实是 b[0]开始1[[email protected] ~]# echo ${b[1]}2[[email protected] ~]# echo ${b[@]}1 2 3 4[[email protected] ~]# echo ${#b[@]} //获取数组的元素个数4[[email protected] ~]# echo ${#b[*]} //获取数组的元素个数4
2.2 Assigning values to arrays, redefining
[[email protected] ~]# b[3]=a[[email protected] ~]# echo ${b[3]}a[[email protected] ~]# echo ${b[*]}1 2 3 a[[email protected] ~]# b[4]=a[[email protected] ~]# echo ${b[*]}1 2 3 a a
2.3 Deletion of array elements
[[email protected] ~]# unset b[2] //删除摸个数组元素[[email protected] ~]# echo ${b[*]}1 2 a a[[email protected] ~]# unset b  //删除整个数组[[email protected] ~]# echo ${b[*]}
2.4 Shards of an array
[[email protected] ~]# a=(`seq 1 10`)[[email protected] ~]# echo ${a[*]}1 2 3 4 5 6 7 8 9 10[[email protected] ~]# echo ${a[@]:3:4} //从第数组a[3]开始,截取4个。4 5 6 7[[email protected] ~]# echo ${a[@]:0-3:2} //从倒数第三个数组开始,截取两个8 9[[email protected] ~]# echo ${a[@]/8/6} //把8换成61 2 3 4 5 6 7 6 9 10  

Shell functions and arrays

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.