Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to run a bash command on output from Drupal's drush command-line interface. drush site-alias returns a list of webroots, first showing the name of the group, and then each site in that group. The site itself is aliased in the form group.site. For instance, you might get

internal internal.site1 internal.site2 external external.site1 external.site2 marketing marketing.site1 marketing.site2

I want to do a command on each of the site aliases, but not on the group alias itself. I need to test if the string has a period in it, and if so, run it:

for i in $(drush site-alias); do {if no period) drush $i command; done;

How can I run this test?

share|improve this question

5 Answers 5

up vote 3 down vote accepted

You can use pattern matching:

for i in $(drush site-alias) ; do
    if [[ $i == *.* ]] ; then
        drush $i command
    fi
done
share|improve this answer

With any Bourne-like shell, you'd write it:

case $i in
  *.*) drush "$i" command;;
esac
share|improve this answer

You can do this with a pattern replace expansion:

for i in $(drush site-alias); do
  if [ -z "${i//[^.]/}" ]; then
     # no period
     drush "$i" command
  fi
done

Yes, I quoted "$i", which is probably something you should do when possible, it avoids surprises. Though in this case it won't matter.

share|improve this answer
    
You don't have to quote it if you use [[ instead of [. You are already using non-POSIX features. –  jordanm Feb 1 '13 at 23:54
    
@jordanm No, I mean I quoted it in the drush "$i" line. Which you should quote regardless of using [[ vs. [. –  derobert Feb 2 '13 at 1:48

Just for the sake of another independent answer, I'm going to suggest using shell parameter expansions.

for i in $(drush site-alias)
do
    if [[ ${i%.*} == $i ]]
    then
        drush $i command
    fi
done
share|improve this answer
    
I don't see any advantage of this over [[ $i = *.* ]], which ksh (even ksh88) also understands and is markedly clearer and marginally faster. –  Gilles Feb 1 '13 at 23:10
    
Like I wrote, just another answer: why leave something out, if stackexchange is to be a definitive Q&A site? –  Bruce Ediger Feb 1 '13 at 23:41

Using the =~ bash operator.

for i in $(drush site-alias); do
  if ! [[ $i =~  \. ]]; then
     drush "$i" command
  fi
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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