shell题库选择题_shell学习:几道常见shell习题
1.?編寫shell腳本,計算1-100的和;
1 #! /bin/bash
2 sum=0
3 for i in `seq 1 100`; do
4 sum=$[$i+$sum]5 done6 echo $sum
計算-00和
2.?編寫shell腳本,要求輸入一個數(shù)字,然后計算出從1到輸入數(shù)字的和,要求,如果輸入的數(shù)字小于1,則重新輸入,直到輸入正確的數(shù)字為止;
1 #! /bin/bash
2 n=0
3 while [ $n -lt "1" ]; do
4 read -p "Please input a number, it must greater than"1":"n5 done6
7 sum=0
8 for i in `seq 1 $n`; do
9 sum=$[$i+$sum]10 done11 echo $sum
12
13
判斷輸入數(shù)字
3.?編寫shell腳本,把/root/目錄下的所有目錄(只需要一級)拷貝到/tmp/目錄下;
1 #! /bin/bash
2 for f in `ls /root/`; do
3 if [ -d $f] ; then4 cp -r $f /tmp/
5 fi6 done
拷貝目錄
4.?編寫shell腳本,批量建立用戶user_00, user_01, … ,user_100并且所有用戶同屬于users組;
1 #! /bin/bash
2 groupadd users3 for i in `seq 0 9`; do
4 useradd -g users user_0$i
5 done6
7 for j in `seq 10 100`; do
8 useradd -g users user_$j
9 done
批量建用戶
5.?編寫shell腳本,截取文件test.log中包含關(guān)鍵詞’abc’的行中的第一列(假設(shè)分隔符為”:”),然后把截取的數(shù)字排序(假設(shè)第一列為數(shù)字),然后打印出重復(fù)次數(shù)超過10次的列;
1 #! /bin/bash
2 awk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txt3 sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt4 awk '$1>10 {print $2}' /tmp/n2.txt
截取文本文件中的字符串
6.?編寫shell腳本,判斷輸入的IP是否正確(IP的規(guī)則是,n1.n2.n3.n4,其中1
1 #! /bin/bash
2 checkip()3 {4 if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then5 a=`echo $1 | awk -F. '{print $1}'`6 b=`echo $1 | awk -F. '{print $2}'`7 c=`echo $1 | awk -F. '{print $3}'`8 d=`echo $1 | awk -F. '{print $4}'`9
10 fi11
12 for n in $a $b $c $d; do
13 if [ $n -ge 255 ] || [ $n -le 0]; then14 echo "the number of the IP should less than 255 and greate than 0"
15 return 2
16 else
17 echo "The IP you input is something wrong, the format is like 192.168.100.1"
18 return 1
19 fi20 done21
22 }23
24
25
26 rs=1
27 while [ $rs -gt 0 ]; do
28 read -p "Please input the ip:"ip29 checkip $ip
30 rs=`echo $?`31
32 done33
34 echo "The IP is right!"
35 判斷IP地址
判斷IP地址
7.編寫一個腳本,打印任何數(shù)的乘法表。如輸入3則打印
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
1 awk -vstr='3' 'BEGIN{for(i=1;i<=str;i++){for(p=1;p<=i;p++)printf p"*"i"="p*i"\t";printf "\n"}}'
打印剩法口決
8.編寫一個腳本,輸入自己的生日時間(YYYYMMDD),計算還有多少天多少個小時是自己的生日。
1 read -p "Input your birthday(YYYYmmdd):"date12 m=`date --date="$date1" +%m`3 d=`date --date="$date1" +%d`4 date_now=`date +%s`5 y=`date +%Y`6
7 birth=`date --date="$y$m$d" +%s`8 internal=$(($birth-$date_now))9 if [ "$internal" -lt "0"]; then10 birth=`date --date="$(($y+1))$m$d" +%s`11 internal=$(($birth-$date_now))12 fi13
14 awk -vinternal=$internal 'BEGIN{d=int(internal/60/60/24);h=int((internal-24*60*60*d)/3600);print "There is :"d"days"h"hours."}'
計算時間
9.編寫一個腳本,自動將用戶主目錄下所有小于5KB的文件打包成XX.tar.gz.(提示:用ls,grep,find等命令,文件一般指普通文件)
1 find ~ -size -5 -type f -maxdepth 1|xargs tar zcvpf backup.tar.gz
查找文件
10.編寫一個程序,他的作用是先查看一下/root/test/logical這個名稱是否存在,若不存在,則創(chuàng)建一個文件。使用touch來創(chuàng)建,創(chuàng)建完成后離開;如果存在的話,判斷該名稱是否為文件,若為文件則將之刪除后新建一個目錄。文件名為loglical,之后離開;如果存在的話,而且該名稱為目錄,則刪除此目錄。
1 if [ ! -e "/root/test/logical" ]; then touch "hh"; elif [ -f "/root/test/logical" ];then rm /root/test/logical && mkdir logical&&exit;elif [ -d "/root/test/logical" ];then rm /root/test/logical; fi
判斷文件是否存在
11.導(dǎo)出 2013-05-24 15:00:00 ~ 2013-05-28 16:00:00 之間的apache訪問日志
1 sed -n '/24\/May\/2013:15:00:01/,/28\/May\/2013:16:59:58/p' xxxx-access_log > 20130524.15-20130528.16-access_log
截取指定范轉(zhuǎn)的內(nèi)容
PS:需要注意的是如果起始時間在日志中不存在,則整個截取將返回 0 行結(jié)果。而如果結(jié)束時間在日志中不存在,則會截取到日志的最后一條。所以在截取前得要找到最日志中最合適的起始點和結(jié)束點。
另一種做法是先使用grep去找到兩個點? 再使用sed去截取
1 #找出 2013-05-24 15點第一條記錄的時間[root@style logs]# grep '24/May/2013:15' xxxx-access_log | head -110.200.114.183 - - [24/May/2013:15:00:01 +0800] "GET /gp10/pic_259_218_1368781965.png HTTP/1.0" 401 484# 找出 2013-05-28 16點最后一條記錄的時間[root@style logs]# grep '28/May/2013:16' xxxx-access_log | tail -1222.92.115.195 - - [28/May/2013:16:59:58 +0800] "GET /favicon.ico HTTP/1.1" 404 17846# 然后取這兩個時間段之間的記錄
View Code
總結(jié)
以上是生活随笔為你收集整理的shell题库选择题_shell学习:几道常见shell习题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 字符串 4位一组_Pyth
- 下一篇: arduino使用oled代码_【惊不?