RSS

Tag Archives: shell

Running multiple bash commands inline

‘&’ just runs the command in the background.

ls &

Remember that if the running command receives an HUP interrupt (maybe you’re closing the terminal), it will be killed.
If you want the command will survive your ‘exit’, you can launch the command with the nohup

nohup ls &

‘&&’ is to concatenate two commands (AND): the second command will only run if the first command finishes successfully, that is if it sends an exit code of 0.

command1 && command2

‘||’ is to concatenate two commands (OR): the second command will only run if the first one fails in some way (exit code is not 0; the command exited with an error).

command1 || logErrorAndExit1

‘;’ is to concatenate two commands: the second command will start right after the first one finishes, no matter whether it succeeds or not.

command1 ; command2
 
Leave a comment

Posted by on 2013/04/18 in dev

 

Tags: ,

How to create a script library in Bash

Create your common routines file (i.e. naming the file “mylibrary.sh”):

#MY ROUTINES LIBRARY
function fn1
{
  echo "fn1 called"
}
function fn2
{
  echo "fn2 called with parameter: " $1
}

Then “include” the library at the top of your bash script to use your common functions:

#!/bin/bash
. "./mylibrary.sh"
fn1
fn2 "i'm the parameter"

This way the library file will be executed at the beginning, making all the functions ready to be called.

 
Leave a comment

Posted by on 2013/03/12 in dev

 

Tags: , , ,