day 18

#例题32
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 32.sh
#写一个脚本,实现如下功能:  
#脚本可以带参数也可以不带
#参数可以有多个,每个参数必须是一个目录
#脚本检查参数个数,若等于0,则列出当前目录本身,否则显示每个参数包含的子目录
#!/bin/bash
if [ $# -eq 0 ]
then
    echo "当前目录下的文件是:"
    ls .
else
    for d in $@
    do
        if [ -d $d ]
        then
            echo "目录$d下有这些子目录:"
            find $d -type d 
        else
            echo "并没有该目录:$d"
        fi
    done
fi

 

#例题33
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 33.sh
#定义一个shell函数,能接受两个参数,满足以下要求:  
#第一个参数为URL,即可下载的文件,第二个参数为目录,即下载后保存的位置
如果用户给的目录不存在,则提示用户是否创建,如果创建就继续执行,否则,函数返回一个51的错误值给调用脚本
#如果给的目录存在,则下载文件,下载命令执行结束后测试文件下载成功与否,如果成功,则返回0给调用脚本,否则,返回52给调用脚本
#!/bin/bash
if [ $# -ne 2 ]
then
   echo "你必须要输入两个参数,第一个参数是网址,第二个参数是目录." 
   exit 1
fi

if [ ! -d $2 ]
then
    while :
    do
    echo "你输入的第二个参数,并不是一个存在的目录。是否要创建该目录呢?(y|n): "c
    case $c in 
        y|Y)
            mkdir -p $2
            ;;
        n|N)
            exit 51
            ;;
        *)
            echo "请输入y或者n."
            continue
            ;;
    esac
    done
else
    cd $2
    wget $1 
    if [ $? -eq 0 ]
    then
        exit 0
    else
        echo "下载失败."
        exit 52
    fi
#例题31
#提示用户输入网卡的名字,然后我们用脚本输出网卡的ip,需要考虑下面问题:  
#输入的字符不符合网卡名字规范,怎么应对。
#名字符合规范,但是根本就没有这个网卡又怎么应对。
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# cat 31.sh 
#!/bin/bash
ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/eth.list
while :
do
    eths=`cat /tmp/eth.list |xargs`
    read -p "Please input a if name(The eths is `echo -e "\033[31m$eths\033[0m"`): " eth
    if [ -z "$eth" ]
    then
    echo "Please input a if name."
    continue
    fi
    if ! grep -qw "$eth" /tmp/eth.list
    then
    echo "The if name is error."
    continue
    else
    break
    fi
done

if_ip() 
{
    ip add show dev $1 |grep ' inet ' |awk '{print $2}'|awk -F '/' '{print $1}' >/tmp/$1.txt
    n=`wc -l /tmp/$1.txt|awk '{print $1}'`
    if [ $n -eq 0 ]
    then
    echo "There is no ip address on the eth."
    else
    echo "The ip addreess is:"
        for ip in `cat /tmp/$1.txt`
    do
        echo -e "\033[33m$ip\033[0m"
    done
    fi
}

if_ip $eth
#例题34
#猜数字
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# cat 34.sh 
#!/bin/bash
n=$[$RANDOM%101]
while :
do
    read -p "请输入一个0-100的数字:" n1
    if [ -z "$n1" ]
    then
    echo "必须要输入一个数字。"
    continue
    fi
    n2=`echo $n1 |sed 's/[0-9]//g'`
    if [ -n "$n2" ]
    then
    echo "你输入的数字并不是正整数."
    continue
    else
    if [ $n -gt $n1 ]
    then
        echo "你输入的数字小了,请重试。"
        continue
    elif [ $n -lt $n1 ]
    then
        echo "你输入的数字大了,请重试。"
        continue
    else
        echo "恭喜你,猜对了!"
        break
    fi
    fi
done


#例题35
#写一个shell脚本,能实现如下需求:  
#执行脚本后,提示输入名字(英文的,可以是大小写字母、数字不能有其他特殊符号),然后输出一个随机的0-99之间的数字,脚本并不会退出,继续提示让输入名字
#如果输入相同的名字,输出的数字还是第一次输入该名字时输出的结果
#前面已经输出过的数字,下次不能再出现
#当输入q或者Q时,脚本会退出。
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# cat 35.sh 
#!/bin/bash
f=/tmp/user_number.txt
j_n()
{
    while :
    do
    n=$[RANDOM%100]
    if awk '{print $2}' $f|grep -qw $n
    then
        continue
    else
        break
    fi
    done
}

while :
do
    read -p "Please input a username: " u
    if [ -z "$u" ]
    then
    echo "请输入用户名."
    continue
    fi

    if [ $u == "q" ] || [ $u == "Q" ]
    then
    exit
    fi 

    u1=`echo $u|sed 's/[a-zA-Z0-9]//g'`
    if [ -n "$u1" ]
    then
    echo "你输入的用户名不符合规范,正确的用户名应该是大小写字母和数字的组合"
    continue
    else
    if [ -f $f ]
    then
        u_n=`awk -v uu=$u '$1==uu {print $2}' $f`
        if [ -n "$u_n" ]
        then
        echo "用户$u对应的数字是:$u_n"
        else
        j_n
        echo "用户$u对应的数字是:$n"
            echo "$u $n" >>$f
        fi
    else
        j_n
        echo "用户$u对应的数字是:$n"
        echo $u $n >> $f
    fi
    fi
done

 

posted @ 2019-01-10 23:23  依哈  阅读(189)  评论(0编辑  收藏  举报