码迷,mamicode.com
首页 > 系统相关 > 详细

shell之流程控制

时间:2017-07-31 17:21:39      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:模仿   reg   ++   title   too   cond   cmd   strong   数字   

一、if语句、while、for循环各种小例题

1.用户验证

技术分享
[root@bogon ~]# cat buer.sh 
#!/bin/bash

#user=‘buer‘
#password=‘1234.com‘

read -p ‘please your name:‘ user
read -p ‘password:‘ passwd

if [ $user = ‘buer‘ -a $passwd = ‘1234.com‘ ];then
    echo ‘login successful‘
fi
技术分享

2.猜年龄

技术分享
#!/bin/bash

age=57
read -p ‘num:‘ num

if [ $num -eq $age ];then
    echo ‘you get it‘
elif [ $num -gt $age ];then
    echo ‘too big‘
elif [ $num -lt $age ];then
    echo ‘too samll‘
fi
技术分享

3.查询成绩

技术分享
[root@bogon ~]# cat score.sh 
#!/bin/bash

read -p ‘please your score:‘ score

if [ $score -ge 90 ];then
    echo ‘very good!‘
elif [ $score -ge 70 -a $score -lt 90 ];then
    echo ‘good!‘
elif [ $score -ge 60 -a $score -lt 70 ];then
    echo ‘just like‘
else
    echo ‘poor‘
fi
技术分享

4.for循环打印1235

技术分享
[root@bogon ~]# cat for.sh 
#!/bin/bash

for i in {1..5}
do
    if ((i == 4));then
        continue
        #break
    fi
    echo ‘=======‘ $i
done
技术分享

脚本4的执行结果:

[root@bogon ~]# ./for.sh 
======= 1
======= 2
======= 3
======= 5

 

5.改进版的猜年龄

技术分享
[root@bogon ~]# cat oldboy.sh 

oldboy=57
while :
do
    read -p ‘input your age:‘ age
    if [ -z $age ];then
        continue
    fi
    if [ $age -eq $oldboy ];then
        echo "bingo"
        break
    elif [ $age -lt $oldboy ];then
        echo "little"
    elif [ $age -gt $oldboy ];then
        echo "big"
    else
        echo "bye"
    fi
done
技术分享

脚本5结果:

技术分享
[root@bogon ~]# ./oldboy.sh 
input your age:45
too samll
input your age:65
too big
input your age:57
you get it
[root@bogon ~]# 
技术分享

 

6.查看文件类型

技术分享
[root@bogon ~]# cat aa.sh 
#!/bin/bash

while :
do
    read -p ‘input your file:‘ file
    if [ -z $file ];then
        continue
    elif [ $file = ‘quit‘ ];then
        break
    fi
    if [ -b $file ];then
        echo "$file is block file"
    elif [ -d $file ];then
        echo "$file is directory file"
    elif [ -f $file ];then
        echo "$file is regular file"
    else
        echo "$file unkown"
    fi
done
技术分享

脚本6结果:

[root@bogon ~]# sh aa.sh 
input your file:/etc    
/etc is directory file
input your file:/pm
/pm unkown
input your file:quit

7.打印乘法口诀表

技术分享
[root@bogon ~]# cat cheng.sh 
#!/bin/bash

for ((i=1;i<=9;i++))
do
    for ((j=1;j<=i;j++))
    do
    echo -n "$i*$j=$[$i*$j]"
    done
    echo
done
技术分享

脚本7结果:

技术分享
[root@bogon ~]# ./cheng.sh 
1*1=1
2*1=22*2=4
3*1=33*2=63*3=9
4*1=44*2=84*3=124*4=16
5*1=55*2=105*3=155*4=205*5=25
6*1=66*2=126*3=186*4=246*5=306*6=36
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
技术分享

8.批量创建用户

技术分享
[root@bogon ~]# cat add.sh 
#!/bin/bash

for i in {1..5};
do
    useradd user$i && echo "user$i create successful"
done
技术分享

脚本8执行结果:

[root@bogon ~]# sh add.sh 
user1 create successful
user2 create successful
user3 create successful
user4 create successful
user5 create successful

9.批量删除用户

技术分享
[root@bogon ~]# cat del.sh 
#!/bin/bash

for i in {1..5};
do 
    userdel -r user$i && echo "user$i delete successful"
done
技术分享

脚本9执行结果:

[root@bogon ~]# sh del.sh 
user1 delete successful
user2 delete successful
user3 delete successful
user4 delete successful
user5 delete successful

10.循环叠加打印数字,并睡眠1s

技术分享
[root@bogon ~]# cat conut.sh 
#!/bin/bash

count=1
while ((count < 10))
do
    echo ‘==========>‘ $count
    sleep 1
    ((count+=1))
done
技术分享

执行结果:

技术分享
[root@bogon ~]# sh conut.sh 
==========> 1
==========> 2
==========> 3
==========> 4
==========> 5
==========> 6
==========> 7
==========> 8
==========> 9
技术分享

11.批量ping  ip

技术分享
[root@bogon ~]# cat ping.sh 
#!/bin/bash

for i in {1..100};
do
    ping -c1 192.168.16.$i &> /dev/null
    if [ $? -eq 0 ];then
        echo "192.168.16.$i OK"
        echo "192.168.16.$i" >> /tmp/ip.txt
    fi
done
技术分享

12.模仿登录用户小工具

技术分享
[root@bogon ~]# cat login.sh 
#!/bin/bash

user=‘buer‘
passwd=‘1234.com‘
while $tag
do
    read -p ‘input your name:‘ name
    read -p ‘input your passwd:‘ pas
    if [[ $name = $user ]] && [[ $pas = $passwd ]];then
        echo ‘login successful‘
        while $tag
        do
            read -p ‘>>:‘ cmd
            if [[ $cmd = ‘quit‘ ]];then
                tag=false
            else
                $cmd
            fi
        done
    fi
done
技术分享

执行结果:

技术分享
[root@bogon ~]# sh login.sh 
input your name:buer
input your passwd:1234.com
login successful
>>:ls
aa.sh         a.txt       del.sh         oldboy.sh  模板  下载
add.sh         buer.sh   for.sh         ping.sh    视频  音乐
age.sh         cheng.sh  initial-setup-ks.cfg  score.sh   图片  桌面
anaconda-ks.cfg  conut.sh  login.sh         公共        文档
>>:pwd
/root
>>:quit
[root@bogon ~]# 
技术分享

shell之流程控制

标签:模仿   reg   ++   title   too   cond   cmd   strong   数字   

原文地址:http://www.cnblogs.com/xiaoyonglaing/p/7263894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!