Search This Blog

Monday, July 14, 2014

Sql Loader Data Load in Unix



When we deal with unix oracle environment, importing a file to db table by sql loader is always effective.

Basic flow is ...


read File->sqlldr takes reference of cntrl file->table description check->validate->load line by line

Below is the syntax to work with...

sqlldr username/password control=ctrl_file_name log=logfile

In case default credentials are set through cfg file, username and password can be ignored.

sample cntrl file

OPTIONS (DIRECT=TRUE)
         LOAD DATA
         INFILE sample.log
         --
         INSERT
         INTO TABLE sample
         --
         (sample_col1
         ,sample_col2
,sample_col3
         )


Thursday, April 24, 2014

Random Numbers

Random numbers can be produced like most of the other programming languages in unix with the key word RANDOM .

$echo $RANDOM
29003
$echo $RANDOM
4613

PS: Required number of digits can then be produced by doing module (%).

$echo $((${RANDOM} % 120}
4

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
$

Friday, February 7, 2014

Trap with Examples :--

Trap:


Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.
The following are some of the more common signals you might encounter and want to use in your programs:
Signal NameSignal NumberDescription
SIGHUP1Hang up detected on controlling terminal or death of controlling process
SIGINT2Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT3Issued if the user sends a quit signal (Ctrl + D).
SIGFPE8Issued if an illegal mathematical operation is attempted
SIGKILL9If a process gets this signal it must quit immediately and will not perform any clean-up operations
SIGALRM14Alarm Clock signal (used for timers)
SIGTERM15Software termination signal (sent by kill by default).

List of Signals:

There is an easy way to list down all the signals supported by your system. Just issue kill -l command and it would display all the supported signals:
The actual list of signals varies between Solaris, HP-UX, and Linux.

Sending Signals:

There are several methods of delivering signals to a program or script. One of the most common is for a user to type CONTROL-C or the INTERRUPT key while a script is executing.
When you press the Ctrl+C key a SIGINT is sent to the script and as per defined default action script terminates.
The other common method for delivering signals is to use the kill command whose syntax is as follows:
$ kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the process ID that the signal should be sent to. For Example:
$ kill -1 1001
Sends the HUP or hang-up signal to the program that is running with process ID 1001. To send a kill signal to the same process use the folloing command:
$ kill -9 1001
This would kill the process running with process ID 1001.

Cleaning Up Temporary Files:


$ trap "rm -f $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 2
$ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2
$ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2
As an example of the trap command, the following shows how you can remove some files and then exit if someone tries to abort the program from the terminal:
$ trap "rm -f $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 2
From the point in the shell program that this trap is executed, the two files work1$$ and dataout$$ will be automatically removed if signal number 2 is received by the program.
So if the user interrupts execution of the program after this trap is executed, you can be assured that these two files will be cleaned up. The exit command that follows the rm is necessary because without it execution would continue in the program at the point that it left off when the signal was received.
Signal number 1 is generated for hangup: Either someone intentionally hangs up the line or the line gets accidentally disconnected.
You can modify the preceding trap to also remove the two specified files in this case by adding signal number 1 to the list of signals:
$ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2
Now these files will be removed if the line gets hung up or if the Ctrl+C key gets pressed.
The commands specified to trap must be enclosed in quotes if they contain more than one command. Also note that the shell scans the command line at the time that the trap command gets executed and also again when one of the listed signals is received.
So in the preceding example, the value of WORKDIR and $$ will be substituted at the time that the trap command is executed. If you wanted this substitution to occur at the time that either signal 1 or 2 was received you can put the commands inside single quotes:
$ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2

Ignoring Signals:

If the command listed for trap is null, the specified signal will be ignored when received. For example, the command:
$ trap '' 2
Specifies that the interrupt signal is to be ignored. You might want to ignore certain signals when performing some operation that you don't want interrupted. You can specify multiple signals to be ignored as follows:
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is not equivalent to writing the following, which has a separate meaning of its own:
$ trap  2
If you ignore a signal, all subshells also ignore that signal. However, if you specify an action to be taken on receipt of a signal, all subshells will still take the default action on receipt of that signal.

Resetting Traps:

After you've changed the default action to be taken on receipt of a signal, you can change it back again with trap if you simply omit the first argument; so
$ trap 1 2
resets the action to be taken on receipt of signals 1 or 2 back to the default.