SHELL的案例

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

[root@localhost day9]# cat -n file1 
     1	
     2	hello
     3	
     4	world
     5	china
     6	
     7	chongqing
     8	
     9	
    10	
    11	chengduo
[root@localhost day9]# awk '/^$/{print NR}' file1 
1
3
6
8
9
10
[root@localhost day9]# 

2.有文件chengji.txt内容如下’’

张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出。

[root@localhost day9]# cat chengji.txt 
张三 40
李四 50
王五 60
[root@localhost day9]# awk  '{num+=$2} END{print num}' chengji.txt 
150
[root@localhost day9]#

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

[root@localhost day9]# vim file_exist.sh
#!/bin/bash
read -p "please input filename:" file
a=`find $file >& /dev/null `
if [ $? -eq 0  ]
    then
        echo "file exist"
    else
        echo "file not exist"
fi
[root@localhost day9]# bash file_exist.sh 
please input filename:file1     
file exist
[root@localhost day9]# bash file_exist.sh 
please input filename:file2
file not exist
[root@localhost day9]# 

4.用shell写一个脚本,对文本中无序的一列数字排序

[root@localhost day9]# cat test4.txt 
1
4
6
2
7
3
10
0
9
8
[root@localhost day9]# vim sort.sh
#!/bin/bash
read -p "please input filename:" file
sort -n $file
[root@localhost day9]# bash sort.sh 
please input filename:test4.txt
0
1
2
3
4
6
7
8
9
10
[root@localhost day9]# 

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

[root@localhost day9]# vim file_shen.sh
#!/bin/bash
grep "shen" /home -R | awk  -F : '{print $1}'| sort | uniq -c
grep -r "shen" /home -l
[root@localhost day9]# bash file_shen.sh 
[root@localhost day9]# 

6.一个文本文件info.txt的内容如下:

aa,201
zz,502
bb,1
ee,42
每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列。

[root@localhost day9]# cat info.txt
aa,201
zz,502
bb,1
ee,42
[root@localhost day9]# awk -F , '{print $2}' info.txt  | sort -nr
502
201
42
1
[root@localhost day9]# 

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

[root@localhost day9]# vim class.sh
#!/bin/bash
groupadd class &>/dev/null
for i in `seq 10`
do
    if [ $i -lt 10 ];then
        useradd std$i &>/dev/null
    fi
done
[root@localhost day9]# bash class.sh 

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@localhost day9]# cat baidu_test.txt 
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 day9]# awk -F '/' '{print $3}' baidu_test.txt | sort | uniq -c | sort -nr
      3 www.baidu.com
      2 www.sina.com.cn
      1 hi.baidu.com
[root@localhost day9]#

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

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

ctime:change time 元数据改变时间
btime: birth time 创建时间
注意:此题说的是创建时间,应该使用btime,但是btime在find里面不支持,即用ctime
+3 : 代表三天以前
-3 : 代表三天以后
exec : 执行程序不产生新的进程
{} : 类似于循环里面的临时变量
; : exec…; 固定搭配

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

#!/bin/bash
for FileName in `ls -l  /test | awk '$5>102400' {print $9}`
do
        mv $FileName  /tmp/
done
ls -al  /tmp/
echo  "done!"

11.写一个脚本进行nginx日志统计,得到访问ip最多的前10个

[root@localhost day9]# cat /var/log/nginx/access.log |awk -F ' ' '{print $1}' |sort |uniq -c |sort -nr |head -n 10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值