linux shell 脚本判断某个文件是否有某个字符串以及统计含有某个字符串的个数然后做if else大于小于等于逻辑判断


简介:本脚本主要用来学习判断某个文件是否有某个字符串以及统计含有某个字符串的个数,然后做if else大于小于等于逻辑判断去执行对应的操作


一、测试脚本search_string.sh:

#!/bin/bash


echo 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印"yes messycode",反之打印“no messycode”
grep -w "messycode" compile_error_hint.c && echo "yes messycode"||echo "no messycode"

echo -e "\n"

echo 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,如果有打印"yes tanghanyue",反之打印“no tanghanyue”
grep -w "tanghanyue" compile_error_hint.c && echo "yes tanghanyue"||echo "no tanghanyue"
echo -e "\n"

echo 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数
awk -v RS='\0' -F'tanghanyue' '{print NF-1}' compile_error_hint.c
echo -e "\n"

echo 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数
grep -rn "tanghanyue" compile_error_hint.c | wc -l
echo -e "\n"

echo 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,并把结果保存到number.txt文件里面
grep -rn "tanghanyue" compile_error_hint.c | wc -l >number.txt
echo -e "\n"

echo 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印"yes messycode",反之打印“no messycode”
if grep -q "tanghanyue" compile_error_hint.c
then 
echo "yes tanghanyue"
else
echo "no tanghanyue"
fi
echo -e "\n"

echo 搜索compile_error_hint.c 文件里面是否包含“weifanghai”字符串,如果有打印"yes weifanghai",反之打印“no weifanghai”
grep -q "weifanghai" compile_error_hint.c
if [ $? -eq 0 ];then
    echo "yes weifanghai"
	else  
	echo "no weifanghai"
fi
echo -e "\n"

echo 搜索compile_error_hint.c里面包含"tanghanyue"字符串的个数,并打印出来。
number=$(grep -rn "tanghanyue" compile_error_hint.c | wc -l)
echo $number
echo -e "\n"

a=10  
if [ $a -eq 10 ]  
then  
    echo "a = 10"  
fi  
echo -e "\n"

echo "如果compile_error_hint.c里面包含tanghanyue字符串的个数大于2两个,打印“the number of tanghanyue is greater than 2 ”。"
if [ $number -gt 2 ]
then
  echo the number of tanghanyue is greater than 2 
fi
echo -e "\n"

echo "如果compile_error_hint.c里面包含tanghanyue字符串的个数大于10两个,打印“second the number of tanghanyue is greater than 10 ”。"

if (("$number" > "10" ))
then
  echo second the number of tanghanyue is greater than 10 
fi
echo -e "\n"

二、脚本搜索的文件compile_error_hint.c:

weifanghai
weifanghai
weifanghai
weifanghai
weifanghai

love
love
love
love
love
love

tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue

三、运行脚本,测试 结果:

weifanghai@weifanghai-VirtualBox:/work/shell$ ./search_string.sh 
搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印yes messycode,反之打印“no messycode”
no messycode


搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,如果有打印yes tanghanyue,反之打印“no tanghanyue”
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
tanghanyue
yes tanghanyue


显示compile_error_hint.c 文件里面包含“messycode”字符串的个数
9


显示compile_error_hint.c 文件里面包含“messycode”字符串的个数
9


搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,并把结果保存到number.txt文件里面


搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印yes messycode,反之打印“no messycode”
yes tanghanyue


搜索compile_error_hint.c 文件里面是否包含“weifanghai”字符串,如果有打印yes weifanghai,反之打印“no weifanghai”
yes weifanghai


搜索compile_error_hint.c里面包含tanghanyue字符串的个数,并打印出来。
9


a = 10


如果compile_error_hint.c里面包含tanghanyue字符串的个数大于2两个,打印“the number of tanghanyue is greater than 2 ”。
the number of tanghanyue is greater than 2


如果compile_error_hint.c里面包含tanghanyue字符串的个数大于10两个,打印“second the number of tanghanyue is greater than 10 ”。


weifanghai@weifanghai-VirtualBox:/work/shell$ 

贴个实际运行图:


  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值