• 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.

Thursday, January 6, 2011

Posted by venu k
25 comments | 1:29 PM


#!/bin/bash
# SCRIPT : menu_dialog.sh
# PURPOSE : A menu driven Shell script using dialog utility
# which has following options:
# Display Today's Date and Time.
# Display calendar.
# Delete selected file from supplied directory.
# List of users currently logged in
# Disk Statistics
# Exit
#
##############################################################################
# Checking availability of dialog utility #
##############################################################################

# dialog is a utility installed by default on all major Linux distributions.
# But it is good to check availability of dialog utility on your Linux box.

which dialog &> /dev/null

[ $? -ne 0 ] && echo "Dialog utility is not available, Install it" && exit 1

##############################################################################
# Define Functions Here #
##############################################################################

###################### deletetempfiles function ##############################

# This function is called by trap command
# For conformation of deletion use rm -fi *.$$

deletetempfiles()
{
rm -f *.$$
}


######################## Show_time function #################################

# Shows today's date and time

show_time()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "DATE & TIME" \
--msgbox "\n Today's Date: `date +"%d-%m-%Y"` \n\n \
Today's Time: `date +"%r %Z"`" 10 60
}

####################### show_cal function ###################################

# Shows current month calendar

show_cal()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "CALENDAR" \
--msgbox "`cal`" 12 25
}

####################### deletefile function #################################

# Used to delete file under supplied directory, not including sub dirs.

deletefile()
{

dialog --backtitle "MENU DRIVEN PROGRAM" --title "Directory Path" \
--inputbox "\nEnter directory path (Absolute or Relative) \
\nPress just Enter for current directory" 12 60 2> temp1.$$

if [ $? -ne 0 ]
then
rm -f temp1.$$
return
fi

rmdir=`cat temp1.$$`

if [ -z "$rmdir" ]
then
dirname=$(pwd) # You can also use `pwd`
rmdir=$dirname/*
else

# remove trailing * and / from directory path

echo "$rmdir" | grep "\*$" &> /dev/null && rmdir=${rmdir%\*}
echo "$rmdir" | grep "/$" &> /dev/null && rmdir=${rmdir%/}

# Check supplied directory exist or not

( cd $rmdir 2>&1 | grep "No such file or directory" &> /dev/null )

# Above codeblock run in sub shell, so your current directory persists.

if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Validating Directory" \
--msgbox "\n $rmdir: No such file or directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

# Do you have proper permissions ?

( cd $rmdir 2> /dev/null )

if [ $? -ne 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: Permission denied to access this directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

if [ ! -r $rmdir ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: No read permission \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

dirname=$rmdir
rmdir=$rmdir/* # get all the files under given directory

fi

for i in $rmdir # process each file
do

# Store all regular file names in temp2.$$

if [ -f $i ]
then
echo " $i delete? " >> temp2.$$
fi

done

if [ -f temp2.$$ ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Select File to Delete" \
--menu "Use [UP/DOWN] keys to move, then press enter \
\nFiles under directory $dirname:" 18 60 12 \
`cat temp2.$$` 2> file2delete.$$
else
dialog --backtitle "MENU DRIVEN PROGRAM" --title "Select File to Delete" \
--msgbox "\n\n There are no regular files in $dirname directory" 10 60
return
fi

rtval=$?

file2remove=`cat file2delete.$$`

case $rtval in

0) dialog --backtitle "MENU DRIVEN PROGRAM" --title "ARE YOU SURE" \
--yesno "\nDo you Want to Delete File: $file2remove" 7 70


if [ $? -eq 0 ]
then
rm -f $file2remove 2> Errorfile.$$

# Check file successfully deleted or not.

if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : FILE DELETED" \
--msgbox "\nFile : $file2remove deleted" 8 70
else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : ERROR ON DELETION" \
--msgbox "\nProblem in Deleting File: $file2remove \
\n\nError: `cat Errorfile.$$` \n\nPress ENTER to return to the Main Menu" 12 70
fi

else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : DELETION ABORTED" \
--msgbox "Action Aborted: \n\n $file2remove not deleted" 8 70
fi ;;

*) deletetempfiles # Remove temporary files
return ;;
esac

deletetempfiles # remove temporary files
return
}

########################## currentusers function ############################

currentusers()
{
who > userslist.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "CURRENTLY LOGGED IN USERS LIST" \
--textbox userslist.$$ 12 60
}

############################ diskstats function #############################

diskstats()
{
df -h | grep "^/" > statsfile.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "DISK STATISTICS" \
--textbox statsfile.$$ 10 60
}

##############################################################################
# MAIN STRATS HERE #
##############################################################################

trap 'deletetempfiles' EXIT # calls deletetempfiles function on exit

while :
do

# Dialog utility to display options list

dialog --clear --backtitle "MENU DRIVEN PROGRAM" --title "MAIN MENU" \
--menu "Use [UP/DOWN] key to move" 12 60 6 \
"DATE_TIME" "TO DISPLAY DATE AND TIME" \
"CALENDAR" "TO DISPLAY CALENDAR" \
"DELETE" "TO DELETE FILES" \
"USERS" "TO LIST CURRENTLY LOGGED IN USERS" \
"DISK" "TO DISPLAY DISK STATISTICS" \
"EXIT" "TO EXIT" 2> menuchoices.$$

retopt=$?
choice=`cat menuchoices.$$`

case $retopt in

0) case $choice in

DATE_TIME) show_time ;;
CALENDAR) show_cal ;;
DELETE) deletefile ;;
USERS) currentusers ;;
DISK) diskstats ;;
EXIT) clear; exit 0;;

esac ;;

*)clear ; exit ;;
esac

done

OUTPUT:
[venu@localhost ~]$ sh menu_dialog.sh

SAMPLE SCREEN SHOTS:

Main Menu:
Users:

Disk Stats:

Delete File:



Monday, January 3, 2011

Posted by venu k
28 comments | 11:53 AM
   
Quicksort is a good example of the divide and conquer strategy for
solving problems. In quicksort, we divide the array of items to be
sorted into two partitions and then call the quicksort procedure recu-
rsively to sort the two partitions, ie we divide the problem into two
smaller ones and conquer by solving the smaller ones

Quicksort recursive algorithm:

1 Select an element (called as pivot) x(p) of x .
2 Divide x into two batches x1 and x2 so that
all entries of x1 are < x(p) and
all entries of x2 are > x(p).
all entries of x3 are == x(p)
for each recursive call x3 will be placed to its sorted position.
To finish sorting we must sort x1 and x2.
3 Apply steps 1 and 2 again to each of x1
and x2, using further subdivision.
4 Repeat (recursively) until the sets to be
sorted have no more than one element.

#!/bin/bash
# SCRIPT : quicksort.sh
# USAGE : quicksort.sh
# PURPOSE: Sorts the list using quicksort algorithm.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}
}

sortnumbers()
{

local array=( `echo "$@"` )
local -a l
local -a g
local -a e
local x=

if [ ${#array[@]} -lt 2 ]; then
echo -n ${array[@]}
else
local pivot=${array[0]}

for x in ${array[@]}
do

if [ $x -lt $pivot ]
then
l=( ${l[@]} $x )
elif [ $x -gt $pivot ]
then
g=( ${g[@]} $x )
else
e=(${e[@]} $x)
fi

done

echo "`sortnumbers "${l[@]}"` ${e[@]} `sortnumbers "${g[@]}"`"

fi
}

#####################################################################
# Variable Declaration #
#####################################################################

clear

echo "Enter Numbers to be Sorted : "

read -a ARRAY

count=${#ARRAY[@]}

#####################################################################
# Main Script Starts Here #
#####################################################################

echo "--------------------------------------------------------------"

echo "Numbers Before Sort:"

printnumbers

echo "Numbers After Sort: "

sortnumbers "${ARRAY[@]}"

echo "--------------------------------------------------------------"


OUTPUT:

[venu@localhost shell]$ sh quicksort.sh
Enter Numbers to be Sorted :
12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9
---------------------------------------------------------------
Numbers Before Sort:
12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9
Numbers After Sort:
-46 -11 -8 0 5 9 12 21 32 32 33 54 54 76 84 87 90 222 321
---------------------------------------------------------------
Posted by venu k
8 comments | 11:23 AM
 Binary search works by comparing an input value to the middle element
of the array. The comparison determines whether the element equals the
input, less than the input or greater. When the element being compared
to equals the input the search stops and typically returns the posit-
ion or number of searches of the element. If the element is not equal
to the input and the element at the middle point is greater than the
input being searched, the current middle point becomes the new high
point and the array is cut in half again and re-tested. If the element
at the middle point is less than the input being searched, the current
middle point becomes the new low point and the array is cut in half
again and retested. This cutting in half and adjusting either the high
point or the low point is repeated until the item is found or the low
point and the high point converge. This is much faster then sequentia-
lly searching an entire array.

#!/bin/bash
# SCRIPT : binarysearch.sh
# USAGE : binarysearch.sh
# PURPOSE: Searches given number in a sorted list.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}
}

sortnumbers() # Using insertion sort
{
for((i=1;i<count;i++))
do
Temp=${ARRAY[i]}
j=$((i-1))
while [ $Temp -lt ${ARRAY[j]} ]
do
ARRAY[j+1]=${ARRAY[j]}
let j--
if [ $j == -1 ]
then
break
fi
done
ARRAY[j+1]=$Temp
done
}

binarysearch()
{
status=-1
i=1
array=($(echo "$@"))
LowIndex=0
HeighIndex=$((${#array[@]}-1))

while [ $LowIndex -le $HeighIndex ]
do

MidIndex=$(($LowIndex+($HeighIndex-$LowIndex)/2))
MidElement=${array[$MidIndex]}

if [ $MidElement -eq $SearchedItem ]
then
status=0
searches=$i
return
elif [ $SearchedItem -lt $MidElement ]
then
HeighIndex=$(($MidIndex-1))
else
LowIndex=$(($MidIndex+1))
fi

let i++

done
}

#####################################################################
# Variable Declaration #
#####################################################################

clear

echo "Enter Array Elements : "

read -a ARRAY

count=${#ARRAY[@]}

search=y

#####################################################################
# Main Script Starts Here #
#####################################################################

# sort the loaded array, must for binary search.
# You can apply any sorting algorithm. I applied insertion sort.

sortnumbers

echo "Array Elements After Sort: "

printnumbers

while [ "$search" == "y" -o "$search" == "Y" ]
do

echo -n "Enter Element to be searched : "
read SearchedItem
binarysearch "${ARRAY[@]}"

if [ $status -eq 0 ]
then
echo "$SearchedItem found after $searches searches"
else
echo "$SearchedItem not found in the list"
fi

echo -n "Do you want another search (y/n): "
read search

done

OUTPUT:

[venu@localhost shell]$ chmod 755 binarysearch.sh
[venu@localhost shell]$ ./binarysearch.sh
Enter Array Elements :
12 34 56 21 43 11 -32 87 112 -43 -111 98 100 22 0 11
Array Elements After Sort:
-111 -43 -32 0 11 11 12 21 22 34 43 56 87 98 100 112
Enter Element to be searched : 21 # Middle element
21 found after 1 searches
Do you want another search (y/n): y
Enter Element to be searched : 112
112 found after 5 searches
Do you want another search (y/n): y
Enter Element to be searched : 56 # second middle element(upper)
56 found after 2 searches
Do you want another search (y/n): y
Enter Element to be searched : 0 # second middle element(lower)
0 found after 2 searches
Do you want another search (y/n): y
Enter Element to be searched : -111
-111 found after 4 searches
Do you want another search (y/n): n
Posted by venu k
11 comments | 3:28 AM

#!/bin/bash
# SCRIPT : linearsearch.sh
# USAGE : linearsearch.sh
# PURPOSE: Searches given number in a list.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
# A variation of Here Document permits "commenting out" data block.
: <<DATABLOCK

In computer science, linear search or sequential search is a method
for finding a particular value in a list, that consists in checking
every one of its elements, one at a time and in sequence, until the
desired one is found.

This is a very straightforward loop comparing every element in the
array with the key. As soon as an equal value is found, it returns.
If the loop finishes without finding a match, the search failed and
-1 is returned.

For small arrays, a linear search is a good solution because it's so
straightforward. In an array of a million elements, a linear search
will take,on average, 500,000 comparisons to find the key. For a much
faster search, take a look at binary search.

DATABLOCK

#####################################################################
# Define Functions Here #
#####################################################################

lsearch()
{
status=-1

for((i=0;i<count;i++))
do
Temp=$1

if [ $Temp -eq ${ARRAY[i]} ]
then

status=0
searches=$((i+1))
return

# return $((i+1))
# Bash function can return value between 0-255, That's why I assigned
# result to a global variable. This is one of the method to capture
# return value of a function.

fi

done
}

#####################################################################
# Variable Declaration #
#####################################################################

clear

echo "Enter Array Elements : "

read -a ARRAY

count=${#ARRAY[@]}

search=y

#####################################################################
# Main Script Starts Here #
#####################################################################

while [ "$search" == "y" -o "$search" == "Y" ]
do

echo -n "Enter element to be searched : "
read num
lsearch $num

if [ $status -eq 0 ]
then
echo "$num found after $searches searches"
else
echo "$num not found"
fi

echo -n "Do you want another search (y/n): "
read search

done

OUTPUT:
$ sh linearsearch.sh
Enter Array Elements :
12 34 56 78 90 23 45 56 67 321 66 88 92
Enter element to be searched : 56
56 found after 3 searches
Do you want another search (y/n): y
Enter element to be searched : 321
321 found after 10 searches
Do you want another search (y/n): y
Enter element to be searched : 100
100 not found
Do you want another search (y/n): n

Sunday, January 2, 2011

Posted by venu k
14 comments | 8:19 AM

Wish You a Happy New Year To All My Friends




If you don't know shell color codes, read bellow article first.
How to colorize shell scripts

#!/bin/bash
# SCRIPT: happynewyear.sh
# PURPOSE: Prints Happy new year 2011 on screen.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
##############################################################################
# Arguments Checking #
##############################################################################

if [ $# -eq 1 ]
then
fname=$1
else
echo "Invalid Arguments"
echo "Usage: happynewyear.sh filename"
exit
fi

##############################################################################
# Main Starts #
##############################################################################

clear
i=0

while read line
do

if [ $i -lt 8 ] ; then
COLOR1="\e[1;31;40m" ; COLOR2="\e[0m" # Dark Red
elif [ $i -gt 8 -a $i -lt 17 ] ; then
COLOR1="\e[1;37;40m" ; COLOR2="\e[0m" # White
elif [ $i -gt 17 -a $i -lt 26 ] ; then
COLOR1="\e[1;32;40;5m" ; COLOR2="\e[0m" # Dark Green with blinking
fi # effect


newline=`echo "$line" | tr '69' ' @' `
printf "$COLOR1$newline $COLOR2" # you can also use echo -e "...."
let i++
sleep 0.1


done < $fname

read # Waits for enter



Copy bellow data and save it as newyear.txt

6666666666996666699666666666966666666669999999996699999999966996666669966666666
6666666666996666699666666669996666666669999999996699999999966699666699666666666
6666666666996666699666666699669966666669966666996699666669966669966996666666666
6666666666999999999666666996666996666669999999996699999999966666999966666666666
6666666666999999999666669999999999666669999999996699999999966666699666666666666
6666666666996666699666699999999999966669966666666699666666666666699666666666666
6666666666996666699666996666666666996669966666666699666666666666699666666666666
6666666666996666699669966666666666699669966666666699666666666666699666666666666
6666666666666666666666666666666666666666666666666666666666666666666666666666666
9966666996699999999669966666996666699666666996999999996666666696666666699999999
9996666996699999999669966666996666669966669966999999996666666999666666699999999
9999666996699666666669966666996666666996699666996666666666669969966666699666699
9969966996699999999669966966996666666699996666999999996666699666996666699999999
9966996996699999999669969996996666666669966666999999996666999999999666699999999
9966699996699666666669999699996666666669966666996666666669999999999966699699666
9966669996699999999669996669996666666669966666999999996699666666666996699669966
9966666996699999999669966666996666666669966666999999996996666666666699699666699
6666666666666666666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666699999999666999699999666999666699966666666666666666666666
6666666666666666666666699999999666996666699666699666669966666666666666666666666
6666666666666666666666666666996666999666699666699666669966666666666666666666666
6666666666666666666666666669966666996696699666699666669966666666666666666666666
6666666666666666666666666699666666996669699666699666669966666666666666666666666
6666666666666666666666669966666666996666999666699666669966666666666666666666666
6666666666666666666666699999999666996666699666699666669966666666666666666666666
6666666666666666666666699999999666999699999666999966699996666666666666666666666



For blinking effect run this script in console, not in terminal.

OUTPUT:
$ sh happynewyear.sh newyear.txt