Tuesday, 28 August 2012

Different ways of running a shell script…

We hardly care about how a shell script executes, the only thing which we care about is that scripts should execute without any bugs. Here, we are going to discuss about some ways of running a script.
Before seeing the ways of running a shell script, let me clear a question. What is a Shell Script?
We all execute commands on shell. A shell is a program which provides us the environment to execute commands. This also means that we need to have shell running before we start running commands. Now, a shell script is nothing but collection of commands. We club some commands in a file and then ask the interpreter to run the file. This file, where we club our commands is known as interpreter file. Please note that it is not the interpreter file which executes, but the contents of the interpreter file is executed by interpreter, and interpreter is nothing but the shell. Not going in much details into shell and interpreters, we assume that now we know that a shell or interpreter is very necessary for execution of Shell Scripts.
Let's see the ways of running a script
1) /bin/sh testscript.sh
This is the first way in which we specify that we want to run the script using bash shell. Now, the main shell will fork a new shell and that subshell/interpreter will execute the shell script.
Note: We will not be able to access any variables from parent shell unless they are exported.
2) ./testscript.sh or {full-path}/testscript.sh
This is the second way in which we don't specify the shell name. Infact, we write a special line at the top of the interpreter file
#! /bin/sh
# in shell script means that the line is a comment, but # in the first line defines the interpreter to be used for running the script. This line is known as shebang line.
Note: If we don't specify shebang line in this format of running the script then by default, the executing shell will be of same type as the user's login shell. For ex: If the user's default login shell is ksh then ksh shell will be used for running the script.
It is necessary to give relative or full path of script, if we type only script name on the command line, the shell will treat it as a command and start searching in the PATH for the executable file of this command. We can set PATH variable to contain the script path if want to run the script using the script name only.
3) . ./testscript.sh
If we leave first dot, it is same as the second one, then what change does this first dot brings?
Here first dot means script will be run using the same/current shell, i.e. don't fork a new subshell for running the script. The script will be able to use all the variables of the current shell because it is running on the current shell.

For more info visit

Monday, 20 August 2012

How to use TO_NUMBER() and TO_CHAR() to get Number/String in a given format in PL/SQL?

 TO_NUMBER: The structure of TO_NUMBER function is shown below.

TO_NUMBER(string,[format],[NLS params])

The parameters shown in [] are optional. We can simply use TO_NUMBER to convert a string to number as shown below.

TO_NUMBER('32392.849');

This output of the above function call will be 32392.849. But, what about the case when your string contains separators such as , .

Suppose a string 323,567,897.90. Now you want to convert this string into a number. Straightaway using the above function will give an error. This string can be converted into a number as shown below.

TO_NUMBER('323,567,897.90' , '999G999G999D99');

Here G is the second character in NLS_NUMERIC_CHARACTERS set of a session and D is the first character.

Run the following query to see the NLS_NUMERIC_CHARACTERS set of your session

Select * from NLS_SESSION_PARAMETERS.

You can modify the NLS_NUMERIC_CHARACTERS set in your session or pass the custom NLS_NUMERIC_CHARACTERS as the third parameter of TO_NUMBER() function. Now, the output of the above query will be big, but we will see the line of output which is useful to us.

NLS_NUMERIC_CHARACTERS .,

So, if G is the second character then it denotes , , and if D is the first character then it denotes . .

You can pass NLS_NUMERIC_CHARACTERS in the TO_NUMBER() function as follows.

TO_NUMBER('323.567.897,90' , '999G999G999D99' , 'NLS_NUMERIC_CHARACTERS='',.''');

Now, D will denote , ,and G will denote . .

Currency specifier can also be passed into the format as shown below.

TO_NUMBER('$323,567,897.90' , 'L999G999G999D99');

The default currency in NLS_SESSION_PARAMETERS is $. Again, you can pass your own currency as the third parameter of TO_NUMBER function.

NOTE: We cannot have more digits in first parameter than specified in the second parameter of the TO_NUMBER function, both to the left as well as right hand side of decimal.

Now, let's see how to use TO_CHAR function to convert a number into string. The format is same as TO_NUMBER function.

Suppose a number 323567897.90. Now we want to convert this number to String in a given format

TO_CHAR(323567897.90 , 'L999G999G999D99')

and the output will be $323,567,897.90.

Also, there is a minor difference between TO_CHAR and TO_NUMBER. TO_NUMBER doesn't allow you to have more than specified number of digits in the second parameter either on left or right side of decimal in the first parameter. But in TO_CHAR, if you have more number of digits to the right side of decimal than specified in the format, it will round it off, for ex.

TO_CHAR(323567897.83 , 'L999G999G999D9') is perfectly normal in TO_CHAR.

You can always have more digits in format specifier(arg 2) than the actual number(arg1) in either of the function.

THE V FORMAT ELEMENT

Below is the example for V format element.

TO_CHAR(323567897.83 , 'L999G999G999V999')

The output will be $323,567,897830. Did you get the pattern? Overlay arg2 on arg1 with V taking the place of decimal and append extra 0s, if required, on the right side of decimal.

You can always go for various combinations of format specifier and get interesting results.

For more info visit