关于Shell的企业面试真题

一、京东

(1)问题1:
使用Linux命令查询file1中空行所在的行号

答案:
[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt 
5

(2)问题2:
有文件chengji.txt内容如下:

张三 40
李四 50
王五 60

使用Linux命令计算第二列的和并输出:

[atguigu@hadoop102 datas]$ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
150

二、搜狐&和讯网

问题:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?

#!/bin/bash

if [ -f file.txt ]; then
	echo "文件存在!"
else
	echo "文件不存在!"
fi

三、新浪

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

[root@CentOS6-2 ~]# cat test.txt
9
8
7
6
5
4
3
2
10
1

[root@CentOS6-2 ~]# sort -n test.txt|awk '{a+=$0;print $0}END{print "SUM="a}'
1
2
3
4
5
6
7
8
9
10
SUM=55

四、金和网络

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

[atguigu@hadoop102 datas]$ grep -r "shen" /home | cut -d ":" -f 1

/home/atguigu/datas/sed.txt
/home/atguigu/datas/cut.txt

猜你喜欢

转载自blog.csdn.net/weixin_43520450/article/details/107641653