Coldfront Labs

19/08/2009 - blog

Daisy chaining commands - Redux

A while ago, Mat posted a blog entry regarding running multiple commands on the same line.  While queuing up commands like that is an invaluable feature of any shell, what do you do if you want to run commands that depend on each other?

As an example, imagine a program that operates on the current working directory, so you want to change to the target directory, then run the script.

cd /var/www/html/mysite/tmp; /usr/bin/my-script

But what if something went wrong? What if the directory didn't exist or you didn't have permissions to enter it? Your script would be run, but not in the directory you intended.

Luckily for us, there is '&&'. Like ';', '&&' allows you to run multiple commands at once, but it only runs the next command if the current one exited successfully.

So instead you could run:

cd /var/www/html/mysite/tmp && /usr/bin/my-script
dpascoed's picture

David Pascoe-Deslauriers

System Architect

Latest Tweets

  • #dcsf picture time! 20 weeks 12 hours ago
  • What kind of team building stuff do you do remotely? (since not everyone is local) #magicteams 20 weeks 17 hours ago
  • Really enjoying Drupalcon, learning so much! Need time to digest the info....and the awesome food! 20 weeks 1 day ago
  • Holy crap! http://tinyurl.com/yycqls6 Live NHL games on Boxee :-D #iamcanadian 20 weeks 2 days ago
  • Just getting to my second session at #dcsf and I've already lost my pen :-S 20 weeks 2 days ago

Poll

Do you plan on switching to Drupal 7 when it's released?:
Copyright © 2010