shell实例

发布于:2022-12-31 ⋅ 阅读:(649) ⋅ 点赞:(0)

1.使用Linux命令查询file1中空行所在的行号。

[root@r8 ~]# cat file1
hellow world 1
hellow world 2

hellow world 3
hellow world 4

hellow world 5

[root@r8 ~]# awk '/^$/{print NR}' file1
3
6
8


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

[root@r8 ~]# awk '{sum+=$2} END{print sum}' chengji.txt 
150


3.Shell脚本里如何检查一个文件是否存在?

[root@r8 ~]# vim file.sh
#!/bin/bash
if [ -e file1.txt ];then
    echo "exist"
else
    echo " not exist"
fi 


4.用shell写一个脚本,对文本中无序的一列数字排序
9 8 7 6 5 4 3 2 1 0 1

[root@r8 shell_ex]# sort sort.txx 
0
0
1
4
5
6
7
8


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

[root@r8 shell_ex]# grep -r "shen" /home


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

[root@r8 shell_ex]# sort -t "," -k 2 info.txt -r -n
zz,502
aa,201
ee,42
bb,1


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

#!/bin/bash
groupadd class
for i in `seq -f "%02g" 30`
do
    useradd std$i -g class &> /dev/null
done


8.处理以下文件内容,将域名取出并进行计数排序,如处理:
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@r8 shell_ex]# awk -F "/" '{print $3}'  test.txt | sort | uniq -c | sort -nr
      3 www.baidu.com
      2 www.sina.com.cn
      1 hi.baidu.com


9.写一个脚本查找最后创建时间是3天前,后缀是.log的文件并删除

[root@r8 shell_ex]# find / -name "*.log" -ctime +3 -exec rm -f {} \;


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

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

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到