regex to validate string from a variable value
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
var1="temp-pprod-deployment"
Need a shell script for the below use case;
if the above variable $var1 value contains "prod" string then execute a print message eg. echo "Found" else echo "Not found"
linux sed
add a comment |Â
up vote
0
down vote
favorite
var1="temp-pprod-deployment"
Need a shell script for the below use case;
if the above variable $var1 value contains "prod" string then execute a print message eg. echo "Found" else echo "Not found"
linux sed
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
var1="temp-pprod-deployment"
Need a shell script for the below use case;
if the above variable $var1 value contains "prod" string then execute a print message eg. echo "Found" else echo "Not found"
linux sed
var1="temp-pprod-deployment"
Need a shell script for the below use case;
if the above variable $var1 value contains "prod" string then execute a print message eg. echo "Found" else echo "Not found"
linux sed
linux sed
asked 39 mins ago
itgeek
143
143
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago
add a comment |Â
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)
case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)
case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can do something like this:
var1="temp-pprod-deployment"
if `echo "$var1" | grep -q "prod"` ;then
echo "$var1 contains word 'prod'"
else
echo "Not found."
fi
Explanation: You are getting output of variable and piping it to grep for regex. The -q
option, means to return 0 on success(true) and 1 on failure(false), which is test
-ed with if
.
Thanks @fugitive ...
â itgeek
20 mins ago
add a comment |Â
up vote
0
down vote
Use bashes / operator to remove the test string from the variable contents, and see if that operation has changed it. If it has, you know the string is present:
$ var1="temp-pprod-deployment"
$ var2="temp-pdev-deployment"
$ [ "$var1/prod" == "$var1" ] && echo not found
$ [ "$var2/prod" == "$var2" ] && echo not found
not found
$ [ "$var1/dev" == "$var1" ] && echo not found
not found
$ [ "$var2/dev" == "$var2" ] && echo not found
In full compliance with the OP:
if [ "$var1/prod" != "$var1" ]
then
echo "Found."
else
echo "Not found."
fi
New contributor
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can do something like this:
var1="temp-pprod-deployment"
if `echo "$var1" | grep -q "prod"` ;then
echo "$var1 contains word 'prod'"
else
echo "Not found."
fi
Explanation: You are getting output of variable and piping it to grep for regex. The -q
option, means to return 0 on success(true) and 1 on failure(false), which is test
-ed with if
.
Thanks @fugitive ...
â itgeek
20 mins ago
add a comment |Â
up vote
0
down vote
You can do something like this:
var1="temp-pprod-deployment"
if `echo "$var1" | grep -q "prod"` ;then
echo "$var1 contains word 'prod'"
else
echo "Not found."
fi
Explanation: You are getting output of variable and piping it to grep for regex. The -q
option, means to return 0 on success(true) and 1 on failure(false), which is test
-ed with if
.
Thanks @fugitive ...
â itgeek
20 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can do something like this:
var1="temp-pprod-deployment"
if `echo "$var1" | grep -q "prod"` ;then
echo "$var1 contains word 'prod'"
else
echo "Not found."
fi
Explanation: You are getting output of variable and piping it to grep for regex. The -q
option, means to return 0 on success(true) and 1 on failure(false), which is test
-ed with if
.
You can do something like this:
var1="temp-pprod-deployment"
if `echo "$var1" | grep -q "prod"` ;then
echo "$var1 contains word 'prod'"
else
echo "Not found."
fi
Explanation: You are getting output of variable and piping it to grep for regex. The -q
option, means to return 0 on success(true) and 1 on failure(false), which is test
-ed with if
.
answered 25 mins ago
fugitive
780418
780418
Thanks @fugitive ...
â itgeek
20 mins ago
add a comment |Â
Thanks @fugitive ...
â itgeek
20 mins ago
Thanks @fugitive ...
â itgeek
20 mins ago
Thanks @fugitive ...
â itgeek
20 mins ago
add a comment |Â
up vote
0
down vote
Use bashes / operator to remove the test string from the variable contents, and see if that operation has changed it. If it has, you know the string is present:
$ var1="temp-pprod-deployment"
$ var2="temp-pdev-deployment"
$ [ "$var1/prod" == "$var1" ] && echo not found
$ [ "$var2/prod" == "$var2" ] && echo not found
not found
$ [ "$var1/dev" == "$var1" ] && echo not found
not found
$ [ "$var2/dev" == "$var2" ] && echo not found
In full compliance with the OP:
if [ "$var1/prod" != "$var1" ]
then
echo "Found."
else
echo "Not found."
fi
New contributor
add a comment |Â
up vote
0
down vote
Use bashes / operator to remove the test string from the variable contents, and see if that operation has changed it. If it has, you know the string is present:
$ var1="temp-pprod-deployment"
$ var2="temp-pdev-deployment"
$ [ "$var1/prod" == "$var1" ] && echo not found
$ [ "$var2/prod" == "$var2" ] && echo not found
not found
$ [ "$var1/dev" == "$var1" ] && echo not found
not found
$ [ "$var2/dev" == "$var2" ] && echo not found
In full compliance with the OP:
if [ "$var1/prod" != "$var1" ]
then
echo "Found."
else
echo "Not found."
fi
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use bashes / operator to remove the test string from the variable contents, and see if that operation has changed it. If it has, you know the string is present:
$ var1="temp-pprod-deployment"
$ var2="temp-pdev-deployment"
$ [ "$var1/prod" == "$var1" ] && echo not found
$ [ "$var2/prod" == "$var2" ] && echo not found
not found
$ [ "$var1/dev" == "$var1" ] && echo not found
not found
$ [ "$var2/dev" == "$var2" ] && echo not found
In full compliance with the OP:
if [ "$var1/prod" != "$var1" ]
then
echo "Found."
else
echo "Not found."
fi
New contributor
Use bashes / operator to remove the test string from the variable contents, and see if that operation has changed it. If it has, you know the string is present:
$ var1="temp-pprod-deployment"
$ var2="temp-pdev-deployment"
$ [ "$var1/prod" == "$var1" ] && echo not found
$ [ "$var2/prod" == "$var2" ] && echo not found
not found
$ [ "$var1/dev" == "$var1" ] && echo not found
not found
$ [ "$var2/dev" == "$var2" ] && echo not found
In full compliance with the OP:
if [ "$var1/prod" != "$var1" ]
then
echo "Found."
else
echo "Not found."
fi
New contributor
New contributor
answered 12 mins ago
GarethHumphriesAcc
112
112
New contributor
New contributor
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477827%2fregex-to-validate-string-from-a-variable-value%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Do you really need a regex for this? wouldn't a simple shell glob suffice e.g. (in a Bourne-like shell)
case $var1 in *prod*) echo 'Found';; *) echo 'Not found';; esac
â steeldriver
17 mins ago