shell中的函數與數組

來源:互聯網
上載者:User

標籤:函數   數組   

shell中的函數

1.第一個案例:

[[email protected] shell]# sh fun1.sh the first par is bthe second par is athe third par is 2the script name is fun1.shthe number of par is 5[[email protected] shell]# cat fun1.sh #!/bin/bashfunction inp(){    echo "the first par is $1"    echo "the second par is $2"    echo "the third par is $3"    echo "the script name is $0"    echo "the number of par is $#"}inp b a 2 3 adf

也可以把參數放在外面

[[email protected] shell]# cat fun1.sh #!/bin/bashfunction inp(){    echo "the first par is $1"    echo "the second par is $2"    echo "the third par is $3"    echo "the script name is $0"    echo "the number of par is $#"}inp $1 $2 $3 [[email protected] shell]# sh fun1.sh 1 2the first par is 1the second par is 2the third par is the script name is fun1.shthe number of par is 2

2.第二個案例:

[[email protected] shell]# cat fun2.sh #!/bin/bashsum(){    s=$[$1+$2]    echo $s}sum 1 10[[email protected] shell]# sh fun2.sh 11[[email protected] shell]# sh -x fun2.sh + sum 1 10+ s=11+ echo 1111

3.第三個案例:

[[email protected] shell]# cat fun3.sh #!/bin/baship(){    ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘}read -p "please input the eth name: " ethip $eth [[email protected] shell]# sh fun3.sh please input the eth name: ens33192.168.188.130[[email protected] shell]# sh fun3.sh please input the eth name: ens33:0192.168.188.150[[email protected] shell]# sh fun3.sh please input the eth name: ens37192.168.252.128
shell中的數組

1.定義數組並列印:

[[email protected] shell]# b=(1 2 3)[[email protected] shell]# echo $b1[[email protected] shell]# echo ${b[@]}1 2 3[[email protected] shell]# echo ${b[*]}1 2 3[[email protected] shell]# echo ${b[1]}2[[email protected] shell]# echo ${b[0]}1[[email protected] shell]# echo ${b[2]}3

2.擷取數組元素個數:加個#

[[email protected] shell]# echo ${#b[*]}3

3.數組賦值:

[[email protected] shell]# b[3]=a[[email protected] shell]# echo ${b[*]}1 2 3 a[[email protected] shell]# b[3]=66[[email protected] shell]# echo ${b[*]}1 2 3 66

4.如果下標不存在,自動添加一個:

[roo[email protected] shell]# b[5]=6[[email protected] shell]# echo ${b[*]}1 2 3 66 6

5.刪除元素:

[[email protected] shell]# unset b[3][[email protected] shell]# echo ${b[*]}1 2 3 6 6[[email protected] shell]# unset b[4][[email protected] shell]# echo ${b[*]}1 2 3 6[[email protected] shell]# unset b[[email protected] shell]# echo ${b[*]}


6.數組分區:

[[email protected] shell]# a=(`seq 1 10`)[[email protected] shell]# echo ${a[*]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${a[*]:3:4}4 5 6 7[[email protected] shell]# echo ${a[*]:-3:2}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${a[*]:0-3:2}8 9

7.數組替換:

[[email protected] shell]# echo ${a[*]/8/6}1 2 3 4 5 6 7 6 9 10[[email protected] shell]# a=(${a[*]/8/6})[[email protected] shell]# echo ${a[*]}1 2 3 4 5 6 7 6 9 10
警示系統需求分析

shell中的函數與數組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.