jcgj.net
当前位置:首页 >> shEll脚本 字符串包含 >>

shEll脚本 字符串包含

#!/bin/sh str="abc" bb=`echo $str|grep 'a'|wc -l` if [ $bb = 0 ] then echo "no contain" else echo "contain" fi

比如变量是str str="this is a string" 要想在判断str中是否含有"this"这个字符串,下面的语句是可行的 [[ $str =~ "this" ]] && echo "\$str contains this" [[ $str =~ "that" ]] || echo "\$str does NOT contain this" 其实这里就是用到了"[...

grep "80" /var/1.log >/dev/nullif [ $? -eq 0 ]; then echo "Found!"else echo "Not found!"fi用$?取上条grep命令的返回结果。

for file in $(echo `ls -l`) 或者 直接 for file in `ls -l`

stra="this is a string"strb="a string"pos=$(printf "$stra" | awk -v strb="$strb" '{ print index($0, strb) }')if [ $pos -ne 0 ]; then printf "stra contains strb\n"else printf "stra does not contain strb\n"fi

比如: a=1234567890 b=456 echo $s | grep "$b" if [ 0 = $? ]; then echo "string a include string b" fi

sed -i '/cat/d' example.txt

用echo+重定向 。> 生成一个空文件,向内写入,>>追加写入 echo "first line" > output.txtecho "second line" >> output.txt

比如变量是str str="this is a string" 要想在判断str中是否含有"this"这个字符串,下面的语句是可行的 [[ $str =~ "this" ]] && echo "\$str contains this" [[ $str =~ "that" ]] || echo "\$str does NOT contain this"

网站首页 | 网站地图
All rights reserved Powered by www.jcgj.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com