Real questions about Shell's corporate interview

1. Jingdong

(1) Question 1:
Use Linux commands to query the line number of the blank line in file1

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

(2) Question 2: The
content of the file chengji.txt is as follows:

Zhang San 40
Li Si 50
Wang Wu 60

Use Linux commands to calculate the sum of the second column and output:

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

2. Sohu & Hexun

Question: How to check whether a file exists in a shell script? What to do if it does not exist?

#!/bin/bash

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

Three, Sina

(1) Question 1:
Use a shell to write a script to sort an unordered list of numbers in the text

[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

4. Jinhe Network

Question: Please use a shell script to find out the file name that contains the character "shen" in all text files in the current folder (/home)

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

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

Guess you like

Origin blog.csdn.net/weixin_43520450/article/details/107641653