用for循环实现:
1、判断/var/目录下所有文件的类型
方法一:
#!/bin/bash for filename in /var/*;do if [ -h $filename ] ;then echo "$filename is syslink file" elif [ -d $filename ] ;then echo "$filename is dirction" elif [ -f $filename ] ;then echo "$filename is common file" else echo "$filename is other type file" fi done
2、添加10个用户user1-user10,密码同用户名
#!/bin/bash for name in user{1..10} do if cat /etc/passwd | cut -d: -f1 | grep -q "^$name$";then echo "$name already exits" else useradd $name echo "$name" | passwd --stdin $name echo "$name has been added" cat /etc/passwd | grep "^$name\b" fi done
批量删用户
for id in {1..10};do userdel -r user$id ;echo user$id has been deleted ;done
3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的 文件;分别读取每个文件,以K开头的文件输出为文件加stop ,以S开头的文件输出为文件名加start; “K34filename stop” “S66filename start”
#!/bin/bash for file in `ls /etc/rc.d/rc3.d/`;do if echo $file | grep -q "^S" ;then echo "$file start" elif echo $file | grep -q "^K" ;then echo "$file stop" else echo"other type file" fi done
4、写一个脚本,提示输入正整数n的值,计算1+2+3+…n的总和
#!/bin/bash read -p "Please input a number:" lastnum sum=0 if expr $lastnum + 0 &> /dev/null;then for num in $(seq $lastnum);do let sum=$sum+$num done echo "The sum is $sum" else echo "Please input a correct number" fi
5、写一个脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
#!/bin/bash read -p "Please inout an ipaddr(eg:10.1.252.1):" ip up=0;down=0 ipv4=`echo $ip|cut -d. -f1-3` for ipv in {0..255};do IP="$ipv4"."$ipv" if ping "$IP" -c 1 -W 1 &> /dev/dull;then echo "The $IP up" let up+=1 else echo "The $IP down" let down+=1 fi done echo "IPv4($ipv4.1-255) up is :$up" echo "IPv4($ipv4.1-255) down is :$down"
6、打印九九乘法表
#!/bin/bash for row in {1..9};do for line in $(seq 1 $row);do echo -ne "${line}x${row}=$[row*line]\t" done echo done
使用until实现:
1、求100以内所有正整数之和
#!/bin/bash i=100 sum=0 until [ $i -lt 0 ] ; do let sum+=i let i-- done echo "1 to 100 sum is: $sum"
2、通过ping命令探测10.1.252.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。
#!/bin/bash let i=1 until [ $i -gt 254 ] ;do if ping -c 1 -W 1 10.1.252.$i &> /dev.null;then echo "10.1.252.$i is up" ;let up++ else echo "10.1.252.$i is down" ;let down++ fi let i++ done echo "up number is $up" echo "down number is $down"
3、打印九九乘法表
#!/bin/bash row=1 until [ $row -gt 9 ] ;do line=1 until [ $line -gt $row ] ;do echo -ne "${line}x${row}=$[row*line]\t" let line++ done let row++ echo done
4、利用变量RANDOM生成10个随机数字,输出这个10数字 ,并显示其中的最大者和最小者
#!/bin/bash #Auther:liushaoshao #time:Tue Aug 17 11:01:16 CST 2016 #version:1.0 #discription:find the max and min number from 10 random numbers times=1 max=$RANDOM echo "$max" min=$max until [ $times -gt 9 ];do i=$RANDOM echo "$i" if [ $i -lt $min ];then min=$i elif [ $i -gt $max ];then max=$i fi let times++ done echo "The max number is $max" echo "The min number is $min"
5、打印国际象棋棋盘
用for循环完成
#!/bin/bash for row in $(seq 1 8);do for line in $(seq 1 8);do let sum=("$row"+"$line")%2 if [ "$sum" -eq 1 ];then echo -ne "\033[41m \033[0m" else echo -ne "\033[47m \033[0m" fi done echo done
用until循环
#!/bin/bash row=1 until [ $row -gt 8 ] ;do line=1 until [ $line -gt 8 ] ;do let sum=("$row"+"$line")%2 if [ "$sum" -eq 1 ];then echo -ne "\033[41m \033[0m" else echo -ne "\033[47m \033[0m" fi let line++ done let row++ echo done
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
#/bin/bash #author:liushaoshao #version:1.0 #date:Wed Aug 17 19:47:15 CST 2016 #description:send warn message if hacker come while true ;do echo "$(date +"%F %T") $(hostname) logged on" &> /dev/null;then echo time is `date` hostname is `hostname` &> /var/log/login.log echo "Please keep away from here or you will be arrested!" break else echo "no danger" fi sleep 3s done
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小 ,相等则退出。
#/bin/bash #author:liushaoshao #version:1.0 #date:Wed Aug 17 17:07:15 CST 2016 #description:input a number to match the random number target=$[$RANDOM%10] input=11 until [ $input -eq $target ] ;do read -p "Please input an gucess number:" input if [ $input -gt $target ] ;then echo "It is a little lage" elif [ $input -lt $target ] ;then echo "It is a little small" else echo "the number:$input is right,you win!" fi done
运行结果
[root@shao testdir]# bash caizi.sh Please input an gucess number:4 It is a little small Please input an gucess number:6 It is a little small Please input an gucess number:8 the number:8 is right,you win!
3、用*打印等腰三角形、倒的等腰三角形
#!/bin/bash #author:liushaoshao #version:1.0 #date:Wed Aug 17 19:07:15 CST 2016 #description:print a triangle # read -p "Please input a positive int number to represent the hight:" hight if expr $hight + 0 &>/dev/null;then for ((row=1;row<=hight;row++));do space=$[$hight-$row] star=$[$row*2-1] for ((i=0;i运行结果
[root@shao testdir]# bash triangle.sh Please input a positive int number to represent the hight:9 * *** ***** ******* ********* *********** ************* *************** *****************