设为首页 收藏本站
查看: 700|回复: 0

linux shell 面试

[复制链接]

尚未签到

发表于 2018-8-25 06:33:18 | 显示全部楼层 |阅读模式
  1.有一个文件,里面有二列,第一列ip地址,第二列是时间,同一个ip可能出现多次,但时间不同.
  文件类似下面的样子:
  192.168.1.2              13:10
  192.127.12.1            13.11
  192.168.1.2              14:22
  现要求写一脚本,显示出现最多的ip top 10
  awk '{print $1}' file|sort|uniq -c|sort -nr|head -10
  分析:
  只是提取最多的IP,并没有要求包含时间,所以先提取该IP列进行下一步的处理;
  然后利用sort排序,再用uniqu -c 统计次数并显示;
  再用sort -nr 按由高到低的顺序排列,最后利用head 截取前10 排;
[root@localhost ~]# awk '{print $1}' bb.txt | sort -nr |uniq -c |head -10  10 192.168.1.2
  5 192.127.12.1
  2.假设Apache产生的日志文件为access.log,在Apache正在运行的时候,执行命令mv access.log access.bak
  ,执行完毕后,请问新的apache日志会打印到那里?为什么?
  答: 新的日志会打印在access.bak中. 因为apache启动时,会找到access.log文件,随时准备向文件中追
  加日志,虽然此时文件被改名,但是由于服务正在运行,因为它的inode节点的位置没有变,程序打开的
  fd仍然会指向原来的那个inode.不会因为文件名的改变而改变,但若重启服务器之后,系统就会检查
  access.log文件是否存在,不存在,则创建.
  3.在shell环境中,如何查看远程Linux系统运行了多少时间?
[root@localhost ~]# uptime  10:37:40 up 12:50,  2 users,  load average: 0.02, 0.03, 0.00
  
[root@localhost ~]# ssh root@192.168.10.100 uptime | awk '{print $1,$2,$3}'
  
root@192.168.10.100's password:
  
10:37:45 up 12:50,
  4.处理一下文件内容,将域名取出并进行计数排数,如处理:
  http://www.baidu.com/index.html
  http://www.baidu.com/1.html
  http://www.baidu.com/2.html
  http://post.baidu.com/index.html
  http://mp3.baidu.com/index.html
  http://www.baidu.com/3.html
  http://post.baidu.com/2.html
  得到如下结果:域名的出现次数,域名
  4     www.baidu.com
  2     post.baidu.com
  1     mp3.baidu.com
  shell程序如下
[root@localhost ~]# cat aa.txt  
http://www.baidu.com/index.html
  
http://www.baidu.com/1.html
  
http://www.baidu.com/2.html
  
http://post.baidu.com/index.html
  
http://mp3.baidu.com/index.html
  
http://www.baidu.com/3.html
  
http://post.baidu.com/2.html
  
[root@localhost ~]# cat aa.txt |sed 's#http://##g;s#/.*##g'|sort -nr |uniq -c
  4 www.baidu.com
  2 post.baidu.com
  1 mp3.baidu.com
  
[root@localhost ~]# awk -F "/" '{print $3}' aa.txt |sort -nr |uniq -c
  4 www.baidu.com
  2 post.baidu.com
  1 mp3.baidu.com
  
[root@localhost ~]# cat aa.txt |sed -e 's/http:\/\///g ' -e 's/\/.*//g'|sort -nr |uniq -c
  4 www.baidu.com
  2 post.baidu.com
  1 mp3.baidu.com
  写一个脚本
  1.设定变量FILE的值为/etc/passwd
  2.依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么
  形如:(提示:LINE=`wc -l /etc/passwd | cut -d" " -f1`)
  Hello,root,your UID is 0.
  3.统计一共有多少个用户
[root@localhost ~]# cat dd.sh  
#!/bin/bash
  
file=/etc/passwd
  
total_line=`wc -l $file |cut -d " " -f1`
  
while read aa
  
do
  name=`echo "$aa"|awk -F : '{print $1}'`
  uid=`echo "$aa"|awk -F : '{print $3}'`
  echo "Hello,$name,your UID is $uid"
  
done < $file
  echo "total users is $total_line"
  写一个脚本
  1.切换工作目录至/var
  2.依次向/var目录中的每个文件或子目录问好,形如:
  (提示:for FILE in /var/*;或for FILE in `ls /var`;)
  Hello,log
  3.统计/var目录下共有多个文件,并显示出来
[root@localhost ~]# ls /var |wc -l  
25
  
[root@localhost ~]# cat ee.sh
  
#!/bin/bash
  
num=0
  
for a in `ls /var`
  
do
  echo "hello, $a"
  num=$(($num+1))
  
done
  
echo "the var directory has $num files"
  
[root@localhost ~]# bash ee.sh
  
hello, account
  
hello, cache
  
hello, crash
  
....
  
the var directory has 25 files
  写一个脚本
  1.设定变量file的值为/etc/passwd
  2.使用循环读取文件/etc/passwd的第2,4,6,10,13,15行,并显示其内容
  3.把这些行保存至/tmp/mypasswd文件中
[root@localhost ~]# cat ff.sh  
#!/bin/bash
  
file=/etc/passwd
  
for i in 2 4 6 10 13 15
  
do
  line=`head -$i $file|tail -1`
  echo "$line"
  echo "$line" >>/tmp/passwd
  
done
  
[root@localhost ~]# bash ff.sh
  
bin:x:1:1:bin:/bin:/sbin/nologin
  
。。。。
  
[root@localhost ~]# cat /tmp/passwd
  
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  
。。。。
  练习:
  传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商
[root@localhost ~]# cat hh.sh  
#!/bin/bash
  
read -p "the first number: " num1
  
read -p "the second number: " num2
  sum=$[$num1 + $num2]
  sub=$((num1 - $num2))
  let mul=num1*num2
  div=`expr $num1 / $num2`
  
echo "sum=$sum; sub=$sub; mul=$mul; div=$div"
  
[root@localhost ~]# bash hh.sh
  
the first number: 88
  
the second number: 66
  
sum=154; sub=22; mul=5808; div=1
写一个脚本:       1.创建目录/tmp/scripts       2.切换工作目录至此目录中       3.复制/etc/pam.d目录至当前目录,并重命名为test       4.将当前目录的test及其里面的文件和子目录的属主改为redhat       5.将test及其子目录中的文件的其它用户的权限改为没有任何权限#!/bin/bash  
mkdir /tmp/scripts
  
cd /tmp/scripts
  
cp -r /etc/pam.d ./test
  
chown -R redhat test
  
chmod -R o-rwx  test
  
~
写一个脚本       1.显示当前系统日期和时间,而后创建目录/tmp/lstest       2.切换工作目录至/tmp/lstest       3.创建目录a1d,b56e,6test       4.创建空文件xy,x2y,732       5.列出当前目录下以a,x或者6开头的文件或目录       6.列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录#!/bin/bash  
date
  
mkdir /tmp/lstest
  
cd /tmp/lstest
  
mkdir a1d b56e 6test
  
touch xy x2y 732
  
ls [ax6]*
  
ls [a-z][0-9]*
  写一个脚本        添加10个用户user1到user10,但要求只有用户不存在的情况下才能添加
#!/bin/bah  
for i in `seq 1 10`
  
do
  cut -d: -f1 /etc/passwd |grep "$user$i" &>/dev/null || useradd user$i
  
done
  写一个脚本  查看是否有user1到user10用户 如果有就删除
#!/bin/bash  
for i in `seq 1 10`
  
do
  
awk -F: '{print $1}' /etc/passwd |grep "user$i" && userdel -r user$i
  
done
  
~
  写一个脚本       通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线
  如果在线,就显示“ip is up”       如果不在线,就显示“ip is down”
#!/bin/bash  
for i in $(seq 151 254)
  
do
  
ping -c 1 -w 1 192.168.0.$i &>/dev/null && echo "192.168.0.$i is up" || echo "192.168.0.$i is down"
  
trap "exit" sigint # 接收ctrl +c退出
  
done
  统计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
  这个地方有个疑问,为什么在使用uniq之前要sort。
  因为uniqu 只能判别相邻的内容是否一样!
  写一个脚本:输入三个数字,从大到小进行排序
[root@localhost ~]# cat 5.sh  
#!/bin/bash
  
read -p "the first number: " a
  
read -p "the second number: " b
  
read -p "the third number: " c
  
if [ $a -lt $b  ];then
  tmp=$a
  a=$b
  b=$tmp
  
fi
  
if [ $a -lt $c ];then
  tmp=$a
  a=$c
  c=$tmp
  
fi
  
if [ $b -lt $c  ];then
  tmp=$b
  b=$c
  c=$tmp
  
fi
  
echo "$a, $b, $c"
  1.用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。
  参考程序:
  #!/bin/sh
  FILENAME=
  echo “Input file name:”
  read FILENAME
  if [ -c "$FILENAME" ]
  then
  cp $FILENAME /dev
  fi
  2.请下列shell程序加注释,并说明程序的功能和调用方法:#!/bin/sh
  #!/bin/sh
  #
  # /etc/rc.d/rc.httpd
  #
  # Start/stop/restart the Apache web server.
  #
  # To make Apache start automatically at boot, make this
  # file executable: chmod 755 /etc/rc.d/rc.httpd
  #
  case "$1" in
  'start')
  /usr/sbin/apachectl start ;;
  'stop')
  /usr/sbin/apachectl stop ;;
  'restart')
  /usr/sbin/apachectl restart ;;
  *)
  echo "usage $0 start|stop|restart" ;;
  esac
  参考答案:
  (1)程序注释
  #!/bin/sh 定义实用的shell
  #
  # /etc/rc.d/rc.httpd 注释行,凡是以星号开始的行均为注释行。
  #
  # Start/stop/restart the Apache web server.
  #
  # To make Apache start automatically at boot, make this
  # file executable: chmod 755 /etc/rc.d/rc.httpd
  #
  case "$1" in #case结构开始,判断“位置参数”决定执行的操作。本程序携带一个“位置参数”,即$1
  'start') #若位置参数为start
  /usr/sbin/apachectl start ;; #启动httpd进程
  'stop') #若位置参数为stop
  /usr/sbin/apachectl stop ;; #关闭httpd进程
  'restart') #若位置参数为stop
  /usr/sbin/apachectl restart ;; #重新启动httpd进程
  *) #若位置参数不是start、stop或restart时
  echo "usage $0 start|stop|restart" ;; #显示命令提示信息:程序的调用方法
  esac #case结构结束
  (2)程序的功能是启动,停止或重新启动httpd进程
  (3)程序的调用方式有三种:启动,停止和重新启动。
  3.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。
  参考答案:
  #!/bin/sh
  i=1

  groupadd>  while [ $i -le 30 ]
  do
  if [ $i -le 9 ] ;then
  USERNAME=stu0${i}
  else
  USERNAME=stu${i}
  fi
  useradd $USERNAME
  mkdir /home/$USERNAME
  chown -R $USERNAME /home/$USERNAME

  chgrp -R>  i=$(($i+1))
  done
  4.编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。
  参考程序:
  #!/bin/sh
  i=1
  while [ $i -le 50 ]
  do
  userdel -r stud${i}
  i=$(($i+1 ))
  done
  5.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
  (1)在下午4 :50删除/abc目录下的全部子目录和全部文件;
  (2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内;
  (3)每逢星期一下午5:50将/data目录下的所有目录和文件归档并压缩为文件:backup.tar.gz;
  (4)在下午5:55将IDE接口的CD-ROM卸载(假设:CD-ROM的设备名为hdc);
  (5)在早晨8:00前开机后启动。
  参考答案:
  解决方案:
  (1)用vi创建编辑一个名为prgx的crontab文件;
  (2)prgx文件的内容:
  50 16 * * * rm -r /abc/*
  0 8-18/1 * * * cut -f1 /xyz/x1 >;>; /backup/bak01.txt
  50 17 * * * tar zcvf backup.tar.gz /data
  55 17 * * * umount /dev/hdc
  (3)由超级用户登录,用crontab执行 prgx文件中的内容:
  root@xxx:#crontab prgx;在每日早晨8:00之前开机后即可自动启动crontab。
  6.设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。
  参考答案:
  (1)编写shell程序fileback:
  #!/bin/sh
  DIRNAME=`ls /root | grep bak`
  if [ -z "$DIRNAME" ] ; then
  mkdir /root/bak
  cd /root/bak
  fi
  YY=`date +%y`
  MM=`date +%m`
  DD=`date +%d`
  BACKETC=$YY$MM$DD_etc.tar.gz
  tar zcvf $BACKETC /etc
  echo "fileback finished!"
  (2)编写任务定时器:
  echo "0 0 1 * * /bin/sh /usr/bin/fileback" >; /root/etcbakcron
  crontab /root/etcbakcron
  或使用crontab -e 命令添加定时任务:
  0 1 * * * /bin/sh /usr/bin/fileback
  7.有一普通用户想在每周日凌晨零点零分定期备份/user/backup到/tmp目录下,该用户应如何做?
  参考答案:(1)第一种方法:
  用户应使用crontab –e 命令创建crontab文件。格式如下:
  0 0 * * sun cp –r /user/backup /tmp
  (2)第二种方法:
  用户先在自己目录下新建文件file,文件内容如下:
  0 * * sun cp –r /user/backup /tmp
  然后执行 crontab file 使生效。
  8.设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。
  参考答案: 建立程序 Pro16如下:
  #!/bin/sh
  i=1
  while [ i -le 50 ]
  do
  if [ -d /userdata ];then
  mkdir -p /userdata/user$i
  chmod 754 /userdata/user$i
  echo "user$i"
  let "i = i + 1" (或i=$(($i+1))
  else
  mkdir /userdata
  mkdir -p /userdata/user$i
  chmod 754 /userdata/user$i
  echo "user$i"
  let "i = i + 1" (或i=$(($i+1))
  fi
  done



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-556085-1-1.html 上篇帖子: shell 监控磁盘空间并自动报警实例 下篇帖子: shell 笔记一
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表