Labels

Friday 26 October 2012

UNIX - All about Positional Parameters

In this article we are going to see about Unix - Positional Parameters. 'Positional Parameters' are none other than 'command-line arguments' in any other languages.

Do remember the special variables before jump into the script:
$0 - Holds the filename or name of the script
$* - All the command line arguments supplied to the script
$@ - It is same as $* but differs when the arguments are explicitly enclosed with quotes
$# - No of arguments passed to the script
$1 - To get the value of first argument
${N} - To get the 'N'th element. For ex, to get 11th parameter, use ${11}
shift - To remove the first element from the argument list 

If you want to practice yourself before writing the shell script, you can run below commands in the shell prompt itself:

$ set one two three four
$ echo $3

three
$ echo $#
4
$ echo $*
one two three four
$ shift
$ echo $*
two three four

Same thing but here you have done it through the script by putting together all commands you were executed previously in the shell prompt:

#!/usr/bin/bash 
echo "No of command line arguments \$#:" $#
echo "Parameters are \$*" $*
echo "Use of '\$@':" $@
echo "File Name \$0: " $0
echo "First parameter is \$1: " $1
echo "To get 10th params \${10}:" ${10}

# To handle Unknown number of parameters
for PARA in $*
do
        echo $PARA
done

Run the script:
$ sh cmdargs.sh one two three four five six seven eight nine ten eleven

Output:
No of command line arguments $#: 11
Parameters are $* one two three four five six seven eight nine ten eleven
Use of '$@': one two three four five six seven eight nine ten eleven
File Name $0:  cmdargs.sh
First parameter is $1:  one
To get 10th params ${10}: ten
one
two
three
four
five
six
seven
eight
nine
ten
eleven

Wednesday 24 October 2012

SQL - 'COUNT' with 'DISTINCT'


Consider you have an employee table which contains the following records:

Select * from emp
empname
empid
sal
mgr_id
sundar
101
100
505
sund1
102
100
506
sund2
103
10
507
sund3
104
1000
103
sund4
105
2000
509
Sundar1
NULL
NULL
NULL
Sundar2
NULL
NULL
NULL

And of course, you very well knew that count(*) returns the number of records in a table.
For example: Select count(*) from emp .
It returns ‘7’

Suppose if you want to get the Unique number of records in a column of table, you may use distinct.
Select distinct(empid) from emp
But problem here is that, distinct consider ‘Null’ values as well. So it returns below 6 records.
empid
101
102
103
104
105
NULL

The best way to find out the unique records in a particular column, use 'count' instead of 'distinct'.
For example:
Select count(empid) from emp
It returns ‘5’ and ignores ‘Null’ values.


Saturday 13 October 2012

Unix - Hardlink Vs Softlink

Hardlink Vs Softlink

    This is very classic question which you can expect in almost all interviews. Links are basically like creating shortcuts in Windows Operating System. Links will come to rescue you when you want to migrate from one directory to another directory without changing the file location in the actual script.

Hard link generally created by 'ln' command. Syntax is,
$ ln <existing_filename> <hardlink_name>
Example:
$ln fnamex.txt fhardlink.txt

Soft links are also created by same ln command with additionally supplying the options -s . For ex
$ln -s fnamex.txt fsoft.txt

Now run the ls command (see below image) to list out the files and observe the differences. Note: '-i' option for displaying the inode number of the file. To know more about inode refer :
http://en.wikipedia.org/wiki/Inode

 

You could have observed the following differences:
- All hard links share the same inode number as of original file whereas soft links have different inode
- Soft links can be identified with "l" followed by file permissions
- Hard links have the file size same as Original file. So even after deleting the original file, you can still access with hard link name. Softlinks are useless if the original file deleted. Also note that softlink occupy only few bytes to store file attributes (not actual file content).
- As we created 3 hard links for the same file, count shows 4 s (including the original file count) for hard link. It is not the same for softlinks and it always has "1" as count.

Other major difference that, using hard link you cannot link directories in two different file system. For ex,



 But it will work for softlink.


Hope I have covered most of the important differences. Let me know if any questions. Thanks !!