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:
#!/bin/sh
telnet_log=/tmp/telnet.log.$$
restore_title()
{
if [ "${OLD_TITLE}x" != "x" ]
then
dcop "$KONSOLE_DCOP_SESSION" renameSession "$OLD_TITLE"
OLD_TITLE=
fi
}
change_title()
{
if [ ! -z "$KONSOLE_DCOP_SESSION" ]
then
OLD_TITLE=`dcop "$KONSOLE_DCOP_SESSION" sessionName`
dcop "$KONSOLE_DCOP_SESSION" renameSession "$NEW_TITLE"
fi
}
handle_quit_intr()
{
restore_title
}
trap handle_quit_intr SIGINT INT QUIT
for arg in $@
do
NEW_TITLE="$arg"
done
OLD_TITLE=
export OLD_TITLE
export NEW_TITLE
change_title
/usr/bin/telnet $* 2> $telnet_log
rc=$?
restore_title
echo "telnet exit with code $rc"
if [ $rc -eq 0 ]
then
[ -f $telnet_log ] && rm -f $telnet_log
exit 0
fi
exit $rc
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##*/}
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:
cat > $RQXML <<End
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SetStatus xmlns="http://domain.com/">
<FormID>${FORM_ID}</FormID>
<Status>${ITEM}</Status>
</SetStatus>
</soap12:Body>
</soap12:Envelope>
End
Then we will send request to web service using wget:
if ! /usr/local/bin/wget -q -O $OUTPUT --post-file=$RQXML
--header="Content-Type: text/xml; charset=utf-8" ${URL}
then
# failre here
return 0
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.
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:
totalProc=4
idx=0
while [[ $idx -lt $totalProc ]]
do
# will wait for each OS
if [[ ${CHILDS[$idx]} -gt 2 ]]
then
# starting child process
${executable name} 2>&1 > ${logname}.log <strong>&</strong>
CHILDS[$idx]=$!
fi
idx=$(( $idx + 1 ))
done
Next step is to wait till child processes are finished:
idx=0
while [[ $idx -lt $totalProc ]]
do
# will wait for each OS
if [[ ${CHILDS[$idx]} -gt 2 ]]
then
echo "===================== waiting for ${CHILDS[$idx]}"
PID=${CHILDS[$idx]}
wait $PID
CHILDS[$idx]=$?
if [[ ${CHILDS[$idx]} -ne 0 ]]
then
# process failed
else
# process finished
fi
fi
idx=$((idx+1))
done
This script will wait for all children processes one by one in order they were created.