Archive for the ‘ Bash ’ Category

basic arithmetics

So I think it is very simple to do some shell scripting, but when you have to calculate some simple variables it is not very handy. So I asked my friend Google and here are some of the result, simply to add, subtract, multiply vars in the bash language!

VAR=55             # Assign integer 55 to variable VAR.
((VAR = VAR + 1))  # Add one to variable VAR.  Note the absence of the '$' character.
((++VAR))          # Another way to add one to VAR.  Performs C-style pre-increment.
((VAR++))          # Another way to add one to VAR.  Performs C-style post-increment.
echo $[VAR * 22]   # Multiply VAR by 22 and substitute the result into the command.
echo $((VAR * 22)) # Another way to do the above.
 
 
# some date arithmetics (yestarday and one month ago)
date -d “-1 day” +%d%m     #will give me the yesterdays date.
date -d “-1 month” +%d%m  #will give me date one month previous.
 
Example: 
YESTERDAY=`date -d"-1 day" "+%Y.%m.%d"`;
echo $YESTERDAY;

Open multiple konsoles

Script to open multiple konsoles on kde 3.x

#!/bin/sh
SESSIONS="
    SESSION_1
    SESSION_2
"
 
KONSOLE=`dcopclient $KONSOLE_DCOP`
CURSESSION=$KONSOLE_DCOP_SESSION
 
for A in $SESSIONS ; do
    NEWSESSION=`dcop $KONSOLE konsole newSession $A`
    #dcop $KONSOLE $NEWSESSION renameSession $A
done
 
#dcop $CURSESSION closeSession

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