Automatically rename KONSOLE tab during telnet or ssh.

Posted on June 6, 2008 by ZDima.
Categories: Development, UNIX, UNIX-Shell.

I am using KDE konsole as my primary terminal interface.

To keep tab organized I am using wrapper script for telnet and ssh where I am calling dcop to change the tab name with a with a host name:

  1. #!/bin/sh
  2.  
  3. telnet_log=/tmp/telnet.log.$$
  4.  
  5. restore_title()
  6. {
  7.     if [ "${OLD_TITLE}x" != "x" ]
  8.     then
  9.         dcop "$KONSOLE_DCOP_SESSION" renameSession "$OLD_TITLE"
  10.         OLD_TITLE=
  11.     fi
  12. }
  13.  
  14. change_title()
  15. {
  16.     if [ ! -z "$KONSOLE_DCOP_SESSION" ]
  17.     then
  18.         OLD_TITLE=`dcop "$KONSOLE_DCOP_SESSION" sessionName`
  19.         dcop "$KONSOLE_DCOP_SESSION" renameSession "$NEW_TITLE"
  20.     fi
  21. }
  22.  
  23. handle_quit_intr()
  24. {
  25.     restore_title
  26. }
  27.  
  28. trap handle_quit_intr SIGINT INT QUIT
  29.  
  30. for arg in $@
  31. do
  32.     NEW_TITLE="$arg"
  33. done
  34.  
  35. OLD_TITLE=
  36. export OLD_TITLE
  37. export NEW_TITLE
  38.  
  39. change_title
  40. /usr/bin/telnet $* 2> $telnet_log
  41. rc=$?
  42. restore_title
  43. echo "telnet exit with code $rc"
  44.  
  45. if [ $rc -eq 0 ]
  46. then
  47.     [ -f $telnet_log ] && rm -f $telnet_log
  48.     exit 0
  49. fi
  50. exit $rc

Get drectory name or file name using shell

Posted on December 19, 2007 by ZDima.
Categories: UNIX, UNIX-Shell.

Don’t have basename or dirname?

Don’t want to start another process every time you need a path?

Not a problem.

dirname == ${path%/*}

basename == ${path##*/}

Calling SOAP API from UNIX Shell

Posted on May 14, 2007 by ZDima.
Categories: UNIX-Shell.

Sometime a UNIX shell is a best choice to have a quick clean solution. However sometimes it is luck the flexibility and APIs to work with latest technologies.

Recently I was asked to write a small UNIX application and one of the requirements was to update MS SQL database table with status value. i didn’t want to install additional packages on UNIX server to achieve direct access to MS SQL server, but rather to use a minimal tools.

I decided to use SOAP as a transport layer between UNIX and MS SQL server. On UNIX machine I install wget. Make sure you install wget with at least version 1.10.2!!!

On Windows machine I create a simple Web Service application using ASP.NET that will access the database using native driver.

Now, to send a request I will create an XML file with SOAP envelop:

  1. cat > $RQXML <<End
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  4.  xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  5.   <soap12:Body>
  6.     <SetStatus xmlns="http://domain.com/">
  7.       <FormID>${FORM_ID}</FormID>
  8.       <Status>${ITEM}</Status>
  9.     </SetStatus>
  10.   </soap12:Body>
  11. </soap12:Envelope>
  12. End

Then we will send request to web service using wget:

  1. if ! /usr/local/bin/wget -q -O $OUTPUT --post-file=$RQXML
  2.        --header="Content-Type: text/xml; charset=utf-8" ${URL}
  3. then
  4.   # failre here
  5.   return 0
  6. fi

The important part of this line is —-header=”Content-Type: text/xml; charset=utf-8″. It will indicate that the stream is an XML data. The reply from server will be stored into ${OUTPUT} which you can parse later for results.

Writing multi-process application in UNIX-Shell

Posted on by ZDima.
Categories: UNIX-Shell.

Sometimes you need to improve performance of an application where UNIX shell script need to start a several process. The solution below can help you if application logic allows to run them in parallel, and application will not advance to the next step till all started processes are finished.

The solution have two parts. First part is to start processes simultaneously:

  1. totalProc=4
  2. idx=0
  3.  
  4. while [[ $idx -lt $totalProc ]]
  5. do
  6.   # will wait for each OS
  7.   if [[ ${CHILDS[$idx]} -gt 2 ]]
  8.   then
  9.     # starting child process
  10.     ${executable name} 2&gt;&amp;1 &gt; ${logname}.log <strong>&amp;</strong>
  11.     CHILDS[$idx]=$!
  12.   fi
  13.   idx=$(( $idx + 1 ))
  14. done

Next step is to wait till child processes are finished:

  1. idx=0
  2. while [[ $idx -lt $totalProc ]]
  3. do
  4.   # will wait for each OS
  5.   if [[ ${CHILDS[$idx]} -gt 2 ]]
  6.   then
  7.     echo "===================== waiting for ${CHILDS[$idx]}"
  8.     PID=${CHILDS[$idx]}
  9.     wait $PID
  10.     CHILDS[$idx]=$?
  11.     if [[ ${CHILDS[$idx]} -ne 0 ]]
  12.     then
  13.       # process failed
  14.     else
  15.       # process finished
  16.     fi
  17.   fi
  18.   idx=$((idx+1))
  19. done

This script will wait for all children processes one by one in order they were created.