英文:
Bash script is not maching regex
问题
I truly don't understand what I'm missing here.
我真的不明白我在这里漏掉了什么。
I have a very basic script that needs to check if text contains only numbers (more specifically, a semantic version for an app), then do some stuff.
我有一个非常基本的脚本,需要检查文本是否仅包含数字(更具体地说,是应用程序的语义版本),然后执行一些操作。
The text.txt is something like this:
text.txt的内容类似于这样:
appVersion: 2.0.1
and this is my script:
这是我的脚本:
#!/bin/bash
appVersion=`cat test.txt | awk '/^appVersion:/{print $2}'`
echo "appVersion is " $appVersion
Version=3.1.5
if [[ $appVersion =~ [0-9]+(\.[0-9]+){2,3}$ ]] ; then
echo "is a number" $appVersion
else
echo "not a number" $appVersion
fi
Basically, I use cat
to extract what I need from the text and then try to use that information in an if condition.
基本上,我使用cat
从文本中提取我需要的内容,然后尝试在if条件中使用该信息。
The problem is the script is not consistently recognizing the variable $appVersion
every time I run it. It keeps saying that there aren't any numbers in my variable, as if it's not matching the regex. However, if I pass the variable $Version
, the script works.
问题在于,每次运行脚本时,脚本似乎不能始终识别变量$appVersion
。它一直说我的变量中没有任何数字,就好像它不能匹配正则表达式一样。但是,如果我传递变量$Version
,脚本就能正常工作。
By the way, the echo "appVersion is " $appVersion
line is working (it's printing what I placed inside appVersion), so the command inside my variable $appVersion
is actually retrieving what I need.
顺便说一下,echo "appVersion is " $appVersion
这一行是有效的(它打印出了我放在appVersion中的内容),所以我的变量$appVersion
内部的命令实际上是在检索我需要的内容。
I think I'm missing something with awk
, but I cannot understand what.
我认为我在使用awk
方面漏掉了一些东西,但我无法理解是什么。
Edit:
编辑:
I'm running this script on my local PC, on WSL2 Ubuntu 20, as a test, to later run it on a Linux Azure DevOps agent.
我正在本地PC上运行此脚本,在WSL2 Ubuntu 20上,作为测试,稍后将在Linux Azure DevOps代理上运行它。
英文:
I truly don't understand what I'm missing here.
I have a very basic script that need to check if text contains only number (more specific a semantic version for an app), then do some stuff.
The text.txt is something like this:
appVersion: 2.0.1
and this is my script
#!/bin/bash
appVersion=`cat test.txt | awk '/^appVersion:/{print $2}'`
echo "appVersion is " $appVersion
Version=3.1.5
if [[ $appVersion =~ [0-9]+(\.[0-9]+){2,3}$ ]] ; then
echo "is a number" $appVersion
else
echo "not a number" $appVersion
fi`
Basically I cat
from the text what I need, and then trying to pass that info in an if condition.
The problem is the script is basically not taking the variable appVersion
every time I run it, it keep saying that there isn't any number in my variable, like is not matching the regex, but if I pass the variable $Version
the script works.
By the way the echo "appVersion is " $appVersion is working (it's printing what I run inside appVersion), so the command inside my variable $appVersion is actually retrieving what I need.
I think I'm missing something with awk but I cannot understand what.
Edit:
I'm running this script on my local pc, on wsl2 ubuntu 20, as a test, to let then run it on linux azure devops agent
答案1
得分: 3
我建议替换
appVersion=`cat test.txt | awk '/^appVersion:/{print $2}'`
与
appVersion=$(awk '/^appVersion:/{print $2}' test.txt | dos2unix)
或者使用GNU的awk
appVersion=$(awk 'BEGIN{RS="\r\n"} /^appVersion:/{print $2}' test.txt)
以转换Windows的回车符。
参见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
英文:
I suggest to replace
appVersion=`cat test.txt | awk '/^appVersion:/{print $2}'`
with
appVersion=$(awk '/^appVersion:/{print $2}' test.txt | dos2unix)
or with GNU awk
appVersion=$(awk 'BEGIN{RS="\r\n"} /^appVersion:/{print $2}' test.txt)
to convert Windows' carriage-returns.
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论