全部博文(39)
分类:
2011-09-22 11:59:55
原文地址:shell 面试题汇集 作者:oychw
磁针石:xurongzhong#gmail.com
#/bin/sh
Max_CPU=0
Avg_CPU=0
Total_Time=1
Process=$1
Interval=$2
# check the parameters
if [ $# -ne 2 ]; then
echo "Usage: $0 ProcessName Interval"
exit
fi
LogFile="Per.txt"
echo "`date`" > $LogFile
while sleep $Interval
do
top -d 1 -n 1|grep $Process|grep -v grep|awk '{print
$9"\t"$10}' >> $LogFile
done
#/bin/bash
echo -e "The program will Judge a file is or not a device file.\n\n"
read -p "Input a filename:" filename
if [ -b $filename -o -c $filename ]; then
echo "$filename is a device file"
exit 0
else
echo "$filename is not a device file"
exit 1
firead –p:用于在读数据时输出提示信息
注意! [ 之间是有空格的:if ! [ -f $filename ] ; then。一般用if [ ! * ]
#/bin/bash
groupadd -f class1
for i in {9909..9911}
do
xx=`echo $i | sed 's/99//g'`
useradd -g class1 std${xx}
echo std${xx} | passwd std${xx} --stdin
echo -e "user std${xx} passwd is std${xx}">>/root/newuser.txt
done
exit 0
注意等号的前后不要有空格:xx=`echo $i | sed 's/99//g'`
变量如果前后有字符,要是大括号
要求分析apache访问日志,找出访问页面数量在前100位的IP数。日志大小在
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)"
# 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。
#/bin/bash
typeset first second
read -p "Input the first number:" first
read -p "Input the second number:" second
result=$[$first+$second]
echo "result is : $result"
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
答案:
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}'
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
答案:join employee bonus | sort -k 2
处理结果:
Swap:
free -m | sed -n '/Swap/p' | awk '{ print $2}'
free -m | sed -n 's/Swap:\ *\([0-9]*\).*/\1/p'
处理结果:
real
user
sys
答案:
# time for i in {1..2000} ; do mkdir /root/neil$i; done
real
user
sys
处理结果:
答案:netstat -anp | grep sshd | sed -n 's/.*:::\([0-9]*\)\ .* \ \([0-9]*\)\/sshd/\1 \2/p'
处理结果:
root's bins: 2306
echo "root's bins: $(find ./ -type f | xargs ls -l | sed '/-..x/p' | wc -l)"
root's bins: 3664
for file in *.c; do echo $file ; gcc -o $(basename $file .c) $file ; sleep 2; done > compile 2>&1
for i in *.*;do mv $i ${i%%.*}.bak;done