Search This Blog

Thursday, March 27, 2014

Debugging Child and Source Unix Scripts

Debugging The Original:


Script gets executed in debug mode if we specify sh -x while calling.


 1)By sh -x


Say script.sh is file with content

$cat script.sh

count=1
while [ $count -lt 4 ]
do
echo"iteration: $count"
count=`expr $count + 1`
done

$./script.sh

iteration: 1
iteration: 2
iteration: 3

$sh -x ./script.sh

iteration: 1
iteration: 2
iteration: 3


+ count=1
+ [ 1 -lt 4 ]
+ iteration: $count"
iteration: 1
+ count=2
+ [ 2 -lt 4 ]

+ iteration: $count"
iteration: 2
+ count=3
+ [ 3 -lt 4 ]
+ iteration: $count"
iteration: 3
+ count=3
+ [ 4 -lt 4 ]


For Child Scripts ::


The actual problem is here ...

As I mentioned debugging can be done by specifying sh -x while executing the script but the same will not work in case any child script gets called by other.

In this case the same action can be performed by specifying set -x after she-band statement in child script





Compiling Java Programs (csh)



The basic requirement to compile and execute java programs on unix is to have jdk installed on box..

If  available, you would be able to see the setup file named setup.csh at /usr/usc/jdk/<version num>/

U need to run the setup file.For this add this to .login.

If [ -f /usr/usc/jdk/1.4.2/setup.csh ]
then
source /usr/usc/jdk/1.4.2/setup.csh
fi

Here source is used to call the file in current shell.
Once after this u can compile ur java program something like

%javac program.java

It creates a .class file with same name of ur java program i.e. program.class
In order to execute already compiled program.java following cmd can be used.

%java program

Script to Disable Cntrl+C on unix box



Some of the examples using trap is already discussed in a separate post where numeric notation is used for different signals.

Below are the standard examples by which we can come to know the importance of trap in our day to day scenarios.

To disable the activity of Cntrl + C ...

trap "echo 'cntrl c is disabled'" INT

This can be placed in .profile or .login accordingly.

Script:

I tried this simple script to ignore cntrl signals like ctrl c and ctrl d

TrapCase()
{
echo "entered cntrl char"
Original Function
}

OriginalFunction()
{
echo "enter input"
trap 'TrapCase' 1 2 15
read value
echo "This is after trap is executed"
echo $value
}

#Calling basic function
OriginalFunction


Friday, March 14, 2014

OFS (Output Field Seperator) in AWK

Like wise IFS, we can define OFS (Output File Separator) also in awk to design the o/p....
Say abc.txt is a file contains below data..

$cat abc.txt
ccc#bbb#111
Nnn#hgh#ghs#hhsh

$awk -F'#' '{print $1 $2}' abc.txt
ccc bbb
Nnn hgh

Below is the example with OFS where output is formatted

$ awk -F'#' 'BEGIN{OFS="-"}{print $1,$2}' abc.txt
ccc-bbb
Nnn-hgh

Specifying a  , between columns is mandatory. OFS will be ignored in case " ," is not specified.

Wednesday, March 12, 2014

Unix Font Coloring

Font co-lour at terminal level can be changed with options of print.The same can be placed in .profile as well for convince.

print "\033[33m This is YELLOW \n"
print "\033[36m This is BLUE \n"
print "\033[35m This is PINK \n"

To get back to the normal test ...

print "\033[m This is Normal Text\n"

Examples:


$print "\033[33m This is YELLOW \n"
This is YELLOW
$
$print "\033[36m This is BLUE \n"
This is BLUE
$
$print "\033[35m This is PINK \n"
This is PINK 
$
$
$print "\033[m This is Normal Text\n"
This is Normal Text
$