Shell常见面试题目

一 利用top取某个进程的CPU的脚本 php

  1. #/bin/sh  
  2.   
  3. Max_CPU=0  
  4. Avg_CPU=0  
  5. Total_Time=1  
  6.   
  7. Process=$1  
  8. Interval=$2  
  9.   
  10. # check the parameters  
  11. if [ $# -ne 2 ]; then  
  12.     echo “Usage: $0 ProcessName Interval”  
  13.     exit  
  14. fi  
  15.   
  16. LogFile=”Per.txt”  
  17. echo “`date`” > $LogFile  
  18.   
  19. while sleep $Interval  
  20. do  
  21.     top -d 1 -n 1 | grep $Process | grep -v grep | awk ‘{print $9″\t”$10}’ >> $LogFile  
  22. done  

二 判断是不是设备文件
[plain] view plain copy
  1. #/bin/bash  
  2. echo -e “The program will Judge a file is or not a device file.\n\n”  
  3. read -p “Input a filename:” filename  
  4. if [ -b $filename -o -c $filename ]; then  
  5. echo “$filename is a device file”  
  6. exit 0  
  7. else  
  8. echo “$filename is not a device file”  
  9. exit 1  
firead –p:用于在读数据时输出提示信息

注意! [ 之间是有空格的:if ! [ -f $filename ] ; then。通常用if [ ! * ]

三 添加用户:
[plain] view plain copy
  1. #/bin/bash  
  2.   
  3. groupadd -f class1  
  4. for i in {9909..9911}  
  5. do  
  6.     xx=`echo $i | sed ‘s/99//g’`  
  7.     useradd -g class1 std${xx}  
  8.     echo std${xx} | passwd std${xx} –stdin  
  9.     echo -e “user std${xx} passwd is std${xx}”>>/root/newuser.txt  
  10. done  
  11. exit 0  
注意等号的先后不要有空格:xx=`echo $i | sed ‘s/99//g’`
变量若是先后有字符,要是大括号

四  统计IP访问:
要求分析apache访问日志,找出访问页面数量在前100位的IP数。日志大小在78M左右。如下是apache的访问日志节选 shell

202.101.129.218 – - [26/Mar/2006:23:59:55 +0800] “GET /online/stat_inst.php?pid=d065 HTTP/1.1″ 302 20-”-” “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” apache

[plain] view plain copy
  1. # awk ‘{print $1}’ log | sort | uniq -c | sort -r | head -n10  
5 221.224.78.15
3 221.233.19.137
1 58.63.148.135
1 222.90.66.142
1 222.218.90.239
1 222.182.95.155
1 221.7.249.206
1 221.237.232.191
1 221.235.61.109
1 219.129.183.122
这个地方有个疑问,为何在使用uniq以前要sort。

五 求2个数之和 bash

[plain] view plain copy
  1. #/bin/bash  
  2. typeset first second  
  3. read -p “Input the first number:” first  
  4. read -p “Input the second number:” second  
  5. result=$[$first+$second]  
  6. echo “result is : $result”  
  7. exit 0  
六 文本分析
取出password中shell出现的次数

第一种方法结果:
4 /bin/bash
1 /bin/sync
1 /sbin/halt
31 /sbin/nologin
1 /sbin/shutdown
第二种方法结果:
/bin/sync 1
/bin/bash 1
/sbin/nologin 30
/sbin/halt 1
/sbin/shutdown 1

答案: ssh

cat /etc/passwd|awk -F: ‘{if ($7!=”") print $7}’|sort|uniq –c
cat /etc/passwd|awk -F: ‘{if ($7!=”") print $7}’|sort|uniq -c | awk ‘{print $2,$1}’ spa

七 文件整理
employee文件中记录了工号和姓名
employee.txt:
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
bonus文件中记录工号和工资
bonus.txt:
100 $5,000
200 $500
300 $3,000
400 $1,250
要求把两个文件合并并输出以下
处理结果:
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000 .net

答案:join employee bonus | sort -k 2 日志

八 打印本机的交换分区大小
处理结果:
Swap:1024M blog

free -m | sed -n ‘/Swap/p’ | awk ‘{ print $2}’
free -m | sed -n ‘s/Swap:\ *\([0-9]*\).*/\1/p’ 进程

九 输出本机建立20000个目录所用的时间
处理结果:
real 0m3.367s
user 0m0.066s
sys 0m1.925s
答案:
# time for i in {1..2000} ; do mkdir /root/neil$i; done
real 0m6.200s
user 0m1.128s
sys 0m4.710s
十 打印当前sshd的端口和进程id
处理结果:
sshd Port&&pid: 22 5412
答案:netstat -anp | grep sshd | sed -n ‘s/.*:::\([0-9]*\)\ .* \ \([0-9]*\)\/sshd/\1 \2/p’

十一 打印root能够使用可执行文件数

处理结果:
root’s bins: 2306
echo “root’s bins: $(find ./ -type f  |  xargs ls -l  |  sed ‘/-..x/p’  | wc -l)”
root’s bins: 3664

十二 编译当前目录下的全部.c文件:

[plain] view plain copy
  1. #!/bin/bash  
  2.   
  3. for file in *.c;   
  4. do echo $file ;   
  5. gcc -o $(basename $file .c) $file ;   
  6. sleep 2;   
  7. done  > compile 2 > &1  

十三 将一目录下全部的文件的扩展名改成bak

[plain] view plain copy
  1. #!/bin/bash  
  2. for i in *.*;  
  3. do   
  4. mv $i ${i%%.*}.bak;  
  5. done 
相关文章
相关标签/搜索