shell-script-03 脚本实例

shell-script-03 脚本实例

1、演示函数参数的返回值

1)简单的加法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# vim test_add.sh
[root@localhost ~]# cat test_add.sh
#!/bin/bash
#数字相加

function add()
{
sum=$[$1+$2]
return $sum
}
[root@localhost ~]# source test_add.sh
[root@localhost ~]# add 3 4
[root@localhost ~]# echo $?
7
[root@localhost ~]#

解析

  • sum=$[$1+$2]不能用sum=$1+$2来计算,因为这样得到的sum值为“3+4”这样一个字符串;
  • sum=$[$1+$2]也不能写成sum=$[1+2],因为这样得到的sum值恒为3;

2)利用for循环计算0加到100

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# ./sum.sh 
0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100=5050
[root@localhost ~]# cat sum.sh
#!/bin/bash
sum=0
numbers=0
for i in `seq 100`
do
numbers=$numbers+$i
sum=$[sum+i]
done
echo "$numbers=$sum"
[root@localhost ~]#

解析

  • numbers=$numbers+$i得到的是字符串
  • sum=$[sum+i]得到的是计算结果

2、测试ping某个网址的丢包率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# vim loss.sh
[root@localhost ~]# cat loss.sh
#!/bin/bash
#判断ping某个网址的丢包率所占百分比,ping次数在脚本内修改
if [ $# == 1 ];then
ping $1 -c 2 | grep 'packet loss' | awk {'print $6'}
else
echo "Usage: /root/ping.sh <url>"
fi
[root@localhost ~]# . loss.sh
Usage: /root/ping.sh <url>
[root@localhost ~]# . loss.sh baidu.com
0%
[root@localhost ~]#

3、计算三个数中最大值、最小值以及平均值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@localhost ~]# vim cal.sh 
[root@localhost ~]# cat cal.sh
#!/bin/bash
#计算三个数中最大值、最小值以及平均值
if [ $# != 3 ];then
echo '/root/1.sh number1 number2 number3'
exit 1
fi
min=$1
max=$1
sum=0
for i in $@
do
if (("$i" > "$max"));then
max=$i
fi
if (("$i" < "$min"));then
min=$i
fi
sum=$[ sum +i ]
done
echo "max is $max"
echo "min is $min"
echo "scale=2; $sum / $#" | bc
[root@localhost ~]# . cal.sh 1 2 4
max is 4
min is 1
2.33
[root@localhost ~]# . cal.sh 7 8 9
max is 9
min is 7
8.00
[root@localhost ~]#

注意:安装后才能使用bc(linux字符界面下的计算器)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# yum install bc -y
[root@localhost ~]# echo "scale = 2; 5 / 3" | bc
1.66
[root@localhost ~]#


#bc很实用,如下:
[root@localhost ~]# echo 5/3
5/3
[root@localhost ~]# echo 5/3 | bc
1
[root@localhost ~]# echo "scale=2;5/3" | bc
1.66
[root@localhost ~]#

4、 去除字符串中的所有空格

1
2
3
[root@localhost ~]# echo "a b c hello world" | sed 's/ //g'
abchelloworld
[root@localhost ~]#

5、写一个输出数字 0 到 100 中 3 的倍数(0 3 6 9 …)的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# vim for.sh
[root@localhost ~]# cat for.sh
#!/bin/bash
for (( i=0; i<=100; i+=3 ));do
echo $i
done
[root@localhost ~]# . for.sh
0
3
6
9
……
96
99
[root@localhost ~]#

6、判断文件是否为目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@localhost ~]# vim test_alg.sh 
[root@localhost ~]# chmod +x test_alg.sh
[root@localhost ~]# mkdir aaa
[root@localhost ~]# echo "hello" > hello.txt
[root@localhost ~]# ./test_alg.sh
Usage: /root/tes_alg.sh filename
[root@localhost ~]# ./test_alg.sh aaa
aaa is a directory
[root@localhost ~]# ./test_alg.sh hello.txt
hello.txt is a file
[root@localhost ~]# ./test_alg.sh hhhhhh
hhhhhh is not found
[root@localhost ~]# cat test_alg.sh
#!/bin/bash
#判断文件是目录、普通文件还是二者皆非
if [ $# != 1 ];then
echo "Usage: /root/tes_alg.sh filename"
else
file=$1
if [ -d $file ];then
echo "$file is a directory"
else
if [ -f $file ];then
if [ -e $file ];then
echo "$file is a file"
else
echo "$file is neither a file nor a directory"
fi
else
echo "$file is not found"
fi
fi
fi

[root@localhost ~]#

解析

  • -d,判断是否为目录
  • -f,判断是否存在
  • -e,判断是否为文件

7、使用shift写一个遍历参数的脚本

  shift的作用是以空格作为分隔符,截去第一个参数 ,然后将剩余参数左移。

1)如下例中,a先被截去,剩余参数就变为b c d ……;然后b被截去,以此类推,参数个数逐减。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# cat test_shift.sh 
#!/bin/bash
while [ $# != 0 ]
do
echo "now, first param is $1, param counts is $#"
shift
done
[root@localhost ~]# . test_shift.sh a b c d e f g
now, first param is a, param counts is 7
now, first param is b, param counts is 6
now, first param is c, param counts is 5
now, first param is d, param counts is 4
now, first param is e, param counts is 3
now, first param is f, param counts is 2
now, first param is g, param counts is 1
[root@localhost ~]#

2)shift也可以设置截去参数的个数,如下例

注意:由于一次性跨越俩参数,因此while循环条件有所变化

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# cat test_shift.sh 
#!/bin/bash
while [ $# -gt 1 ]
do
echo "now, first param is $1, param counts is $#"
shift 2
done
[root@localhost ~]# . test_shift.sh a b c d e f g
now, first param is a, param counts is 7
now, first param is c, param counts is 5
now, first param is e, param counts is 3
[root@localhost ~]#
欢迎打赏,谢谢
------ 本文结束------
0%