• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Sunday, August 23, 2009

Posted by venu k
9 comments | 7:10 AM
Hard link: Hard link refers to "The specific location of physical data".
  • Hard Link is a mirror copy of the original file.
  • Hard links share the same inode.
  • Any changes made to the original or Hard linked file will reflect the other.
  • Even if you delete any one of the files, nothing will happen to the other hard links.
  • But soft link which points to deleted hard link become a dangling soft link.
  • You can't link a directory even within the same file system.
  • Hard links can't cross file systems.
Soft link( also called symbolic link): Soft link refers to "A symbolic path indicating the abstract location of another file".
  • Soft Link is a symbolic link to the original file.(more like windows shortcuts)
  • Soft Links will have a different Inode value.
  • Any changes made to the soft link will reflect the original file and its hard links.
  • A soft link points to the original file. If you delete the original file, the soft link fails. It would become dangling symbolic link.
  • If you delete the soft link, nothing will happen.
  • You can link a directory using soft link on same file system and also on other file system.
  • Soft links can cross file systems

Lets learn the difference with an example:

First create a file named with "mainfile.txt"

Now create a hard link named with "hardlink.txt"

From the above output you can find inode number of both files are equal.

Now create a soft link named with "softlink.txt"

Now you find that inode value is different for the soft link and main file.

But equal for the hard link and main file.

Contents of all files before editing


Now lets try to edit main file "mainfile.txt"

From the above output it clarifies that, changes of main file reflects its soft and hard links.

Note: Permission changes of original file reflects only on hard links. Its soft links permission remains unchanged.

Now lets remove main file "mainfile.txt"


So removing the original file will affect the Soft link. The Soft link fails.Now it become a dangling symbolic link(or broken link).Hard link is unaffected.

Now create main file "mainfile.txt" again and make its links as it is before it is

deleted.

let now try to remove soft link "softlink.txt".

It clarifies that deletion of soft link will not affect the main file and its hard links.

Now lets try to edit hard link "hardlink.tst".Before that create soft link "softlink.txt" again.

From the above output its clear that changing the contents of the hard link will reflects on main file and also reflect on soft link of main file.

Now lets try to edit the soft link "softlink.txt".


From the above output its clear that changing the contents of soft link will reflects on main file and also on all links of main file.

Now lets try to remove main file then edit its soft link.

Some strange result.

Soft link creates its main file.But it will not retain main file contents. It merely creates main file "mainfile.txt" with data what you inserted in "softlink.txt".

Now hard link "hardlink.txt" will not have any relation with main file "mainfile.txt".Both files now have unique inode value.

Now lets give a look on directories. How soft links and hard links behaves with directories:

Now create a directory named with temp and create some files in that directory.

Now try to create hard link and soft link for directory temp.


Above example clarifies that it's not possible to create hard link on directory but it is possible to create soft link.

Note:

Like other files, a symbolic link has a separate directory entry with its own inode number. This means that rm can remove a symbolic link even if its points to a directory(or even a device).

So lets try to remove symbolic link of a directory.In our above example "softtemp" is a symbolic link for directory temp.

So it clears that we can remove directory symbolic link with rm command just like a file. But don't add "/" at the end of link(If you use tab to complete file name at command prompt it automatically adds / at the end of link if it points to a directory).

Observations:

  • You can link multiple files (i.e., create a link for each), but then the destination filename must be a directory.

Ex: ln chap?? project project is a directory

  • When you create hard link, The link count of file increases one. So based on link count you can find how many instances of a file exist in your system.
  • So a file is considered to be completely removed from the system when its link count drops to zero
  • Many UNIX/Linux commands are linked.

Ex:

[root@localhost ~]# ls -li /usr/bin/gzip
499116 lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip ->../../bin/gzip

  • If you want to create symbolic link of a file in other directory your source file path must be absolute.
  • You can identify symbolic links by the character "l" seen in the permission field.
  • Observe that the size of the symbolic link is equal to length of the path name it contains.
  • To view the permissions of the file or directory that the symbolic link references, use L option with ls -l (i.e. ls -lL )

Ex: [root@localhost scripts]# ls -l /usr/bin/gzip
lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip -> ../../bin/gzip
[root@localhost scripts]# ls -lL /usr/bin/gzip
-rwxr-xr-x 1 root root 64524 2008-02-22 02:55 /usr/bin/gzip


Friday, August 14, 2009

Posted by venu k
3 comments | 11:43 PM
#!/bin/bash
#**********************************************************************************************
# gcd.sh: greatest common divisor uses Eclidean algorithm
# Usage : gcd.sh num1 num2 num3 .... (any number of arguments)
# The algorithm used to calculate the GCD two integers is known as the Euclidean algorithm.
# Based on Euclidean algorithm this script is written(Recursive method).
# For checking supplied arguments are integers or not check GCD of two numbers code
#***********************************************************************************************

# Argument check
# Minimum 2 arguments you should to supply
ARGS=2
BADARGS=65

if [ $# -lt "$ARGS" ]
then
echo
echo "Invalid Arguments"
echo "Usage: $0 first-number second-number"
echo
exit $BADARGS
fi
# Preserve command line argument for future use
cmdargs=$*

function Euclidean()
{
if [ $2 -eq 0 ]
then
return $1
else
Euclidean $2 $(($1%$2)) # calling function recursively
fi
}
Euclidean $1 $2
return=$?
# $? returns the exit status of script. This is one method to capture return value of a function

shift
# Shifts command line arguments one step.Now $1 holds second argument

while true
do
shift
# shift is used to pick up next command line argument to continue iteration
# $# holds total number of arguments.At every shift operation its value decreases one

if [ $# -eq 0 ]
then
break 2
fi
Euclidean $return $1
return=$?
done
echo "GCD of $cmdargs is $return"
exit 0

Thursday, August 13, 2009

Posted by venu k
4 comments | 10:35 AM
I written three different methods to calculate GCD of two numbers

Method 1:


#!/bin/bash
#*******************************************************************************************
# gcd.sh: greatest common divisor uses Eclidean algorithm
# Usage : gcd.sh number1 number2
# The algorithm used to calculate the GCD between two integers is known as the Euclidean
# algorithm(Recursive method).
#*******************************************************************************************

#----------------------------------------------------------------------
# Argument check
ARGS=2
BADARGS=65

if [ $# -ne "$ARGS" ]
then
echo ; echo "Invalid Arguments"
echo "Usage: $0 first-number second-number" ; echo
exit $BADARGS
fi

# Check supplied arguments are integers are not

isnumber()
{
if [ $1 -eq $1 2> /dev/null ]
then
:
# : is a null command.This is the shell equivalent of a "NOP"(do nothing)
else
echo -e "\nYou supplied one bad argument \"$1\" is not a number"
echo -e "Usage: $0 first-number second-number\n"
exit $BADARGS
fi
}
isnumber $1
isnumber $2

# ---------------------------------------------------------------------

function Euclidean()
{
if [ $2 -eq 0 ]
then

return $1

else

Euclidean $2 $(($1%$2))
# calling function recursively
fi
}

Euclidean $1 $2

echo "gcd of $1 and $2 is $?"

# $? returns the exit status of script. This is one method to capture return value of a function

exit 0

Method 2:


#!/bin/bash
# gcd2.sh
# Usage: gcd2.sh number1 number2
# For argument check see method 1

gcd ()
{
dividend=$1
divisor=$2
# Arbitrary assignment.
#! It doesn't matter which of the two is larger.

remainder=1

# If uninitialized variable used in loop,it results in an error message
# on the first pass through loop.

if [ $divisor -eq 0 ]
then
echo "GCD of $dividend and $divisor = $dividend"
exit 0
fi

until [ "$remainder" -eq 0 ]
do
let "remainder = $dividend % $divisor"

# Now repeat with 2 smallest numbers

dividend=$divisor .
divisor=$remainder
done
}
# Last $dividend is the gcd.

gcd $1 $2

echo; echo "GCD of $1 and $2 = $dividend"; echo
exit 0

Method 3:


#!/bin/bash
# gcd3.sh
# Usage: gcd3.sh
# This script doesn't use command line arguments.You should to enter two numbers when asking

echo enter two numbers
read n1 n2
remainder=1
# Preserve numbers for future use
t1=$n1
t2=$n2
if [ $n2 -eq 0 ]
then
echo "GCD of $n1 and $n2 = $n1"
exit 0
fi
while [ $remainder -ne 0 ]
do
remainder=`expr $n1 % $n2`
# or use
#remainder=$((n1%n2))
n1=$n2
n2=$remainder
done
echo "GCD of $t1 , $t2 is $n1"

Monday, August 10, 2009

Posted by venu k
5 comments | 8:57 AM
#************************************************************************************
#Locking a terminal using a shell script.
#This script locks your terminal until you enter correct password (open2world)
#It also traps signals and interrupts
#You can't terminate the script by Ctrl+c or Ctrl+\ or Ctrl+z and Ctrl+d
#************************************************************************************

clear
trap "" 1 2 3 20
lines=`tput lines`
b=`expr $lines / 2 - 4 `

## center function

center()
{
columns=`tput cols`
until [ -z "$1" ]
do
for ((i=1;i<$(((columns-${#1})/2));i++)) do echo -n " " done echo -e "\033[1m \033[5m \033[42m $1 \033[0m " shift done } ## End of center function while true do clear tput cup $b 0
center " TERMINAL LOCKED "
center "Press any key to unlock"
read key
echo -n "enter password : "
read -s password
while true
do
if [ $password = "open2world" ]
then
clear
break 2
# breaks second outer loop
else
echo -e "\n You are an illegal user "
echo -e "\n Enter any key"
read
break
fi
done
done

Analysis :


The trap command allows you to execute a command when a signal is received by your script. It works like this:

trap arg signals

"signals" is a list of signals to interrupt and "arg" is a command to execute when one of the signals is received. If "arg" is not supplied script doesn't do any thing after receiving signal.

Note: Two signals "SIGKILL" "SIGSTOP" are unable to trap

Ex: trap "echo signal received" 1 2 3 15
(or use SIGHUP SIGINT SIGQUIT SIGTERM instead of 1 2 3 15 )

What is signal:


A signal is a message which can be sent to a running process. Some times called
software interrupts.

Some of signals and its value:

       Signal             Value     Action   Comment
-------------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT (Ctrl+c) 2 Term Interrupt from keyboard
SIGQUIT (Ctrl+\) 3 Core Quit from keyboard
SIGTERM 15 Term Termination signal
SIGTSTP (Ctrl+z) 20 Stop Stop typed at tty


Note: There are different standards(POSIX,SUSv) to determine signals and its values.
I tested Above signals in Fedora 9, Other systems may support them or not depending on system standard(I think almost all standards supports above signals and values).

More stuff:


I used "tput cols" and "tput lines" commands to collect number of columns and lines.
You can also directly use bash variables $LINES ,$COLUMNS to get columns and lines.But
you should to run your script in current shell only. If you run your script in sub shell
current shell will not export $LINES and $COLUMNS variable values.