老师讲的所有实验记录
1.写一个脚本,判断用户是否存在,如果存在则删除。若不存在,就提示不存在。 2.三个数字比大小,输出最大的 3.三个数字比大小,并且按从大到小排列 4.画斜线正反 5.达到如下效果 * *** ***** ******* ********* 6.写一个9*9乘法表 7.画一个平行四边形 8.连乘算法 while和until 9.要求根据userlist创建用户,要求指定用户名,用户id,用户的附加组及变更用户u密码,若对应用户的附加组不存在,则将附加组创建出来后再根据要求添加用户。 userlist文件的格式如下: carol 777 tom uplooking natasha 778 tom uplooking r1 779 tom uplooking 10.要求根据userlist创建用户,要求指定用户名,用户id,用户的默认组和附加组及变更用户u密码,若对应用户的附加组不存在,则将附加组创建出来后再根据要求添加用户。 [[email protected] ~]# cat /tmp/useraddlist1 dabao 888 xuexi,it uplooking lucy 889 sales,it uplooking lily 899 pro,aa uplooking 11.case 12.要求输出100以下所有能够被7整除,但不能够被5整除的数字。 13.位置参数,判断文件是否存在 14.函数返回值 15.执行如下命令,达到如下效果 [[email protected] scripts]# bash hs.sh -t 9 * *** ***** ******* ********* *********** ************* *************** ***************** [[email protected] scripts]# bash hs.sh -l 4 ^ ^ ^ ^ 16.取出num.list每一行最大的数字。 [[email protected] tmp]# cat num.list 1 4 3 2 10 7 8 9 11 99 23 16 17.位置参数添加用户 18.针对/usr/share/dict/words文件做过滤 1)列出文件中包含 先有字母t,然后中间有一个元音字母,之后是sh的单词; 2)列出文件中包含 先有字母t,然后中间有若干个元音字母,之后是sh的单词; 3)列出文件中刚好包含16个字母的单词。 19.将/etc/fstab文件复制到/tmp目录,针对/tmp/fstab文件做操作: 1)打印1-3行 2)打印该文件第4行到最后一行 3)将包含mapper字符串的行删除 4)将/etc/hosts文件的内容追加到文件的第3行后和最后一行后面 5)将文件行首的#号代替成空格 取服务器公共目录下下载/content/ule/shellscripts/text.txt文件,针对以下做操作: 1)将包含kevin或Kevin字符串的行打印出来 2)将该文件包含kevin字符串的行中,数字1替换成数字2,数字3替换成数字4 3)将给文件的1-3行删除,并把mandy字符串替换成hellomandy 4) 在该文件kevin字符串的行上边添加hello行 20.将工资单进行统计,输出每个人的工资总和,并格式化输出。 21.access.log 文件中,访问apche的时间在3点到4点之间访问最的ip地址 22.检测输入的ip地址是否合法 习题:写一个脚本,判断用户是否存在,如果存在则删除。若不存在,就提示不存在。 #!/bin/bash #userdel #dabao 2016.01.13 read -p "Plz input username:" user if grep ${user} /etc/passwd &> /dev/null then userdel -r ${user} echo "User ${user} has been deleted!" else echo "no ${user}! " fi 习题:三个数字比大小 #!/bin/bash read -p "plz input num1:" num1 read -p "plz input num2:" num2 read -p "plz input num3:" num3 declare -i l declare -i num1 declare -i num2 declare -i num3 if [ ${num1} -gt ${num2} ] then l=${num1} else l=${num2} fi if [ ${l} -gt ${num3} ] then echo "最大为: ${l}" else l=${num3} echo "最大为: ${l}" fi 习题:三个数字比大小,并且按从大到小排列 #!/bin/bash read -p "plz input num1:" num1 read -p "plz input num2:" num2 read -p "plz input num3:" num3 declare -i l declare -i m declare -i n declare -i num1 declare -i num2 declare -i num3 if [ ${num1} -gt ${num2} ] then l=${num1} m=${num2} else l=${num2} m=${num1} fi if [ ${l} -gt ${num3} ] then if [ ${m} -gt ${num3} ] then m=${m} n=${num3} else n=${m} m=${num3} fi else n=${m} m=${l} l=${num3} fi echo "数字从大到小排列为:${l} ${m} ${n}" 习题:画斜线[[email protected] scripts]# bash seq.sh input number10 & & & & & & & & & & #!/bin/bash read -p "input number" num for i in $(seq $num -1 1) do for x in $(seq $i -1 1) do echo -n " " done echo -n "&" done 习题:达到如下效果 [[email protected] scripts]# bash seq2.sh input number:5 * *** ***** ******* ********* #!/bin/bash read -p "input number:" num for i in $(seq 1 ${num}) do for x in $(seq 1 $((${num}-${i}))) do echo -n " " done for t in $(seq 1 $((1+($i-1)*2))) do echo -n "*" done echo done 习题:[[email protected] scripts]# bash my.sh * *** ***** ******* *** *** *** *** *** #!/bin/bash #my test for i in $(seq 1 4) do for x in $(seq 1 $((4-$i))) do echo -n " " done for t in $(seq 1 $((1+($i-1)*2))) do echo -n "*" done echo done for i in $(seq 1 5) do echo " ***" done 作业: 1.写一个9*9乘法表 #!/bin/bash #99乘法表 #dabao 2016.01.13 for i in $(seq 1 9) do for b in $(seq 1 ${i}) do echo -n "${i}*${b}=$((${i}*${b})) " done echo done 2.画一个平行四边形 [[email protected] scripts]# bash px.sh ********** * * * * * * * * * * ********** [[email protected] scripts]# vim px.sh [[email protected] scripts]# cat px.sh #!/bin/bash #显示一个平行四边形 高为多少行,宽为多少位 #dabao 2016.01.13 read -p "请输入平行四边形高为多少行:" hang read -p "请输入平行四边形宽为多少位:" wei for i in 1 do for t in $(seq 1 $hang) do echo -n " " done for t in $(seq 1 $wei) do echo -n "*" done echo done for i in $(seq 1 $(($hang-2))) do for t in $(seq 1 $(($hang-$i))) do echo -n " " done echo -n "*" for t in $(seq 1 $(($wei-2))) do echo -n " " done echo -n "*" echo done echo -n " " for t in $(seq 1 $wei) do echo -n "*" done echo done 实验:连乘算法 #!/bin/bash #用while达到连乘的功能 #dabao 2016.01.14 i=1 c=1 read -p "连乘算法,请输入:" s while [ ${i} -le ${s} ] do c=$((${i}*${c})) i=$((${i}+1)) done echo "$c" echo 9.要求根据userlist创建用户,要求指定用户名,用户id,用户的附加组及变更用户u密码,若对应用户的附加组不存在,则将附加组创建出来后再根据要求添加用户。 userlist文件的格式如下: carol 777 tom uplooking natasha 778 tom uplooking r1 779 tom uplooking [[email protected] scripts]# cat /scripts/useraddauto.sh #!/bin/bash #根据/tmp/userlist文件来创建用户,设置uid,附加组,密码 #dabao 2016.01.14 while read username uid group passwd do if grep ${group} /etc/group &> /dev/null then useradd ${username} -u ${uid} -G ${group} echo ${passwd} | passwd --stdin ${username} else groupadd ${group} useradd ${username} -u ${uid} -G ${group} echo ${passwd} | passwd --stdin ${username} fi done < /tmp/useraddlist [[email protected] scripts]# cat /tmp/useraddlist r1 777 tom uplooking r2 788 tom uplooking r3 799 carol uplooking [[email protected] scripts]# id r1 uid=777(r1) gid=777(r1) groups=777(r1),503(tom) [[email protected] scripts]# id r2 uid=788(r2) gid=788(r2) groups=788(r2),503(tom) [[email protected] scripts]# id r3 uid=799(r3) gid=799(r3) groups=799(r3),789(carol) 10.要求根据userlist创建用户,要求指定用户名,用户id,用户的默认组和附加组及变更用户u密码,若对应用户的附加组不存在,则将附加组创建出来后再根据要求添加用户。 [[email protected] ~]# cat /tmp/useraddlist1 dabao 888 xuexi,it uplooking lucy 889 sales,it uplooking lily 899 pro,aa uplooking #!/bin/bash #根据/tmp/userlist1文件来创建用户,设置uid,附加组,密码 #附加组的格式位 t1,t2 #dabao 2016.01.14 while read username uid group passwd do for i in $(echo ${group} | tr "," " ") do if [ ${i} = $(cut -d":" -f1 /etc/group|grep ${i} ) ];then echo "${i}已存在;";else echo "${i}不存在,需要执行新建用户组操作" groupadd ${i} done useradd ${username} -u ${uid} -g ${group%,*} -G ${group} echo ${passwd} | passwd --stdin ${username} done < /tmp/useraddlist1 不需要判定组是否存在 #!/bin/bash #根据/tmp/userlist1文件来创建用户,设置uid,附加组,密码 #附加组的格式位 t1,t2 #dabao 2016.01.14 while read username uid group passwd do for i in $(echo ${group} | tr "," " ") do groupadd ${i} done useradd ${username} -u ${uid} -g ${group%,*} -G ${group} echo ${passwd} | passwd --stdin ${username} done < /tmp/useraddlist1 11.case #!/bin/bash #case #dabao 2016.01.14 echo "input a : ls -a /tmp/" echo "input l : ls -l /tmp/" echo "input end":end this program until [ ${str} = end ] do read -p "input string:" str case $str in a) ls -a /tmp/ ;; l) ls -l /tmp/ ;; *) echo "input a : ls -a /tmp/" echo "input l : ls -l /tmp/" echo "input end":end this program esac done 12.要求输出100以下所有能够被7整除,但不能够被5整除的数字。 #!/bin/bash #输出100以下所有能够被7整除,但不能够被5整除的数字。 #dabao 2016.01.14 for i in {1..100} do if [ $(($i%7)) = 0 ] && [ $(($i%5)) != 0 ] then echo "$i" else continue fi done ~ 13.位置参数,判断文件是否存在 #!/bin/bash while [ $# -gt 0 ] do if [ -e $1 ] then echo "$1 exits" else echo "$1 not exits" fi shift done [[email protected] scripts]# bash weizhi1.sh /bin /etc /sbin /home /lll /tmp /bin exits /etc exits /sbin exits /home exits /lll not exits /tmp exits 14.函数返回值 #!/bin/bash LOVE_SHELL () { case $1 in yes) return 0;; no) return 1;; *) return 1;; esac } #for i in {1..99} #do read -p "Do you like shell? yes or no:" str if LOVE_SHELL "${str}" then echo "Yes, I like shell!" else echo "No, I don‘t like shell!" fi #done 15.执行如下命令,达到如下效果 [[email protected] scripts]# bash hs.sh -t 9 * *** ***** ******* ********* *********** ************* *************** ***************** [[email protected] scripts]# bash hs.sh -l 4 ^ ^ ^ ^ [[email protected] scripts]# cat hs.sh #!/bin/bash #输入-t 5 代表画三角形5行 # -l 7 代表的是画斜线7行 #dabao 2016.01.18 #定义函数XX()画斜线 XX () { for i in $(seq 1 $2) do for x in $(seq 1 $i) do echo -n " " done echo -n "^" echo done } #定义函数SJX()三角形 SJX () { for i in $(seq 1 $2) do for x in $(seq 1 $(($2-${i}))) do echo -n " " done for t in $(seq 1 $((1+($i-1)*2))) do echo -n "*" done echo done } case $1 in -t) SJX "$1" "$2";; -l) XX "$1" "$2";; *) echo "提示符输入错误,位置参数第一位请输入-t或者-l,位置参数第二位请输入数字!" esac 16.取出num.list每一行最大的数字。 [[email protected] tmp]# cat num.list 1 4 3 2 10 7 8 9 11 99 23 16 [[email protected] scripts]# bash a2.sh 4 10 99 [[email protected] scripts]# cat a2.sh #!/bin/bash #取出num.list文件中每一行最大的数字 while read LINE do p=0 A=($LINE) for i in $(seq 0 3) do if [ ${A[i]} -gt $p ] then p=${A[i]} else p=$p fi done echo $p done < /tmp/num.list 17.位置参数添加用户 [[email protected] scripts]# cat useradd.sh #!/bin/bash #useradd.sh /tmp/kk if [ $# -eq 0 ] then echo "Usage:/tmp/kk" exit elif [ $1 != /tmp/kk ] then echo "File error!" exit elif [ ! -f $1 ] then echo "File not exist." exit else while read user do useradd -s /bin/false $user echo user | passwd --stdin $user done < /tmp/kk fi 18.针对/usr/share/dict/words文件做过滤 1)列出文件中包含 先有字母t,然后中间有一个元音字母,之后是sh的单词; grep ‘^t[a-zA-Z]sh‘ /usr/share/dict/words 2)列出文件中包含 先有字母t,然后中间有若干个元音字母,之后是sh的单词; grep ‘^t[a-zA-Z]\+sh‘ /usr/share/dict/words 3)列出文件中刚好包含16个字母的单词。 grep -E ‘^[a-zA-Z0-9]{16}$‘ /usr/share/dict/words grep ‘^[a-zA-Z0-9]\{16\}$‘ /usr/share/dict/words 19.将/etc/fstab文件复制到/tmp目录,针对/tmp/fstab文件做操作: 1)打印1-3行 [[email protected] ~]# sed -n ‘1,3p‘ /tmp/fstab 2)打印该文件第4行到最后一行 [[email protected] ~]# sed -n ‘4,$p‘ /tmp/fstab 3)将包含mapper字符串的行删除 [[email protected] ~]# sed -i ‘/mapper/d‘ /tmp/fstab 4)将/etc/hosts文件的内容追加到文件的第3行后和最后一行后面 sed -i ‘3r /etc/hosts‘ /tmp/fstab sed -i ‘$r /etc/hosts‘ /tmp/fstab [[email protected] ~]# sed -i -e ‘$r /etc/hosts‘ -e ‘3r /etc/hosts‘ /tmp/fstab 5)将文件行首的#号代替成空格 [[email protected] ~]# sed -i ‘s*^#* *g‘ /tmp/fstab 取服务器公共目录下下载/content/ule/shellscripts/text.txt文件,针对以下做操作:3 1)将包含kevin或Kevin字符串的行打印出来 [[email protected] ~]# sed -n ‘/[kK]evin/p‘ /tmp/test.txt 2)将该文件包含kevin字符串的行中,数字1替换成数字2,数字3替换成数字4 [[email protected] ~]# sed -i ‘/kevin/{s/1/2/;s/3/4/}‘ /tmp/test.txt 3)将给文件的1-3行删除,并把mandy字符串替换成hellomandy [[email protected] ~]# sed -i -e ‘1,3d‘ -e ‘s/mandy/hellomandy/g‘ /tmp/test.txt [[email protected] ~]# sed -i -e ‘1,3d‘ -e ‘s/mandy/hello&/g‘ /tmp/test.txt 4) 在该文件kevin字符串的行上边添加hello行 [[email protected] ~]# sed -i ‘/kevin/ihello‘ /tmp/test.txt 20.将工资单进行统计,输出每个人的工资总和,并格式化输出。 [[email protected] ~]# cat pay.txt Name 1st 2nd 3th VBird 23000 24000 25000 DMTsai 21000 20000 23000 Bird2 43000 42000 4100 [[email protected] ~]#awk ‘NR==1{printf"%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"total"};NR>=2{total=$2+$3+$4;printf"%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}‘ pay.txt Name 1st 2nd 3th total VBird 23000 24000 25000 72000.00 DMTsai 21000 20000 23000 64000.00 Bird2 43000 42000 4100 89100.00 21.access.log 文件中,访问apche的时间在3点到4点之间访问最的ip地址 1.截取3-4点间的文件 grep ‘2016:0[34]:‘ /var/log/httpd/access_log 2.取第一段 ip cut -d" " -f1 awk ‘{print $1}‘ 3.输出每个ip的重复次数 uniq -c 4.从大到小排序 sort -n -r 4.取前3名 head -3 grep ‘2016:0[34]:‘ /var/log/httpd/access_log | cut -d" " -f1|uniq -c |sort -n -r|head -3 22.检测输入的ip地址是否合法
时间: 2024-08-27 03:00:15