regex to validate string from a variable value

The name of the pictureThe name of the pictureThe name of the pictureClash 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"










share|improve this question





















  • 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














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"










share|improve this question





















  • 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












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"










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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










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.






share|improve this answer




















  • Thanks @fugitive ...
    – itgeek
    20 mins ago

















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





share|improve this answer








New contributor




GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer




















    • Thanks @fugitive ...
      – itgeek
      20 mins ago














    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.






    share|improve this answer




















    • Thanks @fugitive ...
      – itgeek
      20 mins ago












    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 25 mins ago









    fugitive

    780418




    780418











    • Thanks @fugitive ...
      – itgeek
      20 mins ago
















    • Thanks @fugitive ...
      – itgeek
      20 mins ago















    Thanks @fugitive ...
    – itgeek
    20 mins ago




    Thanks @fugitive ...
    – itgeek
    20 mins ago












    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





    share|improve this answer








    New contributor




    GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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





      share|improve this answer








      New contributor




      GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        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





        share|improve this answer








        New contributor




        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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






        share|improve this answer








        New contributor




        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 12 mins ago









        GarethHumphriesAcc

        112




        112




        New contributor




        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        GarethHumphriesAcc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?