简介:本脚本主要用来学习判断某个文件是否有某个字符串以及统计含有某个字符串的个数,然后做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$ 

贴个实际运行图:


GitHub 加速计划 / li / linux-dash
13
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐