0

I am validating string with some standards. It should start wih R- and proj_nm as a variable and project_cd as numbers and any desc i.e, R-${PROJ_NM}-[Numbers]-anything

if [[ $var == "R-"${PROJ_NM}"-"[[:digit:]]"-"* ]] ; then echo "Naming convention is in correct format" else echo "Not in correct format" exit 1 fi

I am getting output as not in correct format.

  • 4
    Please edit your question and show us a few examples of input, what you want to get from that input and what you actually get from that input. – terdon Feb 8 at 12:02
4

If you need to have one or more numbers (you said numbers), then you need [[:digit:]][[:digit:]] for two, [[:digit:]][[:digit:]][[:digit:]] for three etc.

if [[ $var == "R-"${PROJ_NM}"-"[[:digit:]][[:digit:]]"-"* ]] ; then 
    echo "Naming convention is in correct format"
else 
    echo "Not in correct format" 
fi

But you can simplify your quotes a little:

if [[ $var == "R-${PROJ_NM}-"[[:digit:]][[:digit:]]"-"* ]] ; then 
    echo "Naming convention is in correct format"
else 
    echo "Not in correct format" 
fi

Alternatively, with newer bash versions, you can use regular expressions:

if [[ $var =~ ^R-${PROJ_NM}-[0-9]+- ]] ; then 
    echo "Naming convention is in correct format"
else 
    echo "Not in correct format" 
fi

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.