SHELL训练营--day18_shell练习31-35

#获取网卡IP
#!/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`
    if [ $n -eq 0 ]
    then
        echo "There is no ip address on the eth."
    else
        echo "the ip address is :"
        #cat /tmp/$1.txt
        for ip in `cat /tmp/$1.txt`
        do
            echo -e "\033[33m $ip \033[0m"
        done
    fi
}

if_ip $eth

# 列出目录内容
#!/bin/bash
if [ $# -eq 0 ]
then
    ls -l .
else
    for d in $@
    do
        if [ -d $d ]
        then 
            echo "$d 下有这些子目录:"  
            find $d -type d
        else
            echo "没有该目录:$d"
        fi
    done
fi

#下载文件
#!/bin/bash
if [ $# -ne 2 ]
then
    echo "需要两个参数,一个网址,一个目录。"
    exit 1
fi

if [ ! -d $2 ]
then
    while:
    do
        read -p "第二个参数,需要是目录。是否需要创建?(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
fi

#猜数字
#!/bin/bash
num=$[$RANDOM%101]
while:
do
    read "please input a number(0-100):" n1
    if [ -z "$n1"]
    then
        echo "你需要输入一个数字。"
        continue
    fi

    n2=`echo $n1|sed 's/[0-9]//g'`
    if [ -n "$2" ]
    then
        echo "请输入一个整数。"
        continue
    fi

    if [ $1 -ge 0 -a $1 -le 100 ]
    then
        if [ $num -gt $n1 ]
        then
            echo "你输入的数字小了。"
            continue
        elif [ $num -lt $n1 ]
        then
            echo "你输入的数字大了。"
            continue
        else
            echo "你输入的数字是 $n1,你答对了。。"
            break
        fi
    fi  
done

#根据名字得数字
#!/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}'`
            if [ -n "$u_n" ]
            then
                echo "用户$u对应的数字是:$u_n"
            else
                j_n
                echo "用户$u对应数字是: $n"
                echo $u $n >> $f
            fi
        else
            n=$[$RANDOM%100]
            echo "用户$u对应数字是: $n"
            echo $u $n >> $f
        fi
    fi
done

猜你喜欢

转载自blog.51cto.com/sincethen/2341341