Mixing regular expression and other conditional expression in a bash if statement
I can't get around this for quite sometime now. As I read along manuals and tutorials I'm getting more confused. I want an if statement with the following logic:
if [ -n $drupal_version ] && [[开发者_运维知识库 "$drupal_version" =~ DRUPAL-[6-9]-[1-9][1-9] ]]; then
but I can't get it to work properly.
When the script is evaluated using the "bash -x ... " script construct, works ok but when is run as a regular script my expression is not evaluated (eventhough the above condition should be met the else part is run).
Could you provide any help?
you can use case/esac
, no regex. can be used in bash <3.2
if [ -n "$drupal_version" ] ;then
case "$drupal_version" in
DRUPAL-[6-9]-[1-9][1-9])
echo "found"
;;
*)
echo "version not correct"
;;
esac
fi
however, if you want regex, note it's =~
not =
[[ $drupal_version =~ DRUPAL-[6-9]-[1-9][1-9] ]] && echo "found"
Thank you all for your replies. The double brackets ]] are a construct specific for bash which in turn requires the first line to your script pointing to #!/bin/bash instead of #!/bin/sh that mine did. Changed that line and everything works.
精彩评论