函数+循环

一、函数

随机生成数字函数RANDOM

[root@m01 ~]# echo $RANDOM
2490
2020年 01月 17日 星期五 20:33:49 CST
[root@m01 ~]# echo $RANDOM
19336

限制函数生成范围

[root@m01 ~]# a=$((RANDOM%60))
[root@m01 ~]# echo $a
37
[root@m01 ~]# a=$((RANDOM%60))
[root@m01 ~]# echo $a
46
[root@m01 ~]# a=$((RANDOM%60))
[root@m01 ~]# echo $a
56

格式:

函数名(){
函数体()命令
return
}
[root@m01 scripts]# cat 1-15hanshu.sh
#!/bin/bash
print() {
echo "今天15号了"
echo "调用函数"
echo "函数执行过程"
}
print
[root@m01 scripts]# sh 1-15hanshu.sh
今天15号了
调用函数
函数执行过程

函数传参

[root@m01 scripts]# cat 1-15hanshu.sh
#!/bin/bash
print() {
name=$1
echo "${name}今天15号了"
echo "${name}调用函数"
echo "${name}函数执行过程"
}
print tishi
[root@m01 scripts]# sh 1-15hanshu.sh
tishi今天15号了
tishi调用函数
tishi函数执行过程

在这里插入图片描述
表示位置

[root@m01 scripts]# cat 1-15hanshu.sh
#!/bin/bash
print() {
name=$1
echo "${name}今天15号了"
echo "${name}调用函数"
echo "${name}函数执行过程"
echo $0
echo $#
echo $*
}
print {a..z}
[root@m01 scripts]# sh 1-15hanshu.sh
a今天15号了
a调用函数
a函数执行过程
1-15hanshu.sh
26
a b c d e f g h i j k l m n o p q r s t u v w x y z

将case语句改为函数体

在这里插入图片描述

二、循环

while 输出1-10

[root@m01 scripts]# cat while.sh
#!/bin/bash
i=1
while (( $i < 11 ))
do
echo $i
(( i++ ))
done

[root@m01 scripts]# sh while.sh
1
2
3
4
5
6
7
8
9
10

while累加1-100

[root@m01 scripts]# cat 1-100.sh
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
	((sum+=i))
	((i++))
done
echo $sum	
2020年 01月 15日 星期三 11:00:48 CST
[root@m01 scripts]# sh 1-100.sh
5050
循环比较大小+函数
[root@m01 scripts]# cat xunhuanbijiao.sh 
#!/bin/bash
input(){
read -p "input num1 num2:"  num1 num2
}
#number
#expr $num1  + $num2 + 1   &>/dev/null 
#[ $? -ne 0 ] && {
tow() {
if [[ ! "$num1" =~ ^[0-9]+$ || !  "$num2" =~ ^[0-9]+$ ]]  
then  
    echo "Usage:  Must have  two number" 
    exit 2
fi
}
compare() { 
if [ $num1 -gt $num2 ] 
then
    echo  "$num1 -gt $num2"
elif [ $num1 -lt $num2 ]   #else if 
then
    echo  "$num1 -lt $num2"
else
    echo  "$num1 -eq $num2"
fi
}
while true
do
input
tow
compare
done
[root@m01 scripts]# sh xunhuanbijiao.sh
input num1 num2:22 22
22 -eq 22
input num1 num2:22 23
22 -lt 23
input num1 num2:23 22    
23 -gt 22
input num1 num2:22
Usage:  Must have  two number

输入错误也不退出,继续输入比较
在这里插入图片描述
读取文件内容

[root@m01 scripts]# cat while-read.sh 
#!/bin/bash
file=/etc/hosts
while read line
do 
echo $line
done <$file
2020年 01月 15日 星期三 11:56:10 CST
[root@m01 scripts]# sh while-read.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.41 backup
172.16.1.31 nfs01
172.16.1.7 web01
172.16.1.8 web02
172.16.1.51 db01
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.61 m01
取文件内容并累加第三列
[root@m01 scripts]# cat stu.txt 
01 oldbing  18
02 oldxia   19
03 oldlidao 19
04 oldguo   66

[root@m01 scripts]# cat while-stu.sh
#!/bin/bash
sum=0
file=/server/scripts/stu.txt
while read line
do
i=`echo $line | awk '{print $3}'`
((sum+=i))
done <$file
echo $sum
2020年 01月 15日 星期三 12:14:11 CST
[root@m01 scripts]# sh while-stu.sh
122
三个班级,每班5人,输出各个班级的平均分和年级平均分(分数要求手动输入)
#!/bin/bash
s=0
for (( i=1;i<=3;i++ ))
do
sum=0
    for (( j=1;j<=5;j++ ))
    do
    read -p "请输入"$i"班第"$j"位同学的成绩:" stu
    ((sum+=$stu))
    done
  echo $i"班级的总分为:"$sum 
  echo $i"班级的平均数为:"$((sum/5))
  ((s+=$sum))
done
echo "年级总分为:"$s
echo "班级平均分为:"$((s/3)) 
打印票价信息

在这里插入图片描述

#!/bin/bash
mon=`date +%m`
for (( i=4;i<=10; i++ ))
do
if [ $mon -eq $i ]
  then
    echo "旺季(4-10月)票价列表信息:"
    echo "成人(18-60):60元每位"
    echo "儿童(<18):   半价每位"
    echo "老人(>60):   1/3价每位"
    exit
else
  echo "淡季(11-3月)票价列表信息:"
  echo "成人:40元每位"
  echo "其他:20元每位"
fi
done
~                                                                                                                                        

awk -F ‘[^a-zA-Z]+’ ‘{for(i=1;i<=NF;i++){a[KaTeX parse error: Expected 'EOF', got '}' at position 5: i]++}̲}END{for (word …i]++}}END{for (word in a) print a[word],word}’ string.txt
r ‘,.’ ’ ’ < string.txt |xargs -n1 |awk ‘{a[$1]++}END{for (word in a)print a[word],word}’
tr ‘,.’ ’ ’ < string.txt |xargs -n1 |sort|uniq -c |sort -rn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值