加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell的函数引用和数组

发布时间:2020-12-15 23:27:06 所属栏目:安全 来源:网络整理
导读:shell中的函数 函数就是吧一段代码整理到一个小单元中,并给这个小单元其一个名称,当用到这段代码时直接调用这个名称即可 函数中的特殊调用 linux中shell变量 $#,[email?protected],$0,$1,$2的含义解释:变量说明:$$Shell本身的PID(ProcessID)$!Shell最后
shell中的函数

函数就是吧一段代码整理到一个小单元中,并给这个小单元其一个名称,当用到这段代码时直接调用这个名称即可

函数中的特殊调用
linux中shell变量

$#,[email?protected],$0,$1,$2的含义解释:
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
[email?protected]
所有参数列表。如"[email?protected]"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0

Shell本身的文件名

这里需要注意$1-$n数值的意义
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。

?
特殊$表示的含义,示例:

#!/bin/bash
#
printf "The complete list is %sn" "$$"
printf "The complete list is %sn" "$!"
printf "The complete list is %sn" "$?"
printf "The complete list is %sn" "$*"
printf "The complete list is %sn" "[email?protected]"
printf "The complete list is %sn" "$#"
printf "The complete list is %sn" "$0"
printf "The complete list is %sn" "$1"
printf "The complete list is %sn" "$2

结果:

[[email?protected] ~]$ bash params.sh 123456 QQ
The complete list is 24249
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456
The complete list is QQ
The complete list is 2
The complete list is params.sh
The complete list is 123456
The complete list is QQ

定义一个使用函数公式计算的数值结果
定义一个加法公式,引用函数将某两个数字相加得和

[[email?protected] src]# vim jia.sh 
#!/bin/bash
sum () {
 ? ? ?s=$[$1+$2]
 ? ? ?echo $s
}
sum 20 10
[[email?protected]host src]# sh -x jia.sh 
+ sum 20 10
+ s=30
+ echo 30
30

shell函数脚本输出网卡ip
输入的网卡名称并输出显示该网卡的ip地址

[[email?protected] src]# cat fun1.sh 
ip () {
 ?  ifconfig |grep -A1 "$1: " |awk ‘/inet/ {print $2}‘
 } ? ? 
read -p "Please input the eth name: " eth
ip $eth
[[email?protected] src]# sh -x fun1.sh 
+ read -p ‘Please input the eth name: ‘ eth
Please input the eth name: ens33
+ ip ens33
+ grep -A1 ‘ens33: ‘
+ awk ‘/inet/ {print $2}‘
+ ifconfig
192.168.1.234

判断输入网口名称并提取ip地址的while循环和if判断逻辑

[[email?protected] src]# cat fun1.sh 
#!/bin/bash
 ?
ip () {
 ?  ifconfig |grep -A1 "$1: " |awk ‘/inet/ {print $2}‘
 } ? ? 
while :;
do 
  read -p "Please input the eth name: " eth
 ?n=`ifconfig |grep $eth`
 ? ?if [ -z $eth ]
 ? ? then 
 ? ? ? echo "必须输入一个网口名称"
 ? ? ? ? elif [ -z "$n" ]
 ? ? ? ? ?then
 ? ? ? ? ? ? ? echo "s输入正确的网口名称"
 ? ? ? ? ? continue
 ? ? ? ? ? ? else
 ? ? ? ? ? break
 ? ? fi
done
ip_=`ip $eth`
if [ -z $ip_ ]
 ?then
 ? ?echo "$eth:这个网卡没有配置ip"
 ?else
 ? ?echo "$ip_" 
fi

shell中的数组

数组是一个变量中引用的多个数值,数值不一定是数字也可以是字符
定义数组
数组=(数值或字符)

a=(1 2 3 4 5)

获取数组所有的数值,@表示数组内所有的值

echo ${a[@]}

获取数组元素的个数

echo ${#a[@]}

获取数组的第三个元素,数组内数值是按照从0开始的顺序计算的,0对应第一个字符,1对应第二个字符....以此类推

echo ${a[2]}

数组赋值或添加下标元素

a[5]=100

如果该下标不存在元素时则会添加这个指定的数值元素
删除数组或取消某一个元素

uset a

或取消一个下标是第一位、是第三位的数组元素 ? unset a[1]
数组分片

a=(`seq 1 10`) ? 
echo $a{[@]:2:4}

?截取四个字符,从第三个数值开始,0对应数组的数值1的位置,截取四个字符结束,例如结果如下

[[email?protected] src]# echo ${a[@]:2:4}
3 4 5 6

截取倒数字符
echo ${a[@]:0-2:3} ?
0-2表示从一段数组中的哪个位置开始往后计算,3表示截取几个数值。如下操作示例

[[email?protected] src]# echo ${a[@]:0-2:4}
9 10
[[email?protected] src]# echo ${a[@]:0-5:4}
6 7 8 9

数组替换

echo ${a[@]/3/100}
?a=(${a[@]/3/222})

如下,将数组中某个下标数值替换成其他数值

[[email?protected] src]# echo ${a[@]/3/100}
1 2 100 4 5 6 7 8 9 10
[[email?protected] src]# a=(${a[@]/3/222})
[[email?protected] src]# echo ${a[@]}
1 2 222 4 5 6 7 8 9 10

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读