题目描述str = 'master 7ecbf3f [origin/master: ahead 8, behind 1] local'1.如何取到[ ]之间的字符串origin/master: ahead 8, behind 1?2.如何判断1中取到的字符串包含behind或者diverged?shell太销魂了,求解!
2 回答
慕侠2389804
TA贡献1719条经验 获得超6个赞
1.如何取到[ ]之间的字符串origin/master: ahead 8, behind 1?
str='master 7ecbf3f [origin/master: ahead 8, behind 1] local'#方法一grep -Po '\[\K[^]]+' <<<$str#方法二sed 's/[^[]\+\[//;s/].*//' <<<$str#方法三awk -F'[][]' '{print $2}' <<<$str
2.如何判断1中取到的字符串包含behind或者diverged?
[[ $str =~ "behind" || $str =~ "diverged" ]] && echo "匹配" || echo "不匹配"
- 2 回答
- 0 关注
- 1335 浏览
添加回答
举报
0/150
提交
取消