shell 面试题汇集

news/2024/7/4 3:03:01 标签: 面试, Bash, C#, C++, C

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

 

st1\:*{behavior:url(#ieooui) } <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

2009-8-21

磁针石: xurongzhong#gmail.com

博客: oychw.cublog.cn



#/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'`

       变量如果前后有字符,要是大括号

 

统计 IP 访问:

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

 

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

 

2 个数之和

#/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

 

 

文本分析

取出 passwordshell 出现的次数
第一种方法结果 :
      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:1024M

 

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 文件:

 

for file in *.c; do echo $file ; gcc -o $(basename $file .c) $file  ; sleep 2;  done > compile 2>&1

 

将一目录下所有的文件的扩展名改为 bak

for i in *.*;do mv $i ${i%%.*}.bak;done


http://www.niftyadmin.cn/n/664541.html

相关文章

Linq分页查询

//Linq分页查询 int pageIndex Convert.ToInt32(HttpContext.Current.Request["PageIndex"]); int start pageIndex * Parameter.pageSize; int end (pageIndex 1) * Parameter.pageSize; userList userList.Take<UserInfo>(end).Skip<UserInfo>(st…

Stagefright框架解读(—)音视频Playback流程

转载请注明出处&#xff1a;http://blog.csdn.net/itachi85/article/details/7216639 从Android 2.0&#xff0c;Google引进了Stagefright&#xff0c;并在android2.3时用Stagefright在Android中是以shared library的形式存在(libstagefright.so)&#xff0c;其中AwesomePlayer…

面试-双向链表

面试遇到一个题目&#xff0c;写一个双向链表&#xff0c;包括添加&#xff0c;删除&#xff0c;查找和遍历。当时写了一塌糊涂&#xff0c;后来自己都觉得想笑&#xff0c;双向写着写着被我写成了单向不像单向&#xff0c;双向不像双向了&#xff0c;真是不伦不类。之后 我把这…

Java虚拟机(一)结构原理与运行时数据区域

前言 本来计划要写Android内存优化的&#xff0c;觉得有必要在此之前介绍一下Java虚拟机的相关知识&#xff0c;Java虚拟机也并不是三言两语能够介绍完的&#xff0c;因此开了Java虚拟机系列&#xff0c;这一篇文章我们来学习Java虚拟机的结构原理与运行时数据区域。 1.Java虚拟…

[美食]台湾夜市空降上海 33元吃到饱

正宗的台湾夜市小吃搬到上海咯&#xff01;一家来自台 湾的自助式餐厅近日在上海长宁区延安西路近虹许路开张。大肠包小肠、蚵仔煎、牛肉面、担仔面、天妇罗、生炒花枝、台湾刨冰、台湾水果……食客在该店试营业 期间只需要花33元&#xff08;下午茶/宵夜价格&#xff09;&…

【codeforces 733F】 Drivers Dissatisfaction

http://codeforces.com/problemset/problem/733/F (题目链接) 题意 给出一张n个点的无向图&#xff0c;每一条变有两个特征值&#xff1a;${w&#xff0c;c}$&#xff1b;分别表示这条边的权值为${w}$&#xff0c;每将这条边的权值减小1需要充${c}$元钱。初始时有${S}$元钱&…

Java虚拟机(二)对象的创建与OOP-Klass模型

相关文章 Java虚拟机系列 前言 在前一篇文章中我们学习了Java虚拟机的结构原理与运行时数据区域&#xff0c;那么我们大概知道了Java虚拟机的内存的概况&#xff0c;那么内存中的数据是如何创建和访问的呢&#xff1f;这篇文章会给你答案。 1.对象的创建 对象的创建通常是通过…

IIS http 错误 401.3 - unauthorized

iis http 错误 401.3 - unauthorized 向物理目录添加iis_iusrs用户权限。转载于:https://www.cnblogs.com/zhanqun/p/5508954.html