shell中的函数
1、#!/bin/bash
function inp(){
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The third parameter is $3"
echo "The number of parameter is $#"
echo "The script's name is $0"
}
#定义一个函数
inp a b c asd sdg
运行结果:
The first parameter is a
The second parameter is b
The third parameter is c
The number of parameter is 5
The script's name is f1.sh
2、#!/bin/bash
function inp(){
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The third parameter is $3"
echo "The number of parameter is $#"
echo "The script's name is $0"
}
inp $1 $2 $3
运行结果:
[root@centos7 shell]# sh f1.sh a b c d
The first parameter is a
The second parameter is b
The third parameter is c
The number of parameter is 3
The script's name is f1.sh
3、sum() {
s=$[$1+$2]
echo "$s"
}
sum $1 $2
运行结果:
[root@centos7 shell]# sh f1.sh 2 4
6、#!/bin/sh
ip(){
ifconfig |grep -A1 "$1: " |tail -1|awk '{print $2}'
}
read -p 'please network name: ' n
myip=ip $n
echo "input ip:"$myip
运行结果:
[root@centos7 shell]# sh ip.sh
please network name: ens37
input ip:192.168.136.128 数组
所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。
1、定义数组
a=(1,2,3,4,a)
2、查看
[root@centos7 shell]# echo ${a