Linux——shell企业面试题

12 篇文章 0 订阅

京东
问题1:使用Linux命令查询file1中空行所在的行号。

[root@localhost interview]# awk '$0~/^$/ {print NR}' file1 
2
3
5
[root@localhost interview]# vim file1 

   study is a long time
   
  
   please hold on
   
   there is no more time
    be patient

问题2:有文件chengji.txt内容如下’’
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出。

[root@localhost interview]# awk '{sum += $NF} END{print sum}' chengji.txt 
150

搜狐&和讯网
问题1:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?

[root@localhost interview]# vim souhu.sh
#!/bin/bash
if [[ -f file1 ]];then
        echo "the file is exist!"
else
        echo "the file is not exist!"
fi
~  
[root@localhost interview]# chmod 755 souhu.sh
[root@localhost interview]# ./souhu.sh 
the file is exist!

新浪
问题1:用shell写一个脚本,对文本中无序的一列数字排序

[root@localhost ~]# cat test.txt
9
8
7
6
5
4
3
2
10
1
[root@localhost ~]# sort -n test.txt   //升序排序
1
2
3
4
5
6
7
8
9
10
[root@localhost interview]# sort -nr test.txt     //逆序排序
10
9
8
7
6
5
4
3
2
1

金和网络
问题1:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称

[root@localhost home]# grep -r "shen" /home | cut -d ":" -f1 | uniq -d
/home/fan

小米
问题1:
一个文本文件info.txt的内容如下:
aa,201
zz,502
bb,1
ee,42
每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列。

[root@localhost interview]#  sort -t"," -k 2 -nr info.txt
zz,502
aa,201
ee,42
bb,1

美团
问题1:编写脚本实现以下功能;
每天早上5点开始做备份
要备份的是/var/mylog里所有文件和目录可以压缩进行备份
备份可以保存到别一台器上192.168.1.2 FTP帐号 aaa 密码 bbb
要求每天的备份文件要带有当天的日期标记

[root@localhost ~]vim mylogbak.sh 
#!/bin/bash
bakdir=mylog
date=`date +%F`
cd /var
tar zcf ${bakdir}_${date}.tar.gz ${bakdir}
sleep 1
ftp -n <<- EOF
open 192.168.142.129 #远程ftp服务器IP
user aaa bbb
put mylog_*.tar.gz
bye
EOF
// 添加crontab:
[root@localhost ~]#crontab -l
00 05 * * * /bin/bash /root/mylogbak.sh 

问题2:请用shell脚本创建一个组class、一组用户,用户名为stdX,X从01-30,并归属class组

[root@localhost interview]# vim user.sh
#!/bin/bash
 groupadd class
user=std
for i in {01..30}
do
id -u ${user}$i &>/dev/null || useradd -G class ${user}$i
done
[root@localhost interview]# chmod 755 user.sh 
[root@localhost interview]# ./user.sh 
[root@localhost interview]# id std01
uid=1023(std01) gid=1024(std01) groups=1024(std01),1023(class)

百度
处理以下文件内容,将域名取出并进行计数排序,如处理:
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://www.sina.com.cn/head/www20021123am.shtml
http://www.sina.com.cn/head/www20041223am.shtml

[root@localhost interview]# cat dns.sh
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://www.sina.com.cn/head/www20021123am.shtml
http://www.sina.com.cn/head/www20041223am.shtml
##取出域名
[root@localhost interview]# awk -F / '{print $3}' dns.txt 
www.baidu.com
www.baidu.com
www.baidu.com
hi.baidu.com
www.sina.com.cn
www.sina.com.cn
##进行计数排序
[root@localhost interview]# vim dns.sh

#!/bin/bash
declare -A array
for i in `awk -F / '{print $3}' dns.txt`
do
        let array[$i]++
done
for i in ${!array[*]}
do
        echo "$i ${array[$i]}"
done

[root@localhost interview]#chmod 755 dns.sh
[root@localhost interview]# ./dns.sh
www.sina.com.cn 2
hi.baidu.com 1
www.baidu.com 3

奇虎360
1、写一个脚本查找最后创建时间是3天前,后缀是*.log的文件并删除。

[root@localhost ~]# find / -name “*.log” -ctime +3 -exec rm -f {} \;

2、写一个脚本将某目录下大于100k的文件移动至/tmp下。

[root@localhost ~]# for i in `find /test -type f -size +100k`;do cd /test && mv
$i /tmp;done

3、写一个脚本进行nginx日志统计,得到访问ip最多的前10个(nginx日志路
径:/home/logs/nginx/default/access.log

[root@localhost ~]# awk '{a[$1]++}END{for (j in a) print a[j],j}'
/home/logs/nginx/default/access.log|sort -nr|head

4、写一个脚本把指定文件里的/usr/local替换为别的目录。

[root@localhost ~]# sed 's:/user/local:/tmp:g' filename

滴滴出行
1、指令:ls | grep “[ad]*.conf” 命令解释正确的是:
正确答案: A
A 显示包含a 或者d 为开头,后接任何字符,再后面是.conf字符的文件(或目录)
B 显示包含a 或者d 出现0 次或无数次,后面是.conf字符的文件(或目录)
C 显示包含字母a 或者d出现0次或1次,后面是.conf字符的文件(或目录)
D 显示从字母a 到d ,后接任何字符,再后面是.conf字符的文件(或目录)
2、找出IO重定向执行结果与其他三个不同的:
正确答案: C
A ./run.sh >run.log 2>&1;
B ./run.sh 2>&1 >run.log;
C ./run.sh &>run.log;
D ./run.sh 2>run.log >&2
3、一个文件,大概1亿行,每行一个ip,将出现次数最多的top10输出到一个新的文件中

sort ip.txt | uniq -c | sort -rn | head -n 10 >>IP.txt
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值