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

Friday, November 18, 2011

Posted by venu k
53 comments | 9:37 AM

Note: I have used my device name and number in examples, you have to change it
as your device name and number, otherwise unwanted device data will be wiped
out.

Put your USB flash drive in one of your computer's free USB ports. After a few
seconds, Linux will automatically recognize it and an icon for it will appear on
your desktop. If pen drive doesn't mounted automatically, login as root and try
to mount it, still it is not mounted there may be a problem with pen drive or it
is defective.

Finding device:


Very very first thing is you should to know what device you are working with.
For that you can use any one of the following methods.

Method 1: Using ls /dev/sd* command

First make sure your usb drive is not plugged in. Then run:

[venu@ras ~]$ ls /dev/sd*
/dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8
/dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9

Once you have done that, put in your pen drive and run the above command again.

[venu@ras ~]$ ls /dev/sd*
/dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8 /dev/sdb
/dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9 /dev/sdb1

Whatever drive (ex sdb) was not there before is your pen drive. From the above
list my pen drive is sdb. Don't be confuse with /dev/sdb and /dev/sdb1, sdb is
a drive and sdb1 is first partition in that drive.

Method 2: Using df command

[venu@ras ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda10 29753556 8008860 20208884 29% /
/dev/sda12 19840892 1528664 17288088 9% /msfiles
/dev/sda11 21825108 16772836 3925716 82% /venu
tmpfs 1032232 0 1032232 0% /dev/shm
/dev/sdb1 1947636 4 1947632 1% /media/VENU

As you can see above my pen drive (/dev/sdb1) is mounted at /media/VENU. If you
have any doubt about which one is your pen drive, do same thing as in method1.
Run df command before and after inserting pen drive.

Method 3: dmesg | tail

[venu@ras ~]$ dmesg | tail
sdb: assuming drive cache: write through
SCSI device sdb: 3903359 512-byte hdwr sectors (1999 MB)
sdb: Write Protect is off
sdb: Mode Sense: 0b 00 00 08
sdb: assuming drive cache: write through
sdb: sdb1
sd 8:0:0:0: Attached scsi removable disk sdb
sd 8:0:0:0: Attached scsi generic sg1 type 0
usb-storage: device scan complete
SELinux: initialized (dev sdb1, type vfat), uses genfs_contexts

Method 4: mount -l

[venu@ras ~]$ mount -l
/dev/sda10 on / type ext3 (rw) [/]
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda12 on /msfiles type ext3 (rw) [/msfiles1]
/dev/sda11 on /venu type ext3 (rw) [/venu1]
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /media/VENU type vfat (rw,nosuid,nodev,shortname=winnt,uid=500)
[VENU]

Method 5: fdisk -l

fdisk command need root privileges. So login as root and run the command:

[root@ras ~]# fdisk -l
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 5099 40957686 7 HPFS/NTFS
/dev/sda2 5100 60800 447418282+ f W95 Ext'd (LBA)
/dev/sda5 5100 10198 40957686 7 HPFS/NTFS
/dev/sda10 50994 54817 30716248+ 83 Linux
/dev/sda11 54818 57622 22531131 83 Linux
/dev/sda12 57623 60172 20482843+ 83 Linux
/dev/sda13 60173 60694 4192933+ 82 Linux swap / Solaris

Disk /dev/sdb: 1998 MB, 1998519808 bytes
32 heads, 63 sectors/track, 1936 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 1936 1951456+ b W95 FAT32

fdisk -l displays all detected drives even drive is unmounted.

Formatting pen drive:


First decide which file system you want. FAT32 is probably the file system you
want, but that's up to you. Ext3 may not be work on windows, It asks for fomatt-
ing.

1.Once you have Identified your pen drive, unmount it.

[root@ras ~]# umount /dev/sdb1

2.Format pen drive now.

Depending on distribution different commands are available for formatting, but
in general most used ones that I have seen are mkdosfs or mkfs.vfat for FAT32
and mke2fs or mkfs.ext3 for Linux filesystems.

For Ms-Dos or Windows file system:

Use mkdosfs -n "Label" -I /dev/sdx, Replace Label with the name you want the
pen drive to have and /dev/sdx with your drive.

[root@ras ~]# mkdosfs -n "KVMREDDY" -I /dev/sdb1
mkdosfs 2.11 (12 Mar 2005)
or
[root@ras ~]# mkfs.vfat -n "KVMREDDY" -I /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)

(Note: I – capital letter of i )

If you don't want any Label then no need to supply option n. switch I will
force mkdosfs to work properly but it is not mandatory, you can remove that
also.

For Linux File system:

Use mkfs.ext3 -L "Label" /dev/sdx for ext3 file system or mke2fs -L 'Label'
/dev/sdx
for ex2, Replace Label with the name you want the pen drive to have
and /dev/sdx with your drive.

Ext3 file system:

[root@ras ~]# mkfs.ext3 -L "KVMREDDY" /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=KVMREDDY
OS type: Linux
.............................
[root@ras ~]#

ext2 file system:

[root@ras ~]# mke2fs -L 'VENU' /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=VENU
OS type: Linux
...........................
[root@ras ~]#

After formatting you will be returned to the prompt then remove and insert the
pen drive to have mounted again.

Saturday, October 8, 2011

Posted by venu k
34 comments | 4:45 AM

 Public keys can be erased after importing just like packages. Here's how to
remove the CentOS GPG public key:

rpm -e <GPG public key>

[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -qa gpg-pubkey*
[root@localhost]#

If there are duplicates exist:

For example, Import the same key two times
[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
gpg-pubkey-e8562897-459f07a4
Now try to remove Public Key, You will get error message like this:
[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4
error: "gpg-pubkey-e8562897-459f07a4" specifies multiple packages

How to solve this:

Use --allmatches switch along with -e, It removes all versions of the package which match PACKAGE_NAME(Public Key ).
[root@localhost]# rpm -e --allmatches gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -qa gpg-pubkey*
[root@localhost]#

Friday, October 7, 2011

Posted by venu k
37 comments | 12:21 PM

Note: All the commands tested on CentOs 5.x. Your output may be vary depending
on distribution and version, so your results may not always look exactly like
the listings and figures shown here. Almost all everything works well on
RHEL/CentOs/Fedora.

Why to check signature of an rpm:

The signature confirms that the package was signed by an authorized party and also confirm the integrity and origin of your file. It is extremely important to verify the signature of the RPM files before installing them to ensure that they have not been altered from the original source of the packages.

Checking a package's Signature:

The --checksig(or -K) option checks all the digests and signatures contained in PACKAGE_FILE to ensure the integrity and origin of the package. Note that signatures are now verified whenever a package is read, and --checksig is useful to verify all of the digests and signatures associated with a package. If you wish to verify that a package has not been corrupted or tampered with, examine only the md5sum by typing the following command at a shell prompt (where <rpm-file> is the file name of the RPM package): rpm -K --nosignature <rpm-file> The message <rpm-file>: md5 OK is displayed. This brief message means that the file was not corrupted by the download. To see a more verbose message, replace -K with -Kvv in the command. For demonstration purpose I downloaded createrepo package from CentOs mirror and used in examples.
[root@localhost ~]# rpm -K --nosignature createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: sha1 md5 OK
On the other hand, how trustworthy is the developer who created the package? If the package is signed with the developer's GnuPG key,you know that the developer really is who they say they are. An RPM package can be signed using Gnu Privacy Guard (or GnuPG), to help you make certain your downloaded package is trustworthy. GnuPG is a tool for secure communication; it is a complete and free replacement for the encryption technol- ogy of PGP, an electronic privacy program. With GnuPG, you can authenticate the validity of documents and encrypt/decrypt data to and from other recipients. GnuPG is capable of decrypting and verifying PGP 5.x files as well. During installation,GnuPG is installed by default. That way you can immediately start using GnuPG to verify any packages that you receive from CentOs(RHEL/Fedor a). Before doing so, you must first import CentOs's public key. If you not impo- rted correct public key, you will get following error message.
[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: (SHA1) DSA sha1 md5 (GPG) NOT OK (MISSING
KEYS: GPG#e8562897)
Here the GPG in parentheses indicates that there's a problem with the signature, and the message at the end of the line (MISSING KEYS) shows what the problem is. Basically, RPM asked GPG to verify the package against a key(GPG#e8562897) that GPG didn't have, and GPG complained. It means you missed the correct public key.

How to import public keys:

Digital signatures cannot be verified without a public key. An ascii armored public key can be added to the rpm database using --import. An imported public key is carried in a header, and key ring management is performed exactly like package management. For example, all currently imported public keys can be displayed by: rpm -qa gpg-pubkey* To verify CentOs (RHEL/Fedora) packages, you must import the CentOs(RHEL/Fedora) GPG key. To do so, execute the following command at a shell prompt:
[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
This will create duplicate copies if one already exists. To display a list of all keys installed for RPM verification,execute the command
[root@localhost ~]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
or RPM has the capacity to retrieve the key from a Mirror:
[root@ ~]# rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[root@ ~]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
gpg-pubkey-e8562897-459f07a4
Note: Depending on distribution and version, you have to change mirror link.
OK, public key imported, now check signature of the createrepo rpm.
[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: (sha1) dsa sha1 md5 gpg OK
This means that the signature of the package has been verified, and that it is not corrupted. If you want to know public key builder's name , execute the command:
[root@~]# rpm -qa gpg-pubkey\* --qf "%{name}-%{version}-%{release}-%{summary}\n"
gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)
<centos-5-key@centos.org>)
gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)
<centos-5-key@centos.org>)
gpg-pubkey-2689b887-42315a9a-gpg(Hewlett-Packard Company (HP Codesigning Service
))
Note: For showing difference I imported HP GPG key.
If you're the curious type and you want to know more information about imported GPG key, use the following command. rpm -qi <gpg-pubkey>
[root@localhost data]# rpm -qi gpg-pubkey-e8562897-459f07a4
Name : gpg-pubkey Relocations: (not relocatable)
Version : e8562897 Vendor: (none)
Release : 459f07a4 Build Date: Fri 07 Oct 2011 05:53:03 PM IST
Install Date: Fri 07 Oct 2011 05:53:03 PM IST Build Host: localhost
Group : Public Keys Source RPM: (none)
Size : 0 License: pubkey
Signature : (none)
Summary : gpg(CentOS-5 Key (CentOS 5 Official Signing Key) <centos-5-key@
centos.orgi>)
Description :
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: rpm-4.4.2.3 (NSS-3)
mQGiBEWfB6MRBACrnYW6yKMT+MwJlCIhoyTxGf3mAxmnAiDEy6HcYN8rivssVTJk
CFtQBlBOpLV/OW2YtKrCO2xHn46eNfnMri8FGT8g+9JF3MUVi7kiV1He4iJynHXB
+F2ZqIvHf3IaUj1ys+p8TK64FDFxDQDrGQfIsD/+pkSGx53/877IrvdwjwCguQcr
Ioip5TH0Fj0OLUY4asYVZH8EAIqFHEqsY+9ziP+2R3/FyxSllKkjwcMLrBug+cYO
LYDD6eQXE9Mq8XKGFDj9ZB/0+JzK/XQeStheeFG75q3noq5oCPVFO4czuKErIRAB
qKbDBhaTj3JhOgM12XsUYn+rI6NeMV2ZogoQCC2tWmDETfRpYp2moo53NuFWHbAy
XjETA/sHEeQT9huHzdi/lebNBj0L8nBGfLN1nSRP1GtvagBvkR4RZ6DTQyl0UzOJ
RA3ywWlrL9IV9mrpb1Fmn60l2jTMMCc7J6LacmPK906N+FcN/Docj1M4s/4CNanQ
NhzcFhAFtQL56SNyLTCk1XzhssGZ/jwGnNbU/aaj4wOj0Uef5LRGQ2VudE9TLTUg
S2V5IChDZW50T1MgNSBPZmZpY2lhbCBTaWduaW5nIEtleSkgPGNlbnRvcy01LWtl
eUBjZW50b3Mub3JnPohkBBMRAgAkBQJFnwekAhsDBQkSzAMABgsJCAcDAgMVAgMD
FgIBAh4BAheAAAoJEKikR9zoViiXKlEAmwSoZDvZo+WChcg3s/SpNoWCKhMAAJwI
E2aXpZVrpsQnInUQWwkdrTiL5YhMBBMRAgAMBQJFnwiSBYMSzAIRAAoJEDjCFhY5
bKCk0hAAn134bIx3wSbq58E6P6U5RT7Z2Zx4AJ9VxnVkoGHkVIgSdsxHUgRjo27N
F7kBDQRFnwezEAQA/HnJ5yiozwgtf6jt+kii8iua+WnjqBKomPHOQ8moxbWdv5Ks
4e1DPhzRqxhshjmub4SuJ93sgMSAF2ayC9t51mSJV33KfzPF2gIahcMqfABe/2hJ
aMzcQZHrGJCEX6ek8l8SFKou7vICzyajRSIK8gxWKBuQknP/9LKsoczV+xsAAwUD
/idXPkk4vRRHsCwc6I23fdI0ur52bzEqHiAIswNfO521YgLk2W1xyCLc2aYjc8Ni
nrMX1tCnEx0/gK7ICyJoWH1Vc7//79sWFtX2EaTO+Q07xjFX4E66WxJlCo9lOjos
Vk5qc7R+xzLDoLGFtbzaTRQFzf6yr7QTu+BebWLoPwNTiE8EGBECAA8FAkWfB7MC
GwwFCRLMAwAACgkQqKRH3OhWKJfvvACfbsF1WK193zM7vSc4uq51XsceLwgAoI0/
9GxdNhGQEAweSlQfhPa3yYXH
=o/Mx
-----END PGP PUBLIC KEY BLOCK-----
You can view above PGP public key block directly by: vi /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 Depending on distribution, change file path.

Friday, June 24, 2011

Posted by venu k
26 comments | 10:24 PM
 
In number theory, the prime factors of a positive integer are the prime numbers
that divide that integer exactly, without leaving a remainder. The process of
finding these numbers is called integer factorization, or prime factorization.
The fundamental theorem of arithmetic says that every positive integer has a
unique prime factorization.

Example:

The prime factors of 330 are 2, 3, 5 and 11:

               330 = 2 × 3 × 5 × 11

 There is no other possible set of prime numbers that can be multiplied to
make 330.

 You can find prime factors of a number using UNIX/Linux utility factor. This is
a very convenient and cool UNIX command. Here is its syntax:

$ factor 330
330: 2 3 5 11
$ factor 2121977
2121977: 11 11 13 19 71

 Many Algorithms have been devised for determining the Prime factors of a given
number. They vary quite a bit in sophistication and complexity. It is very diff-
icult to build a general-purpose algorithm for this computationally "hard" prob-
lem, so any additional information which is known about the number in question
or its factors can often be used to save a large amount of time.

 Here I am providing a shell script to find prime factors of a positive integer.
This script does most factorizations within a second. In the worst case scenario
(for some large semi-primes with more than 6-digit factors) factorization will
take a couple of minutes to hours.

#!/bin/bash # SCRIPT: primefactors.sh # USAGE : primefactors.sh <Positive Integer> # PURPOSE: Produces prime factors of a given number. # ############################################################################### # Arguments Checking # ############################################################################### if [ $# -ne 1 ] then echo "Usage: scriptname <Positive Integer>" exit 1 fi expr $1 + 1 &>/dev/null if [ $? -ne 0 ] then echo "Sorry, You supplied non numerical value" exit 1 fi [ $1 -lt 2 ] && echo "Values < 2 are not prime numbers" && exit 1 num=$1 ############################################################################### # Functions # ############################################################################### # To know how to find prime number check bellow link: # Shell script to find prime number # Bellow function finds supplied argument is a prime or not. primenumber() { primenum=$1 for ((counter2=2;$((counter2*counter2))<=$primenum;counter2++)) do if [ $((primenum%counter2)) -eq 0 ] then return 1 fi done return 0 } primefind() { # It's good to check that the number it self is a prime or not before going to # find prime factors of a number. Comment out bellow line and supply a prime # number or semi-prime, you will find the difference. # Ex: primefactors.sh 2121979 primenumber $1 && echo "$1" && exit 0 for ((counter1=$2;counter1<=$1;counter1++)) do primenumber $counter1 && factorcheck $1 $counter1 && break done } factorcheck() { prime=$2 newnum=$1 remainder=$((newnum%prime)) if [ $remainder -eq 0 ] then printf "%dx" $prime newnum=$((newnum/prime)) primefind $newnum 2 return else let prime++ primefind $newnum $prime fi } ############################################################################### # Main # ############################################################################### echo -n "Prime Factors of $1: primefind $num 2 printf "\b \n" # \b is used for removing last x.
OUTPUT: [root@venu ]# sh primefactors.sh Usage: scriptname <positive integer=""> [root@venu ]# sh primefactors.sh 21219797 Prime Factors of 21219797: 101x210097 [root@venu ]# sh primefactors.sh 212197 Prime Factors of 212197: 443x479 Running time of script doesn't depend on number,it depends on number of factors, more factors less time and less factors more time. [root@venu ]# time sh primefactors.sh 9999999999999999 Prime Factors of 9999999999999999: 3x3x11x17x73x101x137x5882353 real 0m1.345s user 0m1.225s sys 0m0.091s [root@venu ]# time sh primefactors.sh 999999999995 Prime Factors of 999999999995: 5x251x1831x435179 real 1m32.105s user 1m28.866s sys 0m3.192s [root@venu ]# time sh primefactors.sh 99999999000000000 Prime Factors of 99999999000000000: 2x2x2x2x2x2x2x2x2x3x3x5x5x5x5x5x5x5x5x5x11x7 3x101x137 real 0m0.543s user 0m0.508s sys 0m0.035s

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