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

26/06/2009 - story

UNIX Quick Tip: Daisy chaining commands in the terminal

I discovered this this morning. I'm working on a tomcat application and I have to stop the server, deploy some theme changes with a script then start the server up again (I know, it's a dumb setup but I didn't write the app, I'm only using it ;-) So I'd type "stopcp" to stop the server, then the deploy skin script, then I'd type in "startcp". But (and this may have been obvious to everyone but me) if I put a semi-colon between the commands, I can have all three run one after the other automatically.

Example:

stopcp; deployskintemplate -t classic; startcp

Saves me from having to do three things. I can just sit back and watch it go. Hopefully you find this useful!

Syndicate content

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