linux shell 脚本判断某个文件是否有某个字符串以及统计含有某个字符串的个数然后做if else大于小于等于逻辑判断
2017-10-28 12:16
726 查看
简介:本脚本主要用来学习判断某个文件是否有某个字符串以及统计含有某个字符串的个数,然后做if else大于小于等于逻辑判断去执行对应的操作。
一、测试脚本search_string.sh:
二、脚本搜索的文件compile_error_hint.c:
三、运行脚本,测试 结果:
贴个实际运行图:
一、测试脚本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$
贴个实际运行图:
相关文章推荐
- 判断量文件相等,判断某文本中是否含有某字符串
- 监测程序详细介绍,快捷键的设置;txt文件里面一行行的读取、删除、添加字符串等;根据程序路径名以及程序名判断程序是否在运行;
- linux shell脚本对未定义变量的判断以及if的用法
- 读取文件,判断是否含有固定字符串
- linux shell脚本对未定义变量的判断以及if的用法详解
- linux shell 编程1文件权限检测、字符串比较判断、数值大小比较、时间获取以及单引号双引号反引号等的使用
- 判断字符串是否是回文字符串或者是否含有回文字符子串 (我是从两颗番茄的博客看的题目,然后自己编的) 不断学习!!
- Oracle取字符串长度以及判断是否含有中文
- shell 判断文件中是否含有某个字符串
- Shell脚本执行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell判断hdfs文件目录是否存在
- iOS 判断字符串是否含有某种字符
- java cloneable以及如何判断一个字符串是否为数字。
- mybatis 映射文件中if标签判断字符串相等的两种方式
- Oracle 判断字符串是否含有汉字
- [转] Linux shell判断文件和文件夹是否存在
- PHP判断字符串中是否含有中文
- C#判断输入的字符串中是否含有大写字母,小写字母,数字,字符
- Shell脚本中判断文件,目录是否存在或者具有权限
- 查找目录下的所有文件中是否含有某个字符串
- Python判断一个文件中的字符串是否存在于另外一个文件中