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" 其实这里就是用到了"[...

echo $A | grep $B

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

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

if [ -e filename ]; 我觉得是你括号错了…此外这种括号不能用通配符…试试[[]]?不行可以用for代替if

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

diskutil info /dev/disk0|awk -F: '/Media Name/{print $2}'

$bash 1.shstring=how are you?string=ok?$cat 1.sh#!/usr/bin/bashwhile read stringdo echo "string=$string"done < 111 $cat 111how are you?ok?脚本名1.sh, 要读取的文件名111。

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