Archive for the ‘ Bash ’ Category

First steps in bash langage

My first bash script, I really prefer Ruby!

#!/bin/bash
# $Id: app.sh 314274 2004-05-24 21:04:46Z geiseri $
# goOut - Copyright (C) 2007 Joern Berrisch <job@gianna>
 
DESCRIPTION='Data syncronisation';
RUNNING_ERROR="$DESCRIPTION is already running. Please wait a bit.";
PID_FILE=/var/run/trip_data_sync.run;
 
E_WRONGARGS=65;
 
USAGE="
Usage : `basename $0`
  -h [http-server]
  -s [switch-to-host]
  -u [ssh-user]
  -p [ssh-port]
";
 
SSH_KEY_FILE=/home/httg/.ssh/id_dsa;
SSH_USER=lundi;
SSH_PORT=22;
HTTP_SERVER_NAME=lundgren;
SWITCH_TO_HOST=lollipop;
 
showUsage() {
  echo -e $USAGE;
  exit $E_WRONGARGS;
}
 
main() {
  switchHttp;
  #echo "executing main";
}
 
switchHttp() {
  printf "Switch $HTTP_SERVER_NAME to $SWITCH_TO_HOST ";
 
  ssh -i $SSH_KEY_FILE $SSH_USER@$HTTP_SERVER_NAME -p$SSH_PORT "echo -e '' && ls && pwd" &
 
  while [ "$(jobs -r | grep -c .)" -gt 0 ]; do
    printf "." ;
    sleep 0.1;
  done
  wait;
}
 
if [[ $1 == '-help' || $1 == '--help' ]]; then
  showUsage;
fi
 
while getopts ":h:s:u:p:" OPTS; do
  case $OPTS in
    h ) HTTP_SERVER_NAME=$OPTARG;;
    s ) SWITCH_TO_HOST=$OPTARG;;
    u ) SSH_USER=$OPTARG;;
    p ) SSH_PORT=$OPTARG;;
  esac
done
shift $(($OPTIND - 1))
 
 
if [ ! -e $PID_FILE ]; then
  touch $PID_FILE;
 
 
  # begin main()
  main
  # end main()
 
 
  rm $PID_FILE;
else
  echo -e $RUNNING_ERROR;
  exit 1;
fi;
 
exit 0