Bash script notes

A couple of points I wanted to track regarding a bash script a had to write recently:

1 - Do and action if the command execution is successful

Assuming you want to delete a docker container only if it exists. You can first try to inspect that container and if the return value is successful you can then delete it.

if docker inspect $CONTAINER_NAME &> /dev/null;  
  echo "Destroying $CONTAINER_NAME container"
  docker rm -f $CONTAINER_NAME
2 - Use absolute paths when referring to dependent scripts

A common problem with scripts with multiple dependent files is that a certain path must be hardcoded on the script which can then lead to breaking paths depending on where the user executes that script. To avoid any issues on that regards you can use the bash variable BASH_SOURCE

script_dir=$(dirname "${BASH_SOURCE[0]}")  

So if you need to execute a script relative to the one being executed you can refer to:


Instead of just


Executing script on the host when running Vagrant commands

When running Vagrant commands there might be the need to execute something on the host as a pre setup requirement.

Since Vagrant is built with ruby it is possible to execute a shell script utilizing the system utility


In some cases you might want to run a script only in a certain command of Vagrant, for example, when reloading the Vagrant box you might need to check if your host has all gateway routes configured or any other pre setup task.

  if [ #{ARGV[0]} = 'reload' ]; then
    echo 'Setting up routes on the host when reloading Vagrant box'

Bash script to check if route exists with netstat and grep

Recently while crafting a Vagrantfile I came across the need of checking if I had already added a route to my host when reload the vagrant box.

Listing routes
netstat -nr  
  • -r shows the routing table
  • -n shows the network addresses as number
Matching the pattern
grep -co '172.17 *'  
  • -c writes the number of matches to stdout
  • -o prints only the matching part of the line

Putting both commands together

if [ $(netstat -nr | grep -co '172.17 *') = 0 ]; then  
        echo 'Adding gateway route for docker0 network'
        sudo route -n add " + $vb_ip_address + "

Migrate git repositories

Recently I came across the task of migrating existing repositories from one gitlabs instance to another.

I could have generated a backup and restored it, however, I didn’t want to deal with any migration issues so I decided to simple push the existing repositories to a new remote.

Below is a shell scripts that clones existing git repositories and then push their refs to a new remote:



 for (( i = 0 ; i < ${#old_name[@]} ; i=$i+1 ));  
 echo -e "nnRepo $i"  
 echo "old – $old"  
 echo "new – $new"

 if [ ! -d "$old" ]; then  
 echo -e "nCloning repository $old…"

 git clone [email protected]:clouddynamics/$old.git  
 cd $old  
 git remote rm origin  
 git remote add origin [email protected]:cloud-dynamics/$new.git  
 git push –all origin  
 cd $cur_dir  


The script above will not push all branches to the new remote.
If you want to migrate all branches run the following script as well:



 for (( i = 0 ; i < ${#old_name[@]} ; i=$i+1 ));  
 echo -e "nnRepo $i"  
 echo "old – $old"  
 cd $old  
 echo -e "nPushing branches from old to origin remote…"  
 git remote add old [email protected]:clouddynamics/$old.git  
 git fetch old  
 git push origin refs/remotes/old/*:refs/heads/*  
 cd $cur_dir