views:

198

answers:

2

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?

A: 

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"
ghostdog74
the case approach is an interesting one. The use of "=" is a typo from my part, in my code I use "=~'. Is there a problem when mixing single brackets [ with double brackets [[ in the same if statement?
Tassos
no, there's no problem, but there are some differences between using `]` and `]]`. if you use single `]`, quote your variables. you can see http://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash
ghostdog74
A: 

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.

Tassos