I am working on one bash script, in which I have to use the regular expression to match string and then store the output in a variable to reuse it.
我正在处理一个bash脚本,在该脚本中,我必须使用正则表达式来匹配字符串,然后将输出存储在一个变量中以重用它。
here is my script,
这是我的脚本,
#!/bin/sh
NAME="MET-3-get-code-from-string"
por="$($NAME | grep -P -o -e '(?<=MET-).*?(\d+)')" #this should store 3 in variable por
echo $por
I tried this many ways, but I am getting error :
./check.sh: MET-3-get-issue-id-from-branch-name: not found
我尝试了很多方法,但是我得到了错误:./检查。承宪:MET-3-get-issue-id-from-branch-name:未找到
if I run individual grep command then yes, it is working properly. But I am not able to store output.
如果我运行单独的grep命令,那么是的,它运行正常。但是我不能存储输出。
I also tried :
我也试过:
por=$($NAME | grep -P -o -e '(?<=MET-).*?(\d+)')
por=$NAME | grep -P -o -e '(?<=MET-).*?(\d+)'
and many other similar references.
还有很多类似的参考资料。
but it's not working. can anyone please help me on this. I have not much experience in bash.
但这不是工作。在这件事上谁能帮我一个忙吗?我在bash上没有多少经验。
thank you.
谢谢你!
1 个解决方案
#1
2
Change
改变
por="$($NAME | grep -P -o -e '(?<=MET-).*?(\d+)')"
to
来
por="$(echo "$NAME" | grep -P -o -e '(?<=MET-).*?(\d+)')"
Also, you are missing a closing double quote (maybe just a typo, should be NAME="MET-3-get-code-from-string"
)
另外,您还缺少一个结束双引号(可能只是一个错误,应该是NAME=" met -3 get-code-from-string")
#1
2
Change
改变
por="$($NAME | grep -P -o -e '(?<=MET-).*?(\d+)')"
to
来
por="$(echo "$NAME" | grep -P -o -e '(?<=MET-).*?(\d+)')"
Also, you are missing a closing double quote (maybe just a typo, should be NAME="MET-3-get-code-from-string"
)
另外,您还缺少一个结束双引号(可能只是一个错误,应该是NAME=" met -3 get-code-from-string")