shell脚本forloops题解

1、判断/var/目录下所有文件的类型

for files in   /var/* ;do
    if [[ -f $files ]];then
        echo "$files exists and is a regular file."
    elif [[ -h $files ]] ;then
        echo "$files is a symbolic link."
    elif [[ -d $files ]] ;then
        echo "$files is a directory."
    else
        echo "$files is others."
    fi
done

2、添加10个用户user1-user10,密码为8位随机字符

for user in user{1..10} ;do
    id $user &> /dev/null
    if [[ $? -ne 0 ]] ;then
        useradd $user
#openssl rand -base64 6  ; uuidgen |cut -c8
        pw=`openssl rand -base64 6`
        echo  $pw|passwd --stdin  $user &> /dev/null
        echo "user:$user, password : $pw" >> /root/pw_file
    else
        echo "Error:$user is exsit"
        exit
    fi
    if [ $user == "user10" ] ;then
        echo "user is created,password is in  /root/pw_File"                                                                                                           
    fi
done

#删除10个用户
for i in {1..10} ;do userdel -r user$i ;done

3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start


for files in /etc/rc.d/rc3.d/[KS]* ;do
    if [ `basename $files|cut -c1` == "K" ] ;then
        echo "$files stop"
    else
        echo "$files start"                                                                                                                                            
    fi
done

4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和

read -p "please input a positive integer: " INT
if [[ $INT =~ ^[0-9]+$ ]] && [[ `expr $INT` != 0 ]] ;then
    sum=0
    for i in `seq $INT`;do
        let sum+=i
    done
    echo "sum=$sum"
else
    echo "Error:your input is not positive integer"
fi

5、计算100以内所有能被3整除的整数之和

sum=0
for i in `seq 3 3 100` ;do
    let sum+=i
done
echo "sum=$sum"
for((i=3,sum=0; i<=100; i+=3,sum+=i));do
    echo -n                                                                                                                                                            
done
echo "sum:$sum"

6、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

read -p "please input IP:" IP
IPC=`echo $IP |awk -F. '$1=255&&$2<=255&&$3<=255&&$4<=255{print "0"}'`
IPCHECK=`echo $IP |grep -Eo "^[0-9]{1,3}\.[0-9]{1,3}\."`
if echo $IP |grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ;then
    echo "start>>>" >>/root/ipup.txt
    if [ $IPC -eq 0 ] ;then
        for IP1 in {0..254} ;do
            for IP2 in `seq 254`;do
                {
                ping -c1 -w1  $IPCHECK$IP1.$IP2 &> /dev/null
                [ $? -eq 0 ] && echo "The host $IPCHECK$IP1.$IP2  run up" >> /root/ipup.txt
                }&
            done
            wait
        done
    else
        echo "your IP $IP is not available"
    fi
else
    echo "your IP $IP format is wrong"
fi

7、打印九九乘法表

for i in {1..9};do
    for j in `seq $i`;do
        echo -en "$i*$j=$(($i*$j))\t"
    done
    echo
done

8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

for i in {1..10};do
    NUM=`openssl rand -base64 6|tr -dc '[:alpha:]' |head -c10`
    touch /data/${i}${NUM}.html
done
unset NUM

9、打印等腰三角形

read -p "please input the triangle number: " NUM
for n in `seq $NUM` ;do
{
    for a in `seq $[$NUM-$n]` ;do
        echo -n " "
    done
    for b in `seq $[2*$n-1]` ;do
        color=$[RANDOM%7+31]
        echo -en  "\033[1;${color}m*\033[0m"
    done
    echo
}&
done

shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解
shell脚本forloops题解

shell脚本forloops题解

猜你喜欢

转载自blog.51cto.com/13698281/2114954
今日推荐