SHELL训练营--day18_shell练习31-35
发布时间:2020-12-15 23:03:20 所属栏目:安全 来源:网络整理
导读:#获取网卡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 " 33[31m $eths 33[0m"`)" eth if [ -z "$eth" ] then echo
#获取网卡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 " 33[31m $eths 33[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 " 33[33m $ip 33[0m" done fi } if_ip $eth # 列出目录内容 #!/bin/bash if [ $# -eq 0 ] then ls -l . else for d in [email?protected] 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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |