Linux shell interview questions (1)

Fog dispels eyesight 2022-06-24 07:48:35 阅读数:406

linuxshellinterviewquestions

Count the number of lines in the file
Title Description :
Write a bash Script to output a text file nowcoder.txt The number of lines in
cat nowcoder.txt | wc -l

Print the last of the file 5 That's ok
Title Description :
Often check the log , Will look forward from the end of the file , So please write a bash Script to output a text file nowcoder.txt Last of all 5 That's ok
tail -n 5 nowcoder.txt

Output 7 Multiple
Title Description :
Write a bash Script to output numbers 0 To 500 in 7 Multiple (0 7 14 21…) The order of
#!/bin/bash
# Output number 0 To 500 in 7 Multiple (0 7 14 21…) The order of
index=0
result={}
for (( i=0; i <= 500; i++ ))
do
if (( i t h e n r e s u l t [ i % 7 == 0)) then result[ ithenresult[index]= i i n d e x = i index= iindex=index+1
fi
done
echo ${result[*]}
perhaps
seq 0 500 | awk ‘$0%7==0 {print $0}’

Extract numbers from words
for example
nowcoder.txt The contents of the document are as follows
a12b8
10ccc
2521abc
9asf

cat nowcoder.txt | tr -c [0-9] “\n” | sed ‘/^$/d’ | tr “\n” " "
Output :
12 8 10 2521 9

https://www.jb51.net/article/135168.htm

copyright:author[Fog dispels eyesight],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240252197805.html