linux's docs

14.4.0: 流程控制-练习题


1. shell练习题

1、编写shell脚本,计算1-100的和
#!/bin/bash

sum=0

for i in `seq 1 100`
do
    sum=$[$i+$sum]
done

echo $sum
2、编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;
#!/bin/bash

sum=0
starter=true

function sum_num() {
    for i in `seq 1 $1`
    do
        sum=$[$sum+$i]
    done
    echo $sum
}

while [ $starter == true ]
do
    read -p "input the number: " number
    if [ -z `echo $number | sed 's/[0-9]//g'` ] && [ $number -ge 1 ]
    then
        sum_num $number
        starter=false
    else
        echo -e "\ninput must be number and greater than 1.\n"
    fi
done
3、编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;
find /root/ -mindepth 1 -maxdepth 1 -type d|xargs -i cp -r {} /tmp/;
echo "===========================================";
ls -l /tmp |grep "^d";
4、编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组;
5、编写shell脚本,截取文件test.log中包含关键词 'abc' 的行中的第一列(假设分隔符为 ":" ),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;

2. 其他shell练习题

1、写一个脚本,执行后,打印一行提示"Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。
a="go"
while [ ! -z $a ]
do
        read -p "Please input a number:" a ;
        if [ "$a" != "end" ];
        then
                echo $a;
        else
                exit 0
        fi
done
2、
## 功能:猜 0-9 范围内的数字
## 要求:1、每次自动生成一个数字
##       2、猜错五次自动退出
##       3、猜对自动退出
##       4、输入非数字不报错
##       5、脚本要实现一天只能运行一次
#还需要多思考,朋友的思路是靠外部文件,是否可以用全局变量呢?
num=`echo $RANDOM|cut -c 1`
a=go
#time1=`date +%d`
#if [ -z $time2 ]
#then
#       time2=0
#fi
#time3=$(($time1-$time2))
#if [ $time3 -lt 1 ]
#then
#       echo "one day one time,no more"
#       exit 0
#fi

for ((i=1;i<=5;i++))
do
        read -p "Guess which number this time : " a
        if [ "$a" = "$num" ];
        then
                echo "Congratulation,you win!"
                exit 0
        else
                echo "Sorry,you lose"
        fi
#       time2=`date +%d`
#       export $time2
done
3、脚本要求:
请系统自动检查普通用户的家目录是否存在;
如存在,输出"该用户名's homedir is exist,very good";
如不存在,输出"该用户名's homedir isn't exist,do you want to create one? y or n"接受用户输入信息;
如果用户输入y,则在/home下创建新的家目录,并从/etc/skel下把所有文件拷贝到家目录下,并修改属组和属主;
如果用户输入n,则输出ok,continue check。;
如果用户输入的不是y和n,提示whats your input is not y or n ,ignore this user
usernum=`awk -F: '$3+0>=500&&$3+0<65534' /etc/passwd|wc -l`
for ((i=0;i<$usernum;i++))
        do
        j=$[$i+1];
        user[j]=`awk -F: '$3+0>=500&&$3+0<65534{print $1}' /etc/passwd |sed -n "$j p"`
        if [ -d /home/${user[j]} ];
        then
                echo "${user[j]}:'s homedir is exist,very good!"|awk -F ":" '{printf("%-8s%-s\n",$1,$2)}'
        else
                 read -p "${user[j]}'s homedir is not exist,do you want to create one?" yn
                 if [ "$yn" == "y" -o "$yn" == "Y" ];
                then
                        mkdir "/home/${user[j]}";
                        echo "/etc/skel/."|xargs -i cp -r {} "/home/${user[j]}";
                        echo "${user[j]}:${user[j]}"|xargs -i chown -R {} "/home/${user[j]}";

                        elif [ "$yn" == "n" -o "$yn" -eq "N" ];
                        then
                                echo "ok,continue check"
                        else
                                echo "whats your input is not y or n ,ignore this user"
                fi
        fi
done
4、
## 功能:自动生成两个文件 ,文件一以空格为分隔符 ,第一列为手机号码 ,第二列为话费金额
##                          文件二以空格为分隔符 ,第一列为手机号码 ,第二列为流量金额

##       其中 ,文件一、二中的手机号码相同、并且一一对应 ,金额不同

## 要求:
## 1、打印出话费金额最大的手机号码跟话费消费金额
## 2、打印出流量金额最大的手机号码跟流量消费金额
## 3、汇总出总话费金额、总流量金额 ,并计算出各平均值
## 4、如果某一手机号码的话费金额超过平均值 ,那么显示 "手机号码 话费金额 平均值"
## 5、如果某一手机号码的流量金额超过平均值 ,那么显示 "手机号码 流量金额 平均值"
## 6、如果某一手机号码的话费金额、流量金额都超过平均值 ,那么显示 "手机号码 恭喜你成为 VIP"
## 7、将 VIP 手机号码的话费金额跟流量金额相加 ,数值最大的手机号码显示 "手机号码 恭喜你成为VIP_P"
nu=50
##create 2 files containing PhoneNumber\phonefee\datafee
for ((i=1;i<=$nu;i++))
do
t1[$i]=`echo $RANDOM$RANDOM$RANDOM$RANDOM|cut -c 1-8`
t2[$i]=`echo $[$RANDOM/100]|cut -c 1-4`
t3[$i]=`echo $[$RANDOM/100]|cut -c 1-4`
phonecost="${t1[$i]} ${t2[$i]}"
datacost="${t1[$i]} ${t3[$i]}"
if [ $i == 1 ];   #at the begining,clear the content in the 2 file and echo new content into it
then
        echo "$phonecost" > /tmp/phonecost;
        echo "$datacost" > /tmp/datacost;
else
        echo "$phonecost" >> /tmp/phonecost;
        echo "$datacost" >> /tmp/datacost;
fi
done
#====================================================

##sumarize phonefee and datafee,then average phonefee and datafee
for ((j=1;j<=$nu;j++))
do
pfsum=$[$pfsum+${t2[j]}];
dfsum=$[$dfsum+${t3[j]}];
done
#===================================================
##display information
echo -e "Below is the Top cost of PhoneFee and DataFee\n============================================"
echo `sort -nr -t " " -k 2 /tmp/phonecost|head -1|awk '{printf("Phone: %-9d PhoneFee: %-5d",$1,$2)}'`
echo `sort -nr -t " " -k 2 /tmp/datacost|head -1|awk '{printf("Phone: %-9d DataFee : %-5d",$1,$2)}'`

pfave=$[$pfsum/$nu];
dfave=$[$dfsum/$nu];
echo -e "\nBelow is the sumary of PhoneFee and DataFee\n============================================"
echo "PhoneFee total: $pfsum"
echo "PhoneFee average: $pfave"
echo "Datafee  total: $dfsum"
echo "DataFee  average: $dfave"
#===================================================
##judge the average and fee itself
echo -e "\nThe PhoneFee below is more than average cost\n==========================================="
for ((k=1;k<=$nu;k++))
do
if [ ${t2[k]} -gt $pfave ];
        then
        grep "\b${t2[k]}\b" /tmp/phonecost|awk '{printf("HP:%-9d Fee:%-9d\n",$1,$2)}'
fi
done
#===================================================
5、
写一个脚本: 计算100以内所有能被3整除的正整数的和
#!/bin/bash
# Program
# 1-100,sum the number is 3's multiple

# Author : Zhao Peiwu
# Date : 19/12/2014
# Version : v1.0
sum=0
for ((i=1;i<=100;i++))
do
        num=$[$i%3]
        if [ "$num" -eq "0" ];
        then
                sum=$[$sum+$i]
        fi
done
echo $sum

Contents