Shell scripts (functions, arrays in the shell)

Source: Internet
Author: User

function

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

}

The function must be placed first, and the function name can be defined by itself.


Case one:

[Email protected] shell]# vim fun.sh

#!/bin/bash

function InP () {

Echo $ $ $ ${10} ${11} $ $# [$*]

}

INP 1 2 3 4 5 6 7 8 9 34 55


[Email protected] shell]# sh fun.sh

1 2 3 fun.sh 11 [1 2 3 4 5 6 7 8 9 34 55]


Note that the $ $ cannot print out a 10th function, and when n>=10, use {10} to get the function. $ A represents the script name. $ #表示多少个函数.


Case TWO:

[Email protected] shell]# vim fun1.sh


#/bin/bash

Sum () {

S=$[$1+$2]

Echo $s

}

Sum 22 2


[Email protected] shell]# sh fun1.sh

24




Case THREE:

Write a script and enter the network card number to display the IP on the card.

#/bin/bash

IP () {

Ifconfig |grep-a1 "$" |tail-1 |awk ' {print $} '

}

Read-p "Please input the ETH name:" E

myip= ' IP $e '

echo "$e address is $myip"



Operation Result:

[Email protected] shell]# sh fun2.sh

Please input the ETH name:ens33

ENS33 address is 192.168.52.50




Arrays in the shell


To define an array:

[[email protected] ~]# a= (1 2 3 4 5 6)

To view the entire array value:

[[email protected] ~]# echo ${a[@]}

1 2 3 4 5 6

[[email protected] ~]# echo ${a[*]}

1 2 3 4 5 6

View array Single value: (zero-based)

[[email protected] ~]# echo ${a[0]}

1

[[email protected] ~]# echo ${a[1]}

2

[[email protected] ~]# echo ${a[2]}

3


Assign or change a value to an array:

[Email protected] ~]# A[2]=AAA

[[email protected] ~]# echo ${a[*]}

1 2 AAA 4 5 6


Delete the array:

Delete a value in the array:

[Email protected] ~]# unset a[1]

[[email protected] ~]# echo ${a[*]}

1 AAA 4 5 6

[Email protected] ~]# unset a[0]

[[email protected] ~]# echo ${a[*]}

AAA 4 5 6


Delete entire array

[Email protected] ~]# unset a

[[email protected] ~]# echo ${a[*]}



Delete Note Change notes

Array shards:

[[email protected] ~]# echo ${b[@]}

1 2 3 4 5 6


Format: Echo ${array name [@]: Starting from the first few elements: intercepting a few}


Starting with the first element, intercept 2:

[[email protected] ~]# echo ${b[@]:0:2}

1 2


Starting with the second element, intercept 3:

[[email protected] ~]# echo ${b[@]:1:3}

2 3 4


So how to intercept it backwards:

Format: Echo ${array name [@]:0-number of elements: intercept several}


Starting from the bottom 2nd element, intercept 2

[[email protected] ~]# echo ${b[@]:0-2:2}

5 6


Array substitution

Temporary replacement:

[[email protected] ~]# echo ${B[@]/5/6}

1 2 3 4 6 6

[[email protected] ~]# echo ${b[@]}

1 2 3 4 5 6


Permanent Replacement:

[[email protected] ~]# b= (${B[@]/5/6})

[[email protected] ~]# echo ${b[@]}

1 2 3 4 6 6


Shell scripts (functions, arrays in the 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.