<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8514572398266969701</id><updated>2012-02-29T01:20:49.053-08:00</updated><category term='Linux Fun'/><category term='Shell Scripts'/><category term='Article'/><category term='HowTo&apos;s'/><category term='Beginners Scripts'/><title type='text'>UNIX/Linux Bash Shell Scripting</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.bashguru.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default?start-index=26&amp;max-results=25'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>60</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7280170272623726598</id><published>2011-11-18T09:37:00.000-08:00</published><updated>2011-11-18T10:22:02.751-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HowTo&apos;s'/><title type='text'>How to format  Flash/Pen Drive in Linux</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note: I have used my device name and number in examples, you have to change it&lt;br /&gt;as your device name and number, otherwise  unwanted device data will be wiped&lt;br /&gt;out.&lt;/div&gt;&lt;br /&gt; Put your USB flash drive in one of your computer's free USB ports. After a few&lt;br /&gt;seconds, Linux will automatically recognize it and an icon for it will appear on&lt;br /&gt;your desktop. If pen drive doesn't mounted automatically, login as root and try&lt;br /&gt;to mount it, still it is not mounted there may be a problem with pen drive or it&lt;br /&gt;is defective.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Finding device: &lt;/h4&gt;&lt;br /&gt; Very very first thing is you should to know what device you are working with.&lt;br /&gt;For that you can use any one of the following methods.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Method 1:  Using  ls /dev/sd*  command&lt;/h5&gt;&lt;br /&gt;First make sure your usb drive is not plugged in. Then run:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[venu@ras ~]$ ls /dev/sd*&lt;br /&gt;/dev/sda   /dev/sda10  /dev/sda12  /dev/sda2  /dev/sda6  /dev/sda8&lt;br /&gt;/dev/sda1  /dev/sda11  /dev/sda13  /dev/sda5  /dev/sda7  /dev/sda9&lt;/div&gt;&lt;br /&gt;Once you have done that, put in your pen drive and run the above command again.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[venu@ras ~]$ ls /dev/sd*&lt;br /&gt;/dev/sda   /dev/sda10  /dev/sda12  /dev/sda2  /dev/sda6  /dev/sda8  &lt;b&gt;/dev/sdb&lt;/b&gt;&lt;br /&gt;/dev/sda1  /dev/sda11  /dev/sda13  /dev/sda5  /dev/sda7  /dev/sda9  &lt;b&gt;/dev/sdb1&lt;/b&gt;&lt;/div&gt; &lt;br /&gt;Whatever drive (ex sdb) was not there before is your pen drive. From the above&lt;br /&gt;list my pen drive is sdb. Don't be confuse with &lt;b&gt;/dev/sdb&lt;/b&gt; and &lt;b&gt;/dev/sdb1&lt;/b&gt;,  sdb is&lt;br /&gt;a drive and sdb1 is first partition in that drive.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Method 2:  Using df command&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[venu@ras ~]$ df&lt;br /&gt;Filesystem           1K-blocks      Used Available Use% Mounted on&lt;br /&gt;&lt;br /&gt;/dev/sda10            29753556   8008860  20208884  29% /&lt;br /&gt;/dev/sda12            19840892   1528664  17288088   9% /msfiles&lt;br /&gt;/dev/sda11            21825108  16772836   3925716  82% /venu&lt;br /&gt;tmpfs                  1032232         0   1032232   0% /dev/shm&lt;br /&gt;&lt;b&gt;/dev/sdb1              1947636         4   1947632   1% /media/VENU&lt;/b&gt;&lt;/div&gt; &lt;br /&gt;As you can see above my pen drive (/dev/sdb1) is mounted at /media/VENU. If you&lt;br /&gt;have any doubt about which one is your pen drive, do same thing as in method1.&lt;br /&gt;Run df command before and after inserting  pen drive.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Method 3:   dmesg | tail&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[venu@ras ~]$ dmesg | tail&lt;br /&gt;sdb: assuming drive cache: write through&lt;br /&gt;SCSI device sdb: 3903359 512-byte hdwr sectors (1999 MB)&lt;br /&gt;sdb: Write Protect is off&lt;br /&gt;sdb: Mode Sense: 0b 00 00 08&lt;br /&gt;sdb: assuming drive cache: write through&lt;br /&gt;&lt;b&gt; sdb: sdb1&lt;/b&gt;&lt;br /&gt;sd 8:0:0:0: Attached scsi removable disk sdb&lt;br /&gt;sd 8:0:0:0: Attached scsi generic sg1 type 0&lt;br /&gt;usb-storage: device scan complete&lt;br /&gt;SELinux: initialized (dev sdb1, type vfat), uses genfs_contexts&lt;/div&gt;&lt;br /&gt;&lt;h5&gt;Method 4:   mount -l&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[venu@ras ~]$ mount -l&lt;br /&gt;/dev/sda10 on / type ext3 (rw) [/]&lt;br /&gt;proc on /proc type proc (rw)&lt;br /&gt;sysfs on /sys type sysfs (rw)&lt;br /&gt;devpts on /dev/pts type devpts (rw,gid=5,mode=620)&lt;br /&gt;/dev/sda12 on /msfiles type ext3 (rw) [/msfiles1]&lt;br /&gt;/dev/sda11 on /venu type ext3 (rw) [/venu1]&lt;br /&gt;tmpfs on /dev/shm type tmpfs (rw)&lt;br /&gt;none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)&lt;br /&gt;sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)&lt;br /&gt;&lt;b&gt;/dev/sdb1 on /media/VENU type vfat (rw,nosuid,nodev,shortname=winnt,uid=500)&lt;br /&gt;[VENU]&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;&lt;h5&gt;Method 5:   fdisk -l &lt;/h5&gt;&lt;br /&gt;fdisk command need root privileges. So login as root and run the command:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# fdisk -l&lt;br /&gt;Disk /dev/sda: 500.1 GB, 500107862016 bytes&lt;br /&gt;255 heads, 63 sectors/track, 60801 cylinders&lt;br /&gt;Units = cylinders of 16065 * 512 = 8225280 bytes&lt;br /&gt;&lt;br /&gt;   Device Boot      Start         End      Blocks   Id  System&lt;br /&gt;&lt;br /&gt;/dev/sda1   *           1        5099    40957686    7  HPFS/NTFS&lt;br /&gt;/dev/sda2            5100       60800   447418282+   f  W95 Ext'd (LBA)&lt;br /&gt;/dev/sda5            5100       10198    40957686    7  HPFS/NTFS&lt;br /&gt;/dev/sda10          50994       54817    30716248+  83  Linux&lt;br /&gt;/dev/sda11          54818       57622    22531131   83  Linux&lt;br /&gt;/dev/sda12          57623       60172    20482843+  83  Linux&lt;br /&gt;/dev/sda13          60173       60694     4192933+  82  Linux swap / Solaris&lt;br /&gt;&lt;br /&gt;Disk /dev/sdb: 1998 MB, 1998519808 bytes&lt;br /&gt;32 heads, 63 sectors/track, 1936 cylinders&lt;br /&gt;Units = cylinders of 2016 * 512 = 1032192 bytes&lt;br /&gt;&lt;br /&gt;   Device Boot      Start         End      Blocks   Id  System&lt;br /&gt;&lt;br /&gt;&lt;b&gt;/dev/sdb1               1        1936     1951456+   b  W95 FAT32&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;fdisk -l displays all detected drives even drive is unmounted.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Formatting pen drive:&lt;/h4&gt;&lt;br /&gt; First decide which file system you want. FAT32 is probably the file system you&lt;br /&gt;want, but that's up to you. Ext3 may not be work on windows, It asks for fomatt-&lt;br /&gt;ing.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;1.Once you have Identified your pen drive, unmount it.&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# umount /dev/sdb1&lt;/div&gt;&lt;br /&gt;&lt;h5&gt;2.Format pen drive now.&lt;/h5&gt;&lt;br /&gt; Depending on distribution different commands are available for formatting, but&lt;br /&gt;in general most used ones that I have seen are &lt;b&gt;mkdosfs&lt;/b&gt; or &lt;b&gt;mkfs.vfat&lt;/b&gt; for FAT32&lt;br /&gt;and  &lt;b&gt;mke2fs&lt;/b&gt; or &lt;b&gt;mkfs.ext3&lt;/b&gt; for Linux filesystems.&lt;br /&gt;&lt;br /&gt;&lt;h6&gt;For Ms-Dos or Windows file system:&lt;/h6&gt;&lt;br /&gt; Use &lt;b&gt;mkdosfs -n "Label" -I /dev/sdx&lt;/b&gt;, Replace Label with the name you want the&lt;br /&gt;pen drive to have and /dev/sdx with your drive.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# mkdosfs -n "KVMREDDY" -I /dev/sdb1&lt;br /&gt;mkdosfs 2.11 (12 Mar 2005)&lt;/div&gt;                              or&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# mkfs.vfat -n "KVMREDDY" -I /dev/sdb1&lt;br /&gt;mkfs.vfat 2.11 (12 Mar 2005)&lt;/div&gt;&lt;br /&gt;          &lt;b&gt; (Note: I – capital letter of i )&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; If you don't want any Label then no need to supply option n. switch I will&lt;br /&gt;force mkdosfs to work properly but it  is not mandatory, you can remove that&lt;br /&gt;also.&lt;br /&gt;&lt;br /&gt;&lt;h6&gt;For Linux File system:&lt;/h6&gt;&lt;br /&gt; Use &lt;b&gt;mkfs.ext3 -L "Label"  /dev/sdx&lt;/b&gt; for ext3 file system or &lt;b&gt;mke2fs -L 'Label'&lt;br /&gt;/dev/sdx&lt;/b&gt; for ex2, Replace Label with the name you want the pen drive to have&lt;br /&gt;and /dev/sdx with your drive.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Ext3 file system:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# mkfs.ext3 -L "KVMREDDY" /dev/sdb1&lt;br /&gt;mke2fs 1.39 (29-May-2006)&lt;br /&gt;Filesystem label=KVMREDDY&lt;br /&gt;OS type: Linux&lt;br /&gt;.............................&lt;br /&gt;[root@ras ~]# &lt;/div&gt;&lt;br /&gt;&lt;b&gt;ext2 file system:  &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ras ~]# mke2fs -L 'VENU' /dev/sdb1&lt;br /&gt;mke2fs 1.39 (29-May-2006)&lt;br /&gt;Filesystem label=VENU&lt;br /&gt;OS type: Linux&lt;br /&gt;...........................&lt;br /&gt;[root@ras ~]# &lt;/div&gt;&lt;br /&gt; After formatting you will be returned to the prompt then remove and insert the&lt;br /&gt;pen drive to have mounted again.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7280170272623726598?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7280170272623726598/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/11/how-to-format-flashpen-drive-in-linux.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7280170272623726598'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7280170272623726598'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/11/how-to-format-flashpen-drive-in-linux.html' title='How to format  Flash/Pen Drive in Linux'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-5016471197675699735</id><published>2011-10-08T04:45:00.000-07:00</published><updated>2011-10-08T04:51:09.863-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HowTo&apos;s'/><title type='text'>How to Remove RPM GPG Public Key</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt; Public keys can be erased after importing just like packages. Here's how to&lt;br /&gt;remove the CentOS GPG public key:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;rpm -e &amp;lt;GPG public key&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost]# rpm -qa gpg-pubkey*&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;br /&gt;[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4&lt;br /&gt;[root@localhost]# rpm -qa gpg-pubkey*&lt;br /&gt;[root@localhost]#&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;If there are duplicates exist:&lt;/h4&gt;&lt;br /&gt;For example, Import the same key two times&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5&lt;br /&gt;[root@localhost]# rpm -qa gpg-pubkey*&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;br /&gt;[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5&lt;br /&gt;[root@localhost]# rpm -qa gpg-pubkey*&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;/div&gt;&lt;br /&gt;Now try to remove Public Key, You will get error message like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4&lt;br /&gt;error: "gpg-pubkey-e8562897-459f07a4" specifies multiple packages&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;How to solve this:&lt;/h4&gt;&lt;br /&gt;Use &lt;b&gt;--allmatches&lt;/b&gt; switch along with -e, It removes all versions of the package&lt;br /&gt;which match PACKAGE_NAME(Public Key ).&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost]# rpm -e --allmatches gpg-pubkey-e8562897-459f07a4&lt;br /&gt;[root@localhost]# rpm -qa gpg-pubkey*&lt;br /&gt;[root@localhost]# &lt;/div&gt;&lt;/span &gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-5016471197675699735?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/5016471197675699735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/10/how-to-remove-rpm-gpg-public-key.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5016471197675699735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5016471197675699735'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/10/how-to-remove-rpm-gpg-public-key.html' title='How to Remove RPM GPG Public Key'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7705892701876749943</id><published>2011-10-07T12:21:00.000-07:00</published><updated>2011-10-11T09:05:15.600-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HowTo&apos;s'/><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>How to Import RPM GPG Key</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note: All the commands tested on CentOs 5.x. Your output may be vary depending &lt;br /&gt;on distribution and version, so your results may not always look exactly like &lt;br /&gt;the listings and figures shown here. Almost all everything works well on&lt;br /&gt;RHEL/CentOs/Fedora.&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;Why to check signature of an rpm:&lt;/h4&gt;&lt;br /&gt; The signature confirms that the package was signed by an authorized party and&lt;br /&gt;also confirm the integrity and origin of your file. It is extremely important&lt;br /&gt;to verify the signature of the RPM files before installing them to ensure that&lt;br /&gt;they have not been altered from the original source of the packages.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Checking a package's Signature:&lt;/h4&gt;&lt;br /&gt; The --checksig(or -K)  option checks all the digests and signatures contained&lt;br /&gt;in PACKAGE_FILE to ensure the integrity and origin of the package. Note that&lt;br /&gt;signatures are now verified whenever a package is read, and --checksig is useful&lt;br /&gt;to verify all of the digests and signatures associated with a package.&lt;br /&gt;&lt;br /&gt; If you wish to verify that a package has not been corrupted or tampered with,&lt;br /&gt;examine only the md5sum by typing the following command at a shell prompt (where&lt;br /&gt;&amp;lt;rpm-file&amp;gt; is the file name of the RPM package):&lt;br /&gt;&lt;br /&gt;&lt;b&gt;rpm -K --nosignature &amp;lt;rpm-file&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; The message &lt;b&gt;&amp;lt;rpm-file&amp;gt;: md5 OK&lt;/b&gt; is displayed. This brief message means&lt;br /&gt;that the file was not corrupted by the download. To see a more verbose message,&lt;br /&gt;replace -K with -Kvv in the command.&lt;br /&gt;&lt;br /&gt; For demonstration purpose I downloaded &lt;b&gt;createrepo&lt;/b&gt; package from CentOs mirror&lt;br /&gt;and used in examples.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost ~]# rpm -K --nosignature createrepo-0.4.11-3.el5.noarch.rpm&lt;br /&gt;createrepo-0.4.11-3.el5.noarch.rpm: sha1 md5 OK&lt;/div&gt;&lt;br /&gt; On the other hand, how trustworthy is the developer who created the package? If&lt;br /&gt;the package is signed with the developer's GnuPG key,you know that the developer&lt;br /&gt;really is who they say they are.&lt;br /&gt;&lt;br /&gt; An RPM package can be signed using Gnu Privacy Guard (or GnuPG), to help you&lt;br /&gt;make certain your downloaded package is trustworthy. GnuPG is a tool for secure&lt;br /&gt;communication; it is a complete and free replacement for the encryption technol-&lt;br /&gt;ogy of PGP, an electronic privacy program. With GnuPG, you can authenticate the&lt;br /&gt;validity of documents and encrypt/decrypt data to and from other recipients.&lt;br /&gt;GnuPG is capable of decrypting and verifying PGP 5.x files as well.&lt;br /&gt;&lt;br /&gt; During installation,GnuPG is installed by default. That way you can immediately&lt;br /&gt;start using GnuPG to verify any packages that you receive from CentOs(RHEL/Fedor&lt;br /&gt;a). Before doing so, you must first import CentOs's public key. If you not impo-&lt;br /&gt;rted correct public key, you will get following error message.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm&lt;br /&gt;createrepo-0.4.11-3.el5.noarch.rpm: (SHA1) DSA sha1 md5 (GPG) NOT OK (MISSING&lt;br /&gt;KEYS: GPG#e8562897)&lt;/div&gt;&lt;br /&gt;Here the GPG in parentheses indicates that there's a problem with the signature,&lt;br /&gt;and the message at the end of the line (MISSING KEYS) shows what the problem is.&lt;br /&gt;Basically, RPM asked GPG to verify the package against a key&lt;b&gt;(GPG#e8562897)&lt;/b&gt; that&lt;br /&gt;GPG didn't have, and GPG complained. It means you missed the correct public key.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;How to import public keys:&lt;/h4&gt;&lt;br /&gt; Digital signatures cannot be verified without a public key. An ascii armored&lt;br /&gt;public key can be added to the rpm database using &lt;b&gt;--import&lt;/b&gt;. An imported public&lt;br /&gt;key is carried in a header, and key ring management is performed exactly like&lt;br /&gt;package management. For example, all currently imported public keys can be&lt;br /&gt;displayed by:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;rpm -qa gpg-pubkey* &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To verify CentOs (RHEL/Fedora) packages, you must import the CentOs(RHEL/Fedora)&lt;br /&gt;GPG key. To do so, execute the following command at a shell prompt:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 &lt;/div&gt;&lt;br /&gt;This will create duplicate copies if one already exists.&lt;br /&gt;To display a list of all keys installed for RPM verification,execute the command&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost ~]# rpm -qa gpg-pubkey*&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;/div&gt;&lt;br /&gt;                                  or&lt;br /&gt;&lt;br /&gt;&lt;b&gt;RPM has the capacity to retrieve the key from a  Mirror: &lt;/b&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@ ~]# rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5&lt;br /&gt;[root@ ~]# rpm -qa gpg-pubkey*&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;br /&gt;gpg-pubkey-e8562897-459f07a4&lt;/div&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note: Depending on distribution and version, you have to change mirror link.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;OK, public key imported, now check signature of the &lt;b&gt;createrepo&lt;/b&gt; rpm.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm&lt;br /&gt;createrepo-0.4.11-3.el5.noarch.rpm: (sha1) dsa sha1 md5 gpg OK&lt;/div&gt;&lt;br /&gt; This means that the signature of the package has been verified, and that it is&lt;br /&gt;not corrupted. If you want to know public key builder's name , execute the&lt;br /&gt;command:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@~]# rpm -qa gpg-pubkey\* --qf "%{name}-%{version}-%{release}-%{summary}\n"&lt;br /&gt;gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)&lt;br /&gt;&amp;lt;centos-5-key@centos.org&amp;gt;)&lt;br /&gt;gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)&lt;br /&gt;&amp;lt;centos-5-key@centos.org&amp;gt;)&lt;br /&gt;gpg-pubkey-2689b887-42315a9a-gpg(Hewlett-Packard Company (HP Codesigning Service&lt;br /&gt;))&lt;/div&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note: For showing difference I imported HP GPG key.&lt;/div&gt;&lt;br /&gt; If you're the curious type and you want to know more information about imported&lt;br /&gt;GPG key, use the following command.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;rpm -qi &amp;lt;gpg-pubkey&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost data]# rpm -qi gpg-pubkey-e8562897-459f07a4&lt;br /&gt;Name        : gpg-pubkey           Relocations: (not relocatable)&lt;br /&gt;Version     : e8562897             Vendor: (none)&lt;br /&gt;Release     : 459f07a4             Build Date: Fri 07 Oct 2011 05:53:03 PM IST&lt;br /&gt;Install Date: Fri 07 Oct 2011 05:53:03 PM IST      Build Host: localhost&lt;br /&gt;Group       : Public Keys          Source RPM: (none)&lt;br /&gt;Size        : 0                    License: pubkey&lt;br /&gt;Signature   : (none)&lt;br /&gt;Summary     : gpg(CentOS-5 Key (CentOS 5 Official Signing Key) &amp;lt;centos-5-key@&lt;br /&gt;centos.orgi&amp;gt;)&lt;br /&gt;Description :&lt;br /&gt;-----BEGIN PGP PUBLIC KEY BLOCK-----&lt;br /&gt;Version: rpm-4.4.2.3 (NSS-3)&lt;br /&gt;&lt;br /&gt;mQGiBEWfB6MRBACrnYW6yKMT+MwJlCIhoyTxGf3mAxmnAiDEy6HcYN8rivssVTJk&lt;br /&gt;CFtQBlBOpLV/OW2YtKrCO2xHn46eNfnMri8FGT8g+9JF3MUVi7kiV1He4iJynHXB&lt;br /&gt;+F2ZqIvHf3IaUj1ys+p8TK64FDFxDQDrGQfIsD/+pkSGx53/877IrvdwjwCguQcr&lt;br /&gt;Ioip5TH0Fj0OLUY4asYVZH8EAIqFHEqsY+9ziP+2R3/FyxSllKkjwcMLrBug+cYO&lt;br /&gt;LYDD6eQXE9Mq8XKGFDj9ZB/0+JzK/XQeStheeFG75q3noq5oCPVFO4czuKErIRAB&lt;br /&gt;qKbDBhaTj3JhOgM12XsUYn+rI6NeMV2ZogoQCC2tWmDETfRpYp2moo53NuFWHbAy&lt;br /&gt;XjETA/sHEeQT9huHzdi/lebNBj0L8nBGfLN1nSRP1GtvagBvkR4RZ6DTQyl0UzOJ&lt;br /&gt;RA3ywWlrL9IV9mrpb1Fmn60l2jTMMCc7J6LacmPK906N+FcN/Docj1M4s/4CNanQ&lt;br /&gt;NhzcFhAFtQL56SNyLTCk1XzhssGZ/jwGnNbU/aaj4wOj0Uef5LRGQ2VudE9TLTUg&lt;br /&gt;S2V5IChDZW50T1MgNSBPZmZpY2lhbCBTaWduaW5nIEtleSkgPGNlbnRvcy01LWtl&lt;br /&gt;eUBjZW50b3Mub3JnPohkBBMRAgAkBQJFnwekAhsDBQkSzAMABgsJCAcDAgMVAgMD&lt;br /&gt;FgIBAh4BAheAAAoJEKikR9zoViiXKlEAmwSoZDvZo+WChcg3s/SpNoWCKhMAAJwI&lt;br /&gt;E2aXpZVrpsQnInUQWwkdrTiL5YhMBBMRAgAMBQJFnwiSBYMSzAIRAAoJEDjCFhY5&lt;br /&gt;bKCk0hAAn134bIx3wSbq58E6P6U5RT7Z2Zx4AJ9VxnVkoGHkVIgSdsxHUgRjo27N&lt;br /&gt;F7kBDQRFnwezEAQA/HnJ5yiozwgtf6jt+kii8iua+WnjqBKomPHOQ8moxbWdv5Ks&lt;br /&gt;4e1DPhzRqxhshjmub4SuJ93sgMSAF2ayC9t51mSJV33KfzPF2gIahcMqfABe/2hJ&lt;br /&gt;aMzcQZHrGJCEX6ek8l8SFKou7vICzyajRSIK8gxWKBuQknP/9LKsoczV+xsAAwUD&lt;br /&gt;/idXPkk4vRRHsCwc6I23fdI0ur52bzEqHiAIswNfO521YgLk2W1xyCLc2aYjc8Ni&lt;br /&gt;nrMX1tCnEx0/gK7ICyJoWH1Vc7//79sWFtX2EaTO+Q07xjFX4E66WxJlCo9lOjos&lt;br /&gt;Vk5qc7R+xzLDoLGFtbzaTRQFzf6yr7QTu+BebWLoPwNTiE8EGBECAA8FAkWfB7MC&lt;br /&gt;GwwFCRLMAwAACgkQqKRH3OhWKJfvvACfbsF1WK193zM7vSc4uq51XsceLwgAoI0/&lt;br /&gt;9GxdNhGQEAweSlQfhPa3yYXH&lt;br /&gt;=o/Mx&lt;br /&gt;-----END PGP PUBLIC KEY BLOCK-----&lt;/div&gt;&lt;br /&gt;You can view above PGP public key block directly by:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;vi /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Depending on distribution, change file path.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7705892701876749943?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7705892701876749943/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/10/how-to-import-rpm-gpg-key.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7705892701876749943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7705892701876749943'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/10/how-to-import-rpm-gpg-key.html' title='How to Import RPM GPG Key'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-3583021598401401806</id><published>2011-06-24T22:24:00.000-07:00</published><updated>2011-06-24T22:41:54.170-07:00</updated><title type='text'>Shell Script to Find Prime Factors of a Number</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;In number theory, the prime factors of a positive integer are the prime numbers&lt;br /&gt;that divide that integer exactly, without leaving a remainder. The process of&lt;br /&gt;finding these numbers is called integer factorization, or prime factorization.&lt;br /&gt;The fundamental theorem of arithmetic says that every positive integer has a&lt;br /&gt;unique prime factorization.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The prime factors of 330 are 2, 3, 5 and 11:&lt;br /&gt;&lt;br /&gt;               330 = 2 × 3 × 5 × 11&lt;br /&gt;&lt;br /&gt; There is no other possible set of prime numbers that can be multiplied to&lt;br /&gt;make 330.&lt;br /&gt;&lt;br /&gt; You can find prime factors of a number using UNIX/Linux utility &lt;b&gt;factor&lt;/b&gt;. This is&lt;br /&gt;a very convenient and cool UNIX command. Here is its syntax:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ factor 330&lt;/b&gt;&lt;br /&gt;330: 2 3 5 11&lt;br /&gt;&lt;b&gt;$ factor 2121977&lt;/b&gt;&lt;br /&gt;2121977: 11 11 13 19 71&lt;br /&gt;&lt;br /&gt; Many Algorithms have been devised for determining the Prime factors of a given&lt;br /&gt;number. They vary quite a bit in sophistication and complexity. It is very diff-&lt;br /&gt;icult to build a general-purpose algorithm for this computationally "hard" prob-&lt;br /&gt;lem, so any additional information which is known about the number in question&lt;br /&gt;or its factors can often be used to save a large amount of time.&lt;br /&gt;&lt;br /&gt; Here I am providing a shell script to find prime factors of a positive integer.&lt;br /&gt;This script does most factorizations within a second. In the worst case scenario&lt;br /&gt;(for some large semi-primes with more than 6-digit factors) factorization will&lt;br /&gt;take a couple of minutes to hours.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  primefactors.sh&lt;br /&gt;# USAGE :  primefactors.sh &amp;lt;Positive Integer&amp;gt;&lt;br /&gt;# PURPOSE: Produces prime factors of a given number.&lt;br /&gt;#&lt;br /&gt;###############################################################################&lt;br /&gt;#                           Arguments Checking                                #&lt;br /&gt;###############################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;    echo "Usage: scriptname &amp;lt;Positive Integer&amp;gt;"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;expr $1 + 1  &amp;&gt;/dev/null&lt;br /&gt;if [ $? -ne 0 ]&lt;br /&gt;then&lt;br /&gt;    echo "Sorry, You supplied non numerical value"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;[ $1 -lt 2 ] &amp;&amp; echo "Values &amp;lt; 2 are not prime numbers" &amp;&amp; exit 1&lt;br /&gt;&lt;br /&gt;num=$1&lt;br /&gt;&lt;br /&gt;###############################################################################&lt;br /&gt;#                              Functions                                      #&lt;br /&gt;###############################################################################&lt;br /&gt;# To know how to find prime number check bellow link:&lt;br /&gt;# &lt;a href="http://bashscript.blogspot.com/2009/11/shell-script-to-find-prime-number.html"&gt;Shell script to find prime number&lt;/a&gt;&lt;br /&gt;# Bellow function finds supplied argument is a prime or not.&lt;br /&gt;&lt;br /&gt;primenumber()&lt;br /&gt;{&lt;br /&gt;           primenum=$1&lt;br /&gt;           for ((counter2=2;$((counter2*counter2))&lt;=$primenum;counter2++))&lt;br /&gt;           do&lt;br /&gt;               if [ $((primenum%counter2)) -eq 0 ]&lt;br /&gt;               then&lt;br /&gt;                    return 1&lt;br /&gt;               fi&lt;br /&gt;           done&lt;br /&gt;           return 0&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;primefind()&lt;br /&gt;{&lt;br /&gt;# It's good to check that the number it self is a prime or not before going to&lt;br /&gt;# find prime factors of a number. Comment out bellow line and supply a prime&lt;br /&gt;# number or semi-prime, you will find the difference.&lt;br /&gt;# Ex: primefactors.sh 2121979&lt;br /&gt;&lt;br /&gt;        primenumber $1 &amp;&amp; echo "$1" &amp;&amp; exit 0&lt;br /&gt;&lt;br /&gt;        for ((counter1=$2;counter1&lt;=$1;counter1++))&lt;br /&gt;        do&lt;br /&gt;                primenumber $counter1 &amp;&amp; factorcheck $1 $counter1 &amp;&amp;  break&lt;br /&gt;        done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;factorcheck()&lt;br /&gt;{&lt;br /&gt;     prime=$2&lt;br /&gt;     newnum=$1&lt;br /&gt;     remainder=$((newnum%prime))&lt;br /&gt;&lt;br /&gt;     if [ $remainder -eq 0 ]&lt;br /&gt;     then&lt;br /&gt;          printf "%dx" $prime&lt;br /&gt;          newnum=$((newnum/prime))&lt;br /&gt;          primefind $newnum 2&lt;br /&gt;          return&lt;br /&gt;     else&lt;br /&gt;          let prime++&lt;br /&gt;          primefind $newnum $prime&lt;br /&gt;    fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;###############################################################################&lt;br /&gt;#                                   Main                                      #&lt;br /&gt;###############################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Prime Factors of $1: "&lt;br /&gt;primefind $num 2&lt;br /&gt;printf "\b \n"                   # \b is used for removing last x.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;OUTPUT:&lt;/span&gt;&lt;br /&gt;[root@venu ]# sh primefactors.sh&lt;br /&gt;Usage: scriptname &lt;Positive Integer&gt;&lt;br /&gt;[root@venu ]# sh primefactors.sh 21219797&lt;br /&gt;Prime Factors of 21219797: 101x210097&lt;br /&gt;[root@venu ]# sh primefactors.sh 212197&lt;br /&gt;Prime Factors of 212197: 443x479&lt;br /&gt;&lt;br /&gt;Running time of script doesn't depend on number,it depends on number of factors,&lt;br /&gt;more factors less time and less factors more time.&lt;br /&gt;&lt;br /&gt;[root@venu ]# time sh primefactors.sh 9999999999999999&lt;br /&gt;Prime Factors of 9999999999999999: 3x3x11x17x73x101x137x5882353&lt;br /&gt;&lt;br /&gt;real    0m1.345s&lt;br /&gt;user    0m1.225s&lt;br /&gt;sys     0m0.091s&lt;br /&gt;&lt;br /&gt;[root@venu ]# time sh primefactors.sh 999999999995&lt;br /&gt;Prime Factors of 999999999995: 5x251x1831x435179&lt;br /&gt;&lt;br /&gt;real    1m32.105s&lt;br /&gt;user    1m28.866s&lt;br /&gt;sys     0m3.192s&lt;br /&gt;&lt;br /&gt;[root@venu ]# time sh primefactors.sh 99999999000000000&lt;br /&gt;Prime Factors of 99999999000000000: 2x2x2x2x2x2x2x2x2x3x3x5x5x5x5x5x5x5x5x5x11x7&lt;br /&gt;3x101x137&lt;br /&gt;&lt;br /&gt;real    0m0.543s&lt;br /&gt;user    0m0.508s&lt;br /&gt;sys     0m0.035s&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-3583021598401401806?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/3583021598401401806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/06/shell-script-to-find-prime-factors-of.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3583021598401401806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3583021598401401806'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/06/shell-script-to-find-prime-factors-of.html' title='Shell Script to Find Prime Factors of a Number'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6757878209207485849</id><published>2011-01-06T13:29:00.000-08:00</published><updated>2011-02-01T05:09:24.897-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Menu Driven Shell Script - Using Dialog Utility</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT  : menu_dialog.sh&lt;br /&gt;# PURPOSE : A menu driven Shell script using dialog utility&lt;br /&gt;#           which has following options:&lt;br /&gt;#           Display Today's Date and Time.&lt;br /&gt;#           Display calendar.&lt;br /&gt;#           Delete selected file from supplied directory.&lt;br /&gt;#           List of users currently logged in&lt;br /&gt;#           Disk Statistics&lt;br /&gt;#           Exit&lt;br /&gt;#&lt;br /&gt;##############################################################################&lt;br /&gt;#                 Checking availability of dialog utility                    #&lt;br /&gt;##############################################################################&lt;br /&gt;&lt;br /&gt;# dialog is a utility installed by default on all major Linux distributions.&lt;br /&gt;# But it is good to check availability of dialog utility on your Linux box.&lt;br /&gt;&lt;br /&gt;which dialog &amp;&gt; /dev/null&lt;br /&gt;&lt;br /&gt;[ $? -ne 0 ]  &amp;&amp; echo "Dialog utility is not available, Install it" &amp;&amp; exit 1&lt;br /&gt;&lt;br /&gt;##############################################################################&lt;br /&gt;#                      Define Functions Here                                 #&lt;br /&gt;##############################################################################&lt;br /&gt;&lt;br /&gt;###################### deletetempfiles function ##############################&lt;br /&gt;&lt;br /&gt;# This function is called by trap command&lt;br /&gt;# For conformation of deletion use rm -fi *.$$&lt;br /&gt;&lt;br /&gt;deletetempfiles()&lt;br /&gt;{&lt;br /&gt;    rm -f *.$$&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;######################## Show_time function #################################&lt;br /&gt;&lt;br /&gt;# Shows today's date and time&lt;br /&gt;&lt;br /&gt;show_time()&lt;br /&gt;{&lt;br /&gt;   dialog --backtitle "MENU DRIVEN PROGRAM" --title "DATE &amp; TIME" \&lt;br /&gt;   --msgbox "\n        Today's Date:   `date +"%d-%m-%Y"` \n\n \&lt;br /&gt;       Today's Time:   `date +"%r %Z"`" 10 60&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;####################### show_cal function ###################################&lt;br /&gt;&lt;br /&gt;# Shows current month calendar&lt;br /&gt;&lt;br /&gt;show_cal()&lt;br /&gt;{&lt;br /&gt;   dialog --backtitle "MENU DRIVEN PROGRAM" --title "CALENDAR" \&lt;br /&gt;   --msgbox "`cal`" 12 25&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;####################### deletefile function #################################&lt;br /&gt;&lt;br /&gt;# Used to delete file under supplied directory, not including sub dirs.&lt;br /&gt;&lt;br /&gt;deletefile()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;   dialog --backtitle "MENU DRIVEN PROGRAM" --title "Directory Path" \&lt;br /&gt;   --inputbox "\nEnter directory path (Absolute or Relative) \&lt;br /&gt;\nPress just Enter for current directory" 12 60 2&gt; temp1.$$&lt;br /&gt;&lt;br /&gt;   if [ $? -ne 0 ]&lt;br /&gt;   then&lt;br /&gt;       rm -f temp1.$$&lt;br /&gt;       return&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   rmdir=`cat temp1.$$`&lt;br /&gt;&lt;br /&gt;   if [ -z "$rmdir" ]&lt;br /&gt;   then&lt;br /&gt;       dirname=$(pwd)                  # You can also use `pwd`&lt;br /&gt;       rmdir=$dirname/*&lt;br /&gt;   else&lt;br /&gt;&lt;br /&gt;       # remove trailing * and / from directory path&lt;br /&gt;&lt;br /&gt;       echo "$rmdir" | grep "\*$" &amp;&gt; /dev/null &amp;&amp; rmdir=${rmdir%\*}&lt;br /&gt;       echo "$rmdir" | grep "/$" &amp;&gt; /dev/null &amp;&amp; rmdir=${rmdir%/}&lt;br /&gt;&lt;br /&gt;       # Check supplied directory exist or not&lt;br /&gt;&lt;br /&gt;       ( cd $rmdir 2&gt;&amp;1 | grep "No such file or directory" &amp;&gt; /dev/null )&lt;br /&gt;&lt;br /&gt;       # Above codeblock run in sub shell, so your current directory persists.&lt;br /&gt;&lt;br /&gt;       if [ $? -eq 0 ]&lt;br /&gt;       then&lt;br /&gt;           dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;           --title "Validating Directory" \&lt;br /&gt;           --msgbox "\n $rmdir: No such file or directory \&lt;br /&gt;\n\n Press ENTER to return to the Main Menu" 10 60&lt;br /&gt;           return&lt;br /&gt;       fi&lt;br /&gt;&lt;br /&gt;       # Do you have proper permissions ?&lt;br /&gt;&lt;br /&gt;       ( cd $rmdir 2&gt; /dev/null )&lt;br /&gt;&lt;br /&gt;       if [ $? -ne 0 ]&lt;br /&gt;       then&lt;br /&gt;           dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;           --title "Checking Permissions" \&lt;br /&gt;           --msgbox "\n $rmdir:  Permission denied to access this directory \&lt;br /&gt;\n\n Press ENTER to return to the Main Menu" 10 60&lt;br /&gt;           return&lt;br /&gt;       fi&lt;br /&gt;&lt;br /&gt;       if [ ! -r $rmdir ]&lt;br /&gt;       then&lt;br /&gt;           dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;           --title "Checking Permissions" \&lt;br /&gt;           --msgbox "\n $rmdir:  No read permission \&lt;br /&gt;\n\n Press ENTER to return to the Main Menu" 10 60&lt;br /&gt;           return&lt;br /&gt;       fi&lt;br /&gt;&lt;br /&gt;   dirname=$rmdir&lt;br /&gt;   rmdir=$rmdir/*             # get all the files under given directory&lt;br /&gt;&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   for i in $rmdir            # process each file&lt;br /&gt;   do&lt;br /&gt;&lt;br /&gt;      # Store all regular file names in temp2.$$&lt;br /&gt;&lt;br /&gt;      if [ -f $i ]&lt;br /&gt;      then&lt;br /&gt;          echo " $i delete? " &gt;&gt; temp2.$$&lt;br /&gt;      fi&lt;br /&gt;&lt;br /&gt;   done&lt;br /&gt;&lt;br /&gt;   if [ -f temp2.$$ ]&lt;br /&gt;   then&lt;br /&gt;       dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;       --title "Select File to Delete" \&lt;br /&gt;       --menu "Use [UP/DOWN] keys to move, then press enter \&lt;br /&gt;\nFiles under directory $dirname:" 18 60 12 \&lt;br /&gt;       `cat temp2.$$` 2&gt; file2delete.$$&lt;br /&gt;   else&lt;br /&gt;     dialog --backtitle "MENU DRIVEN PROGRAM" --title "Select File to Delete" \&lt;br /&gt;      --msgbox "\n\n There are no regular files in $dirname directory" 10 60&lt;br /&gt;      return&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   rtval=$?&lt;br /&gt;&lt;br /&gt;   file2remove=`cat file2delete.$$`&lt;br /&gt;&lt;br /&gt;   case $rtval in&lt;br /&gt;&lt;br /&gt;       0) dialog --backtitle "MENU DRIVEN PROGRAM" --title "ARE YOU SURE" \&lt;br /&gt;          --yesno "\nDo you Want to Delete File: $file2remove" 7 70&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;          if [ $? -eq 0 ]&lt;br /&gt;          then&lt;br /&gt;              rm -f $file2remove 2&gt; Errorfile.$$&lt;br /&gt;&lt;br /&gt;              # Check file successfully deleted or not.&lt;br /&gt;&lt;br /&gt;              if [ $? -eq 0 ]&lt;br /&gt;              then&lt;br /&gt;                  dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;                  --title "Information : FILE DELETED" \&lt;br /&gt;                  --msgbox "\nFile : $file2remove deleted" 8 70&lt;br /&gt;             else&lt;br /&gt;                 dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;                 --title "Information : ERROR ON DELETION" \&lt;br /&gt;                 --msgbox "\nProblem in Deleting File: $file2remove \&lt;br /&gt;\n\nError: `cat Errorfile.$$` \n\nPress ENTER to return to the Main Menu" 12 70&lt;br /&gt;             fi&lt;br /&gt;&lt;br /&gt;          else&lt;br /&gt;              dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;              --title "Information : DELETION ABORTED" \&lt;br /&gt;              --msgbox "Action Aborted: \n\n $file2remove not deleted" 8 70&lt;br /&gt;          fi  ;;&lt;br /&gt;&lt;br /&gt;      *)  deletetempfiles               # Remove temporary files&lt;br /&gt;          return ;;&lt;br /&gt;   esac&lt;br /&gt;&lt;br /&gt;   deletetempfiles                      # remove temporary files&lt;br /&gt;   return&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;########################## currentusers function ############################&lt;br /&gt;&lt;br /&gt;currentusers()&lt;br /&gt;{&lt;br /&gt;   who &gt; userslist.$$&lt;br /&gt;   dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;   --title "CURRENTLY LOGGED IN USERS LIST" \&lt;br /&gt;   --textbox userslist.$$ 12 60&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;############################ diskstats function #############################&lt;br /&gt;&lt;br /&gt;diskstats()&lt;br /&gt;{&lt;br /&gt;   df -h | grep "^/" &gt; statsfile.$$&lt;br /&gt;   dialog --backtitle "MENU DRIVEN PROGRAM" \&lt;br /&gt;   --title "DISK STATISTICS" \&lt;br /&gt;   --textbox statsfile.$$ 10 60&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;##############################################################################&lt;br /&gt;#                           MAIN STRATS HERE                                 #&lt;br /&gt;##############################################################################&lt;br /&gt;&lt;br /&gt;trap 'deletetempfiles'  EXIT     # calls deletetempfiles function on exit&lt;br /&gt;&lt;br /&gt;while :&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;# Dialog utility to display options list&lt;br /&gt;&lt;br /&gt;    dialog --clear --backtitle "MENU DRIVEN PROGRAM" --title "MAIN MENU" \&lt;br /&gt;    --menu "Use [UP/DOWN] key to move" 12 60 6 \&lt;br /&gt;    "DATE_TIME" "TO DISPLAY DATE AND TIME" \&lt;br /&gt;    "CALENDAR"  "TO DISPLAY CALENDAR" \&lt;br /&gt;    "DELETE"    "TO DELETE FILES" \&lt;br /&gt;    "USERS"     "TO LIST CURRENTLY LOGGED IN USERS" \&lt;br /&gt;    "DISK"      "TO DISPLAY DISK STATISTICS" \&lt;br /&gt;    "EXIT"      "TO EXIT" 2&gt; menuchoices.$$&lt;br /&gt;&lt;br /&gt;    retopt=$?&lt;br /&gt;    choice=`cat menuchoices.$$`&lt;br /&gt;&lt;br /&gt;    case $retopt in&lt;br /&gt;&lt;br /&gt;           0) case $choice in&lt;br /&gt;&lt;br /&gt;                  DATE_TIME)  show_time ;;&lt;br /&gt;                  CALENDAR)   show_cal ;;&lt;br /&gt;                  DELETE)     deletefile ;;&lt;br /&gt;                  USERS)      currentusers ;;&lt;br /&gt;                  DISK)       diskstats ;;&lt;br /&gt;                  EXIT)       clear; exit 0;;&lt;br /&gt;&lt;br /&gt;              esac ;;&lt;br /&gt;&lt;br /&gt;          *)clear ; exit ;;&lt;br /&gt;    esac&lt;br /&gt;&lt;br /&gt;done &lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;[venu@localhost ~]$ sh menu_dialog.sh&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;SAMPLE SCREEN SHOTS:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Main Menu:&lt;/h5&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY10no89aI/AAAAAAAAAt8/RUB7ehjU1pA/s1600/menu.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 253px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY10no89aI/AAAAAAAAAt8/RUB7ehjU1pA/s400/menu.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5559189968145675682" /&gt;&lt;/a&gt;&lt;h5&gt;Users:&lt;/h5&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY2QjUQLwI/AAAAAAAAAuE/BDGO0XTZE6Y/s1600/users.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 253px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY2QjUQLwI/AAAAAAAAAuE/BDGO0XTZE6Y/s400/users.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5559190448021450498" /&gt;&lt;/a&gt;&lt;h5&gt;Disk Stats:&lt;/h5&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY2r7BXMrI/AAAAAAAAAuM/bLWohR6kUHw/s1600/stats.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 253px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY2r7BXMrI/AAAAAAAAAuM/bLWohR6kUHw/s400/stats.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5559190918241137330" /&gt;&lt;/a&gt;&lt;h5&gt;Delete File:&lt;/h5&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSY292wNbuI/AAAAAAAAAuU/oDX_S78cLoU/s1600/delete1.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 253px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSY292wNbuI/AAAAAAAAAuU/oDX_S78cLoU/s400/delete1.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5559191226333097698" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSY3GsEO6YI/AAAAAAAAAuc/s3DWfwm3TMw/s1600/delete2.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 296px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSY3GsEO6YI/AAAAAAAAAuc/s3DWfwm3TMw/s400/delete2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5559191378083113346" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6757878209207485849?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6757878209207485849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/01/menu-driven-shell-script-using-dialog.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6757878209207485849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6757878209207485849'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/01/menu-driven-shell-script-using-dialog.html' title='Menu Driven Shell Script - Using Dialog Utility'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSY10no89aI/AAAAAAAAAt8/RUB7ehjU1pA/s72-c/menu.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-9124292481638734839</id><published>2011-01-03T11:53:00.000-08:00</published><updated>2011-02-01T05:11:28.925-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Quicksort Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;   &lt;br /&gt;   Quicksort is a good example of the divide and conquer strategy for&lt;br /&gt;solving problems.  In quicksort,  we divide the array of items to be&lt;br /&gt;sorted into two partitions and then call the quicksort procedure recu-&lt;br /&gt;rsively to sort the two partitions, ie we divide the problem into two&lt;br /&gt;smaller ones and conquer by solving the smaller ones&lt;br /&gt;&lt;br /&gt;Quicksort recursive algorithm:&lt;br /&gt;&lt;br /&gt;1 Select an element (called as pivot) x(p) of x .&lt;br /&gt;2 Divide x into two batches x1 and x2 so that&lt;br /&gt;  all entries of x1 are &lt; x(p) and&lt;br /&gt;  all entries of x2 are &gt; x(p).&lt;br /&gt;  all entries of x3 are == x(p)&lt;br /&gt;  for each recursive call x3 will be placed to its sorted position.&lt;br /&gt;  To ﬁnish sorting we must sort x1 and x2.&lt;br /&gt;3 Apply steps 1 and 2 again to each of x1&lt;br /&gt;  and x2, using further subdivision.&lt;br /&gt;4 Repeat (recursively) until the sets to be&lt;br /&gt;  sorted have no more than one element.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT : quicksort.sh&lt;br /&gt;# USAGE  : quicksort.sh&lt;br /&gt;# PURPOSE: Sorts the list using quicksort algorithm.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;   echo ${ARRAY[*]}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sortnumbers()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    local array=( `echo "$@"` )&lt;br /&gt;    local -a l&lt;br /&gt;    local -a g&lt;br /&gt;    local -a e&lt;br /&gt;    local x=&lt;br /&gt;&lt;br /&gt;    if [ ${#array[@]} -lt 2 ]; then&lt;br /&gt;        echo -n ${array[@]}&lt;br /&gt;    else&lt;br /&gt;        local pivot=${array[0]}&lt;br /&gt;&lt;br /&gt;        for x in ${array[@]}&lt;br /&gt;        do&lt;br /&gt;&lt;br /&gt;          if [ $x -lt $pivot ]&lt;br /&gt;          then&lt;br /&gt;              l=( ${l[@]} $x )&lt;br /&gt;          elif [ $x -gt $pivot ]&lt;br /&gt;          then&lt;br /&gt;              g=( ${g[@]} $x )&lt;br /&gt;          else&lt;br /&gt;              e=(${e[@]} $x)&lt;br /&gt;          fi&lt;br /&gt;&lt;br /&gt;        done&lt;br /&gt;&lt;br /&gt;      echo "`sortnumbers "${l[@]}"` ${e[@]} `sortnumbers "${g[@]}"`"&lt;br /&gt;&lt;br /&gt;    fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Declaration                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;&lt;br /&gt;echo "Enter Numbers to be Sorted : "&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "--------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;echo "Numbers Before Sort:"&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;echo "Numbers After Sort: "&lt;br /&gt;&lt;br /&gt;sortnumbers "${ARRAY[@]}"&lt;br /&gt;&lt;br /&gt;echo "--------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[venu@localhost shell]$ sh quicksort.sh&lt;br /&gt;Enter Numbers to be Sorted :&lt;br /&gt;12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;Numbers Before Sort:&lt;br /&gt;12 54 32 90 76 54 -11 5 0 222 -46 32 -8 33 87 21 84 321 9&lt;br /&gt;Numbers After Sort:&lt;br /&gt;-46 -11 -8 0  5 9 12 21 32 32 33 54 54  76 84 87  90  222 321&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-9124292481638734839?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/9124292481638734839/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/01/quicksort-shell-script.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9124292481638734839'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9124292481638734839'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/01/quicksort-shell-script.html' title='Quicksort Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7844446100177030949</id><published>2011-01-03T11:23:00.000-08:00</published><updated>2011-01-03T11:56:58.029-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Binarysearch Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt; Binary search works by comparing an input value to the middle element&lt;br /&gt;of the array. The comparison determines whether the element equals the&lt;br /&gt;input, less than the input or greater. When the element being compared&lt;br /&gt;to equals the input the search stops and typically returns the posit-&lt;br /&gt;ion or number of searches of the element.  If the element is not equal&lt;br /&gt;to the input and the element at the middle point is greater than the&lt;br /&gt;input being searched, the current middle point becomes the new high&lt;br /&gt;point and the array is cut in half again and re-tested. If the element&lt;br /&gt;at the middle point is less than the input being searched, the current&lt;br /&gt;middle point becomes the new low point and the array is cut in half&lt;br /&gt;again and retested. This cutting in half and adjusting either the high&lt;br /&gt;point or the low point is repeated until the item is found or the low&lt;br /&gt;point and the high point converge. This is much faster then sequentia-&lt;br /&gt;lly searching an entire array.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT : binarysearch.sh&lt;br /&gt;# USAGE  : binarysearch.sh&lt;br /&gt;# PURPOSE: Searches given number in a sorted list.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;  echo ${ARRAY[*]}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sortnumbers()                               # Using insertion sort&lt;br /&gt;{&lt;br /&gt;for((i=1;i&amp;lt;count;i++))&lt;br /&gt;do&lt;br /&gt;   Temp=${ARRAY[i]}&lt;br /&gt;   j=$((i-1))&lt;br /&gt;   while [ $Temp -lt ${ARRAY[j]} ]&lt;br /&gt;   do&lt;br /&gt;      ARRAY[j+1]=${ARRAY[j]}&lt;br /&gt;      let j--&lt;br /&gt;      if [ $j == -1 ]&lt;br /&gt;      then&lt;br /&gt;         break&lt;br /&gt;      fi&lt;br /&gt;   done&lt;br /&gt;   ARRAY[j+1]=$Temp&lt;br /&gt;done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;binarysearch()&lt;br /&gt;{&lt;br /&gt;    status=-1&lt;br /&gt;    i=1&lt;br /&gt;    array=($(echo "$@"))&lt;br /&gt;    LowIndex=0&lt;br /&gt;    HeighIndex=$((${#array[@]}-1))&lt;br /&gt;&lt;br /&gt;    while [ $LowIndex -le $HeighIndex ]&lt;br /&gt;    do&lt;br /&gt;&lt;br /&gt;        MidIndex=$(($LowIndex+($HeighIndex-$LowIndex)/2))&lt;br /&gt;        MidElement=${array[$MidIndex]}&lt;br /&gt;&lt;br /&gt;        if [ $MidElement -eq $SearchedItem ]&lt;br /&gt;        then&lt;br /&gt;            status=0&lt;br /&gt;            searches=$i&lt;br /&gt;            return&lt;br /&gt;        elif [ $SearchedItem -lt $MidElement ]&lt;br /&gt;        then&lt;br /&gt;            HeighIndex=$(($MidIndex-1))&lt;br /&gt;        else&lt;br /&gt;            LowIndex=$(($MidIndex+1))&lt;br /&gt;        fi&lt;br /&gt;&lt;br /&gt;        let i++&lt;br /&gt;&lt;br /&gt;    done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Declaration                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;&lt;br /&gt;echo "Enter Array Elements : "&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;search=y&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# sort the loaded array, must for binary search.&lt;br /&gt;# You can apply any sorting algorithm. I applied insertion sort.&lt;br /&gt;&lt;br /&gt;sortnumbers&lt;br /&gt;&lt;br /&gt;echo "Array Elements After Sort: "&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;while [ "$search" == "y" -o "$search" == "Y" ]&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;     echo -n "Enter Element to be searched : "&lt;br /&gt;     read SearchedItem&lt;br /&gt;     binarysearch "${ARRAY[@]}"&lt;br /&gt;&lt;br /&gt;     if [ $status -eq 0 ]&lt;br /&gt;     then&lt;br /&gt;         echo "$SearchedItem found after $searches searches"&lt;br /&gt;     else&lt;br /&gt;         echo "$SearchedItem not found in the list"&lt;br /&gt;     fi&lt;br /&gt;&lt;br /&gt;     echo -n "Do you want another search (y/n): "&lt;br /&gt;     read search&lt;br /&gt;&lt;br /&gt;done&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[venu@localhost shell]$ chmod 755 binarysearch.sh&lt;br /&gt;[venu@localhost shell]$ ./binarysearch.sh&lt;br /&gt;Enter Array Elements :&lt;br /&gt;12 34 56 21 43 11 -32 87 112 -43 -111 98 100 22 0 11&lt;br /&gt;Array Elements After Sort:&lt;br /&gt;-111 -43 -32 0 11 11 12 21 22 34 43 56 87 98 100 112&lt;br /&gt;Enter Element to be searched : 21       # Middle element&lt;br /&gt;21 found after 1 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter Element to be searched : 112&lt;br /&gt;112 found after 5 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter Element to be searched : 56       # second middle element(upper)&lt;br /&gt;56 found after 2 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter Element to be searched : 0        # second middle element(lower)&lt;br /&gt;0 found after 2 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter Element to be searched : -111&lt;br /&gt;-111 found after 4 searches&lt;br /&gt;Do you want another search (y/n): n&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7844446100177030949?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7844446100177030949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/01/binarysearch-shell-script.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7844446100177030949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7844446100177030949'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/01/binarysearch-shell-script.html' title='Binarysearch Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-3395607259377515460</id><published>2011-01-03T03:28:00.000-08:00</published><updated>2011-01-03T03:38:05.843-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Linear Search Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT : linearsearch.sh&lt;br /&gt;# USAGE  : linearsearch.sh&lt;br /&gt;# PURPOSE: Searches given number in a list.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;# A variation of Here Document permits "commenting out" data block.&lt;br /&gt;: &amp;lt;&amp;lt;DATABLOCK&lt;br /&gt;&lt;br /&gt; In computer science, linear search or sequential search is a method&lt;br /&gt;for finding a particular value in a list, that consists in checking&lt;br /&gt;every one of its elements, one at a time and in sequence, until the&lt;br /&gt;desired one is found.&lt;br /&gt;&lt;br /&gt; This is a very straightforward loop comparing every element in the&lt;br /&gt;array with the key. As soon as an equal value is found, it returns.&lt;br /&gt;If the loop finishes without finding a match, the search failed and&lt;br /&gt;-1 is returned.&lt;br /&gt;&lt;br /&gt; For small arrays, a linear search is a good solution because it's so&lt;br /&gt;straightforward. In an array of a million elements, a linear search&lt;br /&gt;will take,on average, 500,000 comparisons to find the key. For a much&lt;br /&gt;faster search, take a look at binary search.&lt;br /&gt;&lt;br /&gt;DATABLOCK&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;lsearch()&lt;br /&gt;{&lt;br /&gt;   status=-1&lt;br /&gt;&lt;br /&gt;   for((i=0;i&amp;lt;count;i++))&lt;br /&gt;   do&lt;br /&gt;      Temp=$1&lt;br /&gt;&lt;br /&gt;      if [ $Temp -eq ${ARRAY[i]} ]&lt;br /&gt;       then&lt;br /&gt;&lt;br /&gt;          status=0&lt;br /&gt;          searches=$((i+1))&lt;br /&gt;          return&lt;br /&gt;&lt;br /&gt;#         return $((i+1))&lt;br /&gt;# Bash function can return value between 0-255, That's why I assigned&lt;br /&gt;# result to a global variable. This is one of the method to capture&lt;br /&gt;# return value of a function.&lt;br /&gt;&lt;br /&gt;      fi&lt;br /&gt;&lt;br /&gt;   done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Declaration                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;&lt;br /&gt;echo "Enter Array Elements : "&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;search=y&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;while [ "$search" == "y" -o "$search" == "Y" ]&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;     echo -n "Enter element to be searched : "&lt;br /&gt;     read num&lt;br /&gt;     lsearch $num&lt;br /&gt;&lt;br /&gt;     if [ $status -eq 0 ]&lt;br /&gt;     then&lt;br /&gt;         echo "$num found after $searches searches"&lt;br /&gt;     else&lt;br /&gt;         echo "$num not found"&lt;br /&gt;     fi&lt;br /&gt;&lt;br /&gt;     echo -n "Do you want another search (y/n): "&lt;br /&gt;     read search&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;$ sh linearsearch.sh&lt;br /&gt;Enter Array Elements :&lt;br /&gt;12 34 56 78 90 23 45 56 67 321 66 88 92&lt;br /&gt;Enter element to be searched : 56&lt;br /&gt;56 found after 3 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter element to be searched : 321&lt;br /&gt;321 found after 10 searches&lt;br /&gt;Do you want another search (y/n): y&lt;br /&gt;Enter element to be searched : 100&lt;br /&gt;100 not found&lt;br /&gt;Do you want another search (y/n): n&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-3395607259377515460?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/3395607259377515460/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/01/linear-search-shell-script.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3395607259377515460'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3395607259377515460'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/01/linear-search-shell-script.html' title='Linear Search Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-8673827064189558894</id><published>2011-01-02T08:19:00.001-08:00</published><updated>2011-02-01T05:12:50.364-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Happy New Year Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;&lt;h4&gt;Wish You a Happy New Year To All My Friends &lt;/h4&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSCyMJsAKsI/AAAAAAAAAtk/FMm9nroyqVg/s1600/newyear1.gif"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 301px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSCyMJsAKsI/AAAAAAAAAtk/FMm9nroyqVg/s400/newyear1.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5557637862003845826" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you don't know shell color codes, read bellow article first.&lt;br /&gt;&lt;a href="http://bashscript.blogspot.com/2010/01/shell-colors-colorizing-shell-scripts.html"&gt;How to colorize shell scripts&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  happynewyear.sh&lt;br /&gt;# PURPOSE: Prints Happy new year 2011 on screen.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;##############################################################################&lt;br /&gt;#                              Arguments Checking                            #&lt;br /&gt;##############################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 1 ]&lt;br /&gt;then&lt;br /&gt;    fname=$1&lt;br /&gt;else&lt;br /&gt;    echo "Invalid Arguments"&lt;br /&gt;    echo "Usage: happynewyear.sh filename"&lt;br /&gt;    exit&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;##############################################################################&lt;br /&gt;#                               Main Starts                                  #&lt;br /&gt;##############################################################################&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;i=0&lt;br /&gt;&lt;br /&gt;while read line&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;   if [ $i -lt 8 ] ; then&lt;br /&gt;       COLOR1="\e[1;31;40m" ; COLOR2="\e[0m"     # Dark Red &lt;br /&gt;   elif [ $i -gt 8 -a $i -lt 17 ] ; then&lt;br /&gt;       COLOR1="\e[1;37;40m" ; COLOR2="\e[0m"     # White &lt;br /&gt;   elif [ $i -gt 17 -a $i -lt 26 ] ; then&lt;br /&gt;       COLOR1="\e[1;32;40;5m" ; COLOR2="\e[0m"   # Dark Green with blinking &lt;br /&gt;   fi                                            # effect&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;   newline=`echo "$line" | tr '69' ' @' `&lt;br /&gt;   printf "$COLOR1$newline $COLOR2"        # you can also use echo -e "...."&lt;br /&gt;   let i++&lt;br /&gt;   sleep 0.1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;done &lt; $fname&lt;br /&gt;&lt;br /&gt;read                                        # Waits for enter&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Copy bellow data and save it as newyear.txt&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;6666666666996666699666666666966666666669999999996699999999966996666669966666666&lt;br /&gt;6666666666996666699666666669996666666669999999996699999999966699666699666666666&lt;br /&gt;6666666666996666699666666699669966666669966666996699666669966669966996666666666&lt;br /&gt;6666666666999999999666666996666996666669999999996699999999966666999966666666666&lt;br /&gt;6666666666999999999666669999999999666669999999996699999999966666699666666666666&lt;br /&gt;6666666666996666699666699999999999966669966666666699666666666666699666666666666&lt;br /&gt;6666666666996666699666996666666666996669966666666699666666666666699666666666666&lt;br /&gt;6666666666996666699669966666666666699669966666666699666666666666699666666666666&lt;br /&gt;6666666666666666666666666666666666666666666666666666666666666666666666666666666&lt;br /&gt;9966666996699999999669966666996666699666666996999999996666666696666666699999999&lt;br /&gt;9996666996699999999669966666996666669966669966999999996666666999666666699999999&lt;br /&gt;9999666996699666666669966666996666666996699666996666666666669969966666699666699&lt;br /&gt;9969966996699999999669966966996666666699996666999999996666699666996666699999999&lt;br /&gt;9966996996699999999669969996996666666669966666999999996666999999999666699999999&lt;br /&gt;9966699996699666666669999699996666666669966666996666666669999999999966699699666&lt;br /&gt;9966669996699999999669996669996666666669966666999999996699666666666996699669966&lt;br /&gt;9966666996699999999669966666996666666669966666999999996996666666666699699666699&lt;br /&gt;6666666666666666666666666666666666666666666666666666666666666666666666666666666&lt;br /&gt;6666666666666666666666699999999666999699999666999666699966666666666666666666666&lt;br /&gt;6666666666666666666666699999999666996666699666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666666666996666999666699666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666666669966666996696699666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666666699666666996669699666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666669966666666996666999666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666699999999666996666699666699666669966666666666666666666666&lt;br /&gt;6666666666666666666666699999999666999699999666999966699996666666666666666666666&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For blinking effect run this script in console, not in terminal.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;$ sh happynewyear.sh newyear.txt&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_ioCu0w5ZnYE/TSCnofxU6LI/AAAAAAAAAtE/MmIdB_Egs2Y/s1600/newyear2.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 500px; height: 350px;" src="http://4.bp.blogspot.com/_ioCu0w5ZnYE/TSCnofxU6LI/AAAAAAAAAtE/MmIdB_Egs2Y/s400/newyear2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5557626254340188338" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-8673827064189558894?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/8673827064189558894/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2011/01/happy-new-year-shell-script.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8673827064189558894'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8673827064189558894'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2011/01/happy-new-year-shell-script.html' title='Happy New Year Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_ioCu0w5ZnYE/TSCyMJsAKsI/AAAAAAAAAtk/FMm9nroyqVg/s72-c/newyear1.gif' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1104630138230741684</id><published>2010-12-26T07:36:00.000-08:00</published><updated>2010-12-26T07:42:07.490-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Math in Shell Scripts</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt; One thing that often confuses new users to the Unix / Linux shell, is&lt;br /&gt;how to do (even very simple) maths. In most languages, x = x + 1 (or&lt;br /&gt;even x++) does exactly what you would expect. The Unix/Linux shell is&lt;br /&gt;different,however. It doesn’t have any built-in mathematical operators&lt;br /&gt;for variables. It can do comparisons, but maths isn’t supported, not&lt;br /&gt;even simple addition.&lt;br /&gt;&lt;br /&gt; Shell script variables are by default treated as strings,not numbers,&lt;br /&gt;which adds some complexity to doing math in shell script. To keep with&lt;br /&gt;script programming paradigm and allow for better math support, langua-&lt;br /&gt;ges such Perl or Python would be better suited when math is desired.&lt;br /&gt;However, it is possible to do math with shell script.In fact, over the&lt;br /&gt;years, multiple facilities have been added to Unix to support working&lt;br /&gt;with numbers.&lt;br /&gt;&lt;br /&gt; You can do maths using any one of the following methods.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1. Using expr command&lt;br /&gt;2  Using $(()) construct.&lt;br /&gt;3  Using let command&lt;br /&gt;4  Using bc command.&lt;br /&gt;5  Using $[] construct.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;expr command:&lt;/h5&gt;&lt;br /&gt; expr command performs arithmetic operations on integers. It can&lt;br /&gt;perform the four basic arithmetic operations, as well as the modulus&lt;br /&gt;(remainder function).&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ expr 5 + 10&lt;br /&gt;15&lt;br /&gt;$ a=10 b=5&lt;br /&gt;$ expr $a + $b&lt;br /&gt;15&lt;br /&gt;$ expr $a / $b&lt;br /&gt;2&lt;br /&gt;$ expr $a * $b&lt;br /&gt;expr: syntax error&lt;br /&gt;$ expr $a \* $b&lt;br /&gt;50&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;  The operand, be it +,-,* etc.,  must be enclosed on either side by&lt;br /&gt;whitespace. Observe that the multiplication operand (*) has to be&lt;br /&gt;escaped to prevent the shell from interpreting it as the filename meta&lt;br /&gt;character. Since expr can handle only integers, division yields only&lt;br /&gt;the integral part.&lt;br /&gt;&lt;br /&gt; expr is often used with command substitution to assign a variable.&lt;br /&gt;For example, you can set a variable x to the sum of two numbers:&lt;br /&gt;&lt;br /&gt;$ x=`expr $a + $b`&lt;br /&gt;$ echo $x&lt;br /&gt;15&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt; &lt;b&gt;Note:&lt;/b&gt; As you can see, for expr, you must put spaces around the&lt;br /&gt; arguments: "expr 123+456" doesn’t work. "expr 123 + 456" works.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;With double parentheses: $(()) and (())&lt;/h5&gt;&lt;br /&gt; In bash version 3.2 and later you can (and should) use $(()) and (())&lt;br /&gt;for integer arithmetic expressions. You may have may not have spaces&lt;br /&gt;around the operators, but you must not have spaces around the equal&lt;br /&gt;sign, as with any bash variable assignment.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ c=$(($a+9))&lt;br /&gt;$ echo $c&lt;br /&gt;19&lt;br /&gt;$ c=$((a+9))             #Also correct, no need of $ sign.&lt;br /&gt;$ c=$((a + 9))           #Also correct, no restriction on spaces.&lt;br /&gt;$ c= $((a + b))          #Incorrect, space after assignment operator.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt; You may also use operations within double parentheses without&lt;br /&gt;assignment.&lt;br /&gt;&lt;br /&gt;$ ((a++))&lt;br /&gt;$ echo $a&lt;br /&gt;11&lt;br /&gt;$ ((a+=1)) ; echo $a&lt;br /&gt;12&lt;br /&gt;$ ((d=a+b+9)) ; echo $d&lt;br /&gt;26&lt;br /&gt;$ ((a+=$b))                #Correct&lt;br /&gt;$ (($a+=1))                #Incorrect&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;let command:&lt;/h5&gt;&lt;br /&gt; The let command carries out arithmetic operations on variables. In&lt;br /&gt;many cases, it functions as a less complex version of expr command.&lt;br /&gt;As you can see, it is also a little picky about spaces, but it wants&lt;br /&gt;the opposite of what expr wanted. let also relaxes the normal rule of&lt;br /&gt;needing a $ in front of variables to be read.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ let a=10            # Same as 'a=11'&lt;br /&gt;$ let a=a+5           # Equivalent to  let "a = a + 5"&lt;br /&gt;&lt;br /&gt;# Quotes permit the use of spaces in variable assignment. (Double&lt;br /&gt;# quotes and spaces make it more readable.)&lt;br /&gt;&lt;br /&gt;$ let a=$a + 5        # Without quotes spaces not allowed.&lt;br /&gt;bash: let: +: syntax error: operand expected (error token is "+")&lt;/div&gt;&lt;br /&gt;&lt;br /&gt; You need to use quotes if you want to use spaces between tokens of&lt;br /&gt;the expression, for example&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ let "a = a + 5";echo $a&lt;br /&gt;20&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; The only construct that is more convenient to use with let is incre-&lt;br /&gt;ment such as&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ let a++ ; echo $a       #  as well as to ((i++))&lt;br /&gt;16&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;bc: THE CALCULATOR&lt;/h5&gt;&lt;br /&gt; Bash doesn't support floating point arithmetic. The obvious candidate&lt;br /&gt;for adding floating point capabilities to bash is &lt;b&gt;bc&lt;/b&gt;. bc is not only a&lt;br /&gt;command, it also a pseudo-programming language featuring arrays,funct-&lt;br /&gt;ions,conditional(if) and loops(for and while). It also comes with a&lt;br /&gt;library for performing scientific calculations. It can handle very,&lt;br /&gt;very large numbers. If a computation results in a 900 digit number, bc&lt;br /&gt;will show each and every digit. But you have to treat the variables as&lt;br /&gt;strings.&lt;br /&gt;&lt;br /&gt; Here is what happens when we try to do floating point math with the&lt;br /&gt;shell:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ let a=12.5&lt;br /&gt;bash: let: a=12.5: syntax error: invalid arithmetic operator (error&lt;br /&gt;token is ".5")&lt;br /&gt;$ ((b=1*0.5))&lt;br /&gt;bash: ((: b=1*0.5: syntax error: invalid arithmetic operator (error&lt;br /&gt;token is ".5")&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;    I can't explain everything about bc here, it needs another post.&lt;br /&gt;But I will give some examples here.&lt;br /&gt;&lt;br /&gt;Most of the bellow examples follow a simple formula:&lt;br /&gt;&lt;br /&gt;$ echo '57+43' | bc&lt;br /&gt;100&lt;br /&gt;$ echo '57*43' | bc&lt;br /&gt;2451&lt;br /&gt;$ echo '6^6' | bc         # Power&lt;br /&gt;46656&lt;br /&gt;$ echo '1.5*5'|bc         # Allows floating point math.&lt;br /&gt;7.5&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;$[] construct: &lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ x=85&lt;br /&gt;$ y=15&lt;br /&gt;$ echo $[x+y]&lt;br /&gt;100&lt;br /&gt;$ echo $[x/y]&lt;br /&gt;5&lt;br /&gt;$ c=$[x*y]&lt;br /&gt;$ echo $c&lt;br /&gt;1275&lt;/div&gt;&lt;br /&gt;&lt;br /&gt; Working of above methods shell dependent. Bash shell supports all 5&lt;br /&gt;methods. Following shell script demonstrates above methods.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  basicmath.sh&lt;br /&gt;# USAGE:   basicmath.sh&lt;br /&gt;# PURPOSE: Addition, Subtraction, Division and Multiplication of&lt;br /&gt;#          two numbers.&lt;br /&gt;#                       \\\\ ////&lt;br /&gt;#                      \\  - -  //&lt;br /&gt;#                          @ @&lt;br /&gt;#                  ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Variable Declaration                         #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;clear                        #Clears Screen&lt;br /&gt;&lt;br /&gt;Bold="\033[1m"               #Storing escape sequences in a variable.&lt;br /&gt;Normal="\033[0m"&lt;br /&gt;&lt;br /&gt;echo -e "$Bold Basic mathematics using bash script $Normal\n"&lt;br /&gt;&lt;br /&gt;items="1. ADDITTION&lt;br /&gt;        2. SUBTRACTION&lt;br /&gt;        3. MULTIPLICATION&lt;br /&gt;        4. DIVISION&lt;br /&gt;        5. EXIT"&lt;br /&gt;&lt;br /&gt;choice=&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# If didn't understand these functions, simply remove functions and&lt;br /&gt;# its entries from main script.&lt;br /&gt;&lt;br /&gt;exit_function()&lt;br /&gt;{&lt;br /&gt;   clear&lt;br /&gt;   exit&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#Function enter is used to go  back to menu and clears screen&lt;br /&gt;&lt;br /&gt;enter()&lt;br /&gt;{&lt;br /&gt;   unset num1 num2&lt;br /&gt;   ans=&lt;br /&gt;   echo ""&lt;br /&gt;   echo -e  "Do you want to continue(y/n):\c"&lt;br /&gt;   stty -icanon min 0 time 0&lt;br /&gt;&lt;br /&gt;# When -icanon is set then one character has been received.&lt;br /&gt;# min 0 means that read should read 0 characters.&lt;br /&gt;# time 0 ensures that read is terminated the moment one character&lt;br /&gt;# is hit.&lt;br /&gt;&lt;br /&gt;while [ -z "$ans" ]&lt;br /&gt;do&lt;br /&gt;   read ans&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;#The while loop ensures that so long as at least one character is&lt;br /&gt;# not received &lt;b&gt;read&lt;/b&gt; continue to get executed&lt;br /&gt;&lt;br /&gt;if [ "$ans" = "y" -o "$ans" = "Y" ]&lt;br /&gt;then&lt;br /&gt;   stty sane          # Restoring terminal settings&lt;br /&gt;   clear&lt;br /&gt;else&lt;br /&gt;   stty sane&lt;br /&gt;   exit_function&lt;br /&gt;fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                         Main Starts                               #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;while true&lt;br /&gt;do&lt;br /&gt;   echo -e "$Bold \tPROGRAM MENU $Normal\n"&lt;br /&gt;   echo -e "\t$items \n"&lt;br /&gt;   echo -n "Enter your choice : "&lt;br /&gt;   read choice&lt;br /&gt;&lt;br /&gt;   case $choice in&lt;br /&gt;     1) clear&lt;br /&gt;        echo  "Enter two numbers for Addition : "&lt;br /&gt;        echo -n "Number1: "&lt;br /&gt;        read num1&lt;br /&gt;        echo -n "Number2: "&lt;br /&gt;        read num2&lt;br /&gt;        echo "$num1 + $num2 = `expr $num1 + $num2`"&lt;br /&gt;        enter ;;&lt;br /&gt;     2) clear&lt;br /&gt;        echo "Enter two numbers for Subtraction : "&lt;br /&gt;        echo -n "Number1: "&lt;br /&gt;        read num1&lt;br /&gt;        echo -n "Number2: "&lt;br /&gt;        read num2&lt;br /&gt;        echo "$num1 - $num2 = $((num1-num2))"&lt;br /&gt;        enter ;;&lt;br /&gt;     3) clear&lt;br /&gt;        echo "Enter two numbers for Multiplication : "&lt;br /&gt;        echo -n "Number1: "&lt;br /&gt;        read num1&lt;br /&gt;        echo -n "Number2: "&lt;br /&gt;        read num2&lt;br /&gt;        echo "$num1 * $num2 = `echo "$num1*$num2"|bc`"&lt;br /&gt;        enter ;;&lt;br /&gt;    4)  clear&lt;br /&gt;        echo "Enter two numbers for Division : "&lt;br /&gt;        echo -n "Number1: "&lt;br /&gt;        read num1&lt;br /&gt;        echo -n "Number2: "&lt;br /&gt;        read num2&lt;br /&gt;        let div=num1/num2&lt;br /&gt;        echo "$num1 / $num2 = $div"&lt;br /&gt;        enter ;;&lt;br /&gt;    5)  exit_function  ;;&lt;br /&gt;    *)  echo "You entered wrong option, Please enter 1,2,3,4 or 5"&lt;br /&gt;        echo "Press enter to continue"&lt;br /&gt;        read&lt;br /&gt;        clear&lt;br /&gt;   esac&lt;br /&gt;&lt;br /&gt;done &lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt; [venu@localhost ~]$ sh basicmath.sh&lt;br /&gt;&lt;br /&gt; Basic mathematics using bash script&lt;br /&gt;&lt;br /&gt;        PROGRAM MENU&lt;br /&gt;&lt;br /&gt;        1. ADDITTION&lt;br /&gt;        2. SUBTRACTION&lt;br /&gt;        3. MULTIPLICATION&lt;br /&gt;        4. DIVISION&lt;br /&gt;        5. EXIT&lt;br /&gt;&lt;br /&gt;Enter your choice : 1&lt;br /&gt;&lt;br /&gt;Enter two numbers for Addition :&lt;br /&gt;Number1: 123&lt;br /&gt;Number2: 456&lt;br /&gt;123 + 456 = 579&lt;br /&gt;&lt;br /&gt;Do you want to continue(y/n):y&lt;br /&gt;&lt;br /&gt;        PROGRAM MENU&lt;br /&gt;&lt;br /&gt;        1. ADDITTION&lt;br /&gt;        2. SUBTRACTION&lt;br /&gt;        3. MULTIPLICATION&lt;br /&gt;        4. DIVISION&lt;br /&gt;        5. EXIT&lt;br /&gt;&lt;br /&gt;Enter your choice : 3&lt;br /&gt;Enter two numbers for Multiplication :&lt;br /&gt;Number1: 12.5&lt;br /&gt;Number2: 2&lt;br /&gt;12.5 * 2 = 25.0&lt;br /&gt;&lt;br /&gt;Do you want to continue(y/n):n&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1104630138230741684?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1104630138230741684/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/math-in-shell-scripts.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1104630138230741684'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1104630138230741684'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/math-in-shell-scripts.html' title='Math in Shell Scripts'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-3028867416993698951</id><published>2010-12-22T12:38:00.000-08:00</published><updated>2011-02-01T05:14:32.903-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Generate Fibonacci Series</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;  By definition in mathematics, the Fibonacci Numbers are the numbers&lt;br /&gt;in the below sequence:&lt;br /&gt;0,1,1,2,3,5,8,13,21,34,55,89,144, ......&lt;br /&gt;  By definition, the first two Fibonacci numbers are 0 and 1, and each&lt;br /&gt;subsequent number is the sum of the previous two. Some sources omit&lt;br /&gt;the initial 0, instead beginning the sequence with two 1s.&lt;br /&gt;  In mathematical terms, the sequence Fn of Fibonacci numbers is defi-&lt;br /&gt;ned by the recurrence relation&lt;br /&gt;    Fn = Fn-1 + Fn-2,&lt;br /&gt;with seed values&lt;br /&gt;   F0 = 0 and F1 = 1.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Iterative Method: &lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash#!/bin/bash&lt;br /&gt;# SCRIPT:  fibo_iterative.sh&lt;br /&gt;# USAGE:   fibo_iterative.sh [Number]&lt;br /&gt;# PURPOSE: Generate Fibonacci sequence.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Script Starts Here                            #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 1 ]&lt;br /&gt;then&lt;br /&gt;    Num=$1&lt;br /&gt;else&lt;br /&gt;    echo -n "Enter a Number :"&lt;br /&gt;    read Num&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;f1=0&lt;br /&gt;f2=1&lt;br /&gt;&lt;br /&gt;echo "The Fibonacci sequence for the number $Num is : "&lt;br /&gt;&lt;br /&gt;for (( i=0;i&lt;=Num;i++ ))&lt;br /&gt;do&lt;br /&gt;     echo -n "$f1 "&lt;br /&gt;     fn=$((f1+f2))&lt;br /&gt;     f1=$f2&lt;br /&gt;     f2=$fn&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh fibo_iterative.sh 18&lt;br /&gt;The Fibonacci sequence for the number 18 is : &lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 &lt;br /&gt;# sh fibo_iterative.sh&lt;br /&gt;Enter a Number :20&lt;br /&gt;The Fibonacci sequence for the number 20 is : &lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 &lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Recursive Method:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  fibo_recursive.sh&lt;br /&gt;# USAGE:   fibo_recursive.sh [Number]&lt;br /&gt;# PURPOSE: Generate Fibonacci sequence.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 1 ]&lt;br /&gt;then&lt;br /&gt;    Num=$1&lt;br /&gt;else&lt;br /&gt;    echo -n "Enter a Number : "&lt;br /&gt;    read Num&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;Fibonacci()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;case $1 in&lt;br /&gt; 0|1) printf "$1 " ;;&lt;br /&gt; *) echo -n "$(( $(Fibonacci $(($1-2)))+$(Fibonacci $(($1-1))) )) ";;&lt;br /&gt;esac&lt;br /&gt;&lt;br /&gt;#$(( )) construct is used instead of &lt;b&gt;expr&lt;/b&gt; command for doing addition.&lt;br /&gt;#$( ) constrict is used instead of back ticks.&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Main Script Starts Here                      #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "The Fibonacci sequence for the number $Num is : "&lt;br /&gt;&lt;br /&gt;for (( i=0; i&lt;=$Num; i++ ))&lt;br /&gt;do&lt;br /&gt;  Fibonacci $i                     #Calling function Fibonacci&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh fibo_recursive.sh &lt;br /&gt;Enter a Number : 11&lt;br /&gt;The Fibonacci sequence for the number 11 is : &lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 &lt;br /&gt;# sh fibo_recursive.sh 13&lt;br /&gt;The Fibonacci sequence for the number 13 is : &lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 144 233 &lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Be aware that recursion is resource-intensive and executes slowly,&lt;br /&gt;and is therefore generally not appropriate to use in a script.&lt;br /&gt;&lt;br /&gt;[root@localhost shell]# time ./fibo_iterative.sh 15&lt;br /&gt;The Fibonacci sequence for the number 15 is :&lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610&lt;br /&gt;&lt;br /&gt;real    0m0.008s&lt;br /&gt;user    0m0.008s&lt;br /&gt;sys     0m0.000s&lt;br /&gt;[root@localhost shell]# time ./fibo_recursive.sh 15&lt;br /&gt;The Fibonacci sequence for the number 15 is :&lt;br /&gt;0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610&lt;br /&gt;&lt;br /&gt;real    0m7.875s&lt;br /&gt;user    0m0.908s&lt;br /&gt;sys     0m5.188s&lt;br /&gt;&lt;br /&gt;Too many levels of recursion may crash a script with a segfault.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-3028867416993698951?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/3028867416993698951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-generate-fibonacci.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3028867416993698951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3028867416993698951'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-generate-fibonacci.html' title='Shell Script to Generate Fibonacci Series'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-4462374593516320790</id><published>2010-12-20T12:08:00.000-08:00</published><updated>2011-02-01T05:15:50.987-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Find Armstrong Numbers Between a Given Range</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  armstrong_bw_range.sh&lt;br /&gt;# USAGE:   armstrong_bw_range.sh&lt;br /&gt;# PURPOSE: Finding Armstrong numbers between given range.&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;#Armstrong numbers are the sum of their own digits to the power of&lt;br /&gt;#the number of digits. As that is a slightly brief wording, let me&lt;br /&gt;#give an example:&lt;br /&gt;#153 = 1³ + 5³ + 3³&lt;br /&gt;#Each digit is raised to the power three because 153 has three&lt;br /&gt;#digits. They are totalled and we get the original number again!&lt;br /&gt;#Notice that Armstrong numbers are base dependent,but we'll mainly be&lt;br /&gt;#dealing with base 10 examples.The Armstrong numbers up to 5 digits&lt;br /&gt;#are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Script Starts Here                            #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter the Lower Limit : "&lt;br /&gt;read Start&lt;br /&gt;echo -n "Enter the Upper Limit : "&lt;br /&gt;read Ending&lt;br /&gt;&lt;br /&gt;echo "Armstrong Numbers between $Start and $Ending are: "&lt;br /&gt;&lt;br /&gt;while [ $Start -le $Ending ]&lt;br /&gt;do&lt;br /&gt;     Number=$Start&lt;br /&gt;     Length=${#Number}&lt;br /&gt;     Sum=0&lt;br /&gt;     OldNumber=$Number&lt;br /&gt;&lt;br /&gt;     while [ $Number -ne  0 ]&lt;br /&gt;     do&lt;br /&gt;          Rem=$((Number%10))&lt;br /&gt;          Number=$((Number/10))&lt;br /&gt;          Power=$(echo "$Rem ^ $Length" | bc )&lt;br /&gt;          Sum=$((Sum+Power))&lt;br /&gt;     done&lt;br /&gt;&lt;br /&gt;     if [ $Sum -eq $OldNumber ]&lt;br /&gt;     then&lt;br /&gt;         echo -n "$OldNumber  "&lt;br /&gt;     fi&lt;br /&gt;&lt;br /&gt;     let Start++&lt;br /&gt;done&lt;br /&gt;echo&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh armstrong_bw_range.sh&lt;br /&gt;Enter the Lower Limit : 1&lt;br /&gt;Enter the Upper Limit : 500&lt;br /&gt;Armstrong Numbers between 1 and 500 are:&lt;br /&gt;1  2  3  4  5  6  7  8  9  153  370  371  407&lt;br /&gt;&lt;br /&gt;# sh armstrong_bw_range.sh&lt;br /&gt;Enter the Lower Limit : 1000&lt;br /&gt;Enter the Upper Limit : 10000&lt;br /&gt;Armstrong Numbers between 1000 and 10000 are:&lt;br /&gt;1634  8208  9474&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-4462374593516320790?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/4462374593516320790/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-find-armstrong-numbers.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4462374593516320790'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4462374593516320790'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-find-armstrong-numbers.html' title='Shell Script to Find Armstrong Numbers Between a Given Range'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7609163744332713599</id><published>2010-12-20T12:00:00.000-08:00</published><updated>2011-02-01T05:17:17.295-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Check Armstrong Number</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  armstrongnumber.sh&lt;br /&gt;# USAGE:   armstrongnumber.sh&lt;br /&gt;# PURPOSE: Check if the given number is Armstrong number ?&lt;br /&gt;#                         \\\\ ////&lt;br /&gt;#                         \\ - - //&lt;br /&gt;#                            @ @&lt;br /&gt;#                    ---oOOo-( )-oOOo---&lt;br /&gt;# Armstrong numbers are the sum of their own digits to the power of&lt;br /&gt;# the number of digits. As that is a slightly brief wording, let me&lt;br /&gt;# give an example:&lt;br /&gt;# 153 = 1³ + 5³ + 3³&lt;br /&gt;# Each digit is raised to the power three because 153 has three&lt;br /&gt;# digits. They are totalled and we get the original number again!&lt;br /&gt;#Notice that Armstrong numbers are base dependent,but we'll mainly be&lt;br /&gt;# dealing with base 10 examples.The Armstrong numbers up to 5 digits&lt;br /&gt;# are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727,93084&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Script Starts Here                            #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter the number: "&lt;br /&gt;read Number&lt;br /&gt;Length=${#Number}&lt;br /&gt;Sum=0&lt;br /&gt;OldNumber=$Number&lt;br /&gt;&lt;br /&gt;while [ $Number -ne  0 ]&lt;br /&gt;do&lt;br /&gt;     Rem=$((Number%10))&lt;br /&gt;     Number=$((Number/10))&lt;br /&gt;     Power=$(echo "$Rem ^ $Length" | bc )&lt;br /&gt;     Sum=$((Sum+$Power))&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;if [ $Sum -eq $OldNumber ]&lt;br /&gt;then&lt;br /&gt;    echo "$OldNumber is an Armstrong number"&lt;br /&gt;else&lt;br /&gt;    echo "$OldNumber is not an Armstrong number"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh armstrongnumber.sh&lt;br /&gt;Enter the number: 8208&lt;br /&gt;8208 is an Armstrong number&lt;br /&gt;# sh armstrongnumber.sh&lt;br /&gt;Enter the number: 4210818&lt;br /&gt;4210818 is an Armstrong number&lt;br /&gt;# sh armstrongnumber.sh&lt;br /&gt;Enter the number: 4376541&lt;br /&gt;4376541 is not an Armstrong number&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7609163744332713599?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7609163744332713599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-check-armstrong-number.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7609163744332713599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7609163744332713599'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/shell-script-to-check-armstrong-number.html' title='Shell Script to Check Armstrong Number'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-8849759956256160232</id><published>2010-12-20T06:28:00.000-08:00</published><updated>2010-12-20T06:37:31.783-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Decimal to Binary Conversion Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  dec2binary.sh&lt;br /&gt;# USAGE:   dec2binary.sh Decimal_Number(s)&lt;br /&gt;# PURPOSE: Decimal to Binary Conversion. Takes input as command line&lt;br /&gt;#          arguments.&lt;br /&gt;#                        \\\\ ////&lt;br /&gt;#                       \\  - -  //&lt;br /&gt;#                           @ @&lt;br /&gt;#                   ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Script Starts Here                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    echo "Argument(s) not supplied "&lt;br /&gt;    echo "Usage: dec2binary.sh Decimal_number(s)"&lt;br /&gt;else&lt;br /&gt;echo -e "\033[1mDECIMAL             \t\t BINARY\033[0m"&lt;br /&gt;&lt;br /&gt;    while [ $# -ne 0 ]&lt;br /&gt;    do&lt;br /&gt;         DecNum=$1&lt;br /&gt;         Binary=&lt;br /&gt;         Number=$DecNum&lt;br /&gt;&lt;br /&gt;         while [ $DecNum -ne 0 ]&lt;br /&gt;         do&lt;br /&gt;              Bit=$(expr $DecNum % 2)&lt;br /&gt;              Binary=$Bit$Binary&lt;br /&gt;              DecNum=$(expr $DecNum / 2)&lt;br /&gt;         done&lt;br /&gt;&lt;br /&gt;         echo -e "$Number              \t\t $Binary"&lt;br /&gt;         shift&lt;br /&gt;# Shifts command line arguments one step.Now $1 holds second argument&lt;br /&gt;        unset Binary&lt;br /&gt;   done&lt;br /&gt;&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;#NOTE:&lt;/b&gt; Using &lt;b&gt;bc&lt;/b&gt; command you can directly get output at command line.&lt;br /&gt;#   $ echo 'obase=2;15' | bc&lt;br /&gt;#     1111&lt;br /&gt;#   $ echo 'obase=2;1023' | bc&lt;br /&gt;#     1111111111&lt;br /&gt;#   $ echo 'obase=2;1024' | bc&lt;br /&gt;#     10000000000&lt;br /&gt;# No need of ibase=10, because default ibase is 10. &lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh dec2binary.sh 7 16 255 256 1023 1024&lt;br /&gt;DECIMAL                          BINARY&lt;br /&gt;7                                111&lt;br /&gt;16                               10000&lt;br /&gt;255                              11111111&lt;br /&gt;256                              100000000&lt;br /&gt;1023                             1111111111&lt;br /&gt;1024                             10000000000&lt;br /&gt;&lt;br /&gt;# sh dec2binary.sh 9223372036854775807&lt;br /&gt;DECIMAL                          BINARY&lt;br /&gt;9223372036854775807              1111111111111111111111111111111111111&lt;br /&gt;11111111111111111111111111&lt;br /&gt;&lt;br /&gt;#sh dec2binary.sh 9223372036854775808&lt;br /&gt;DECIMAL                          BINARY&lt;br /&gt;dec2binary.sh:line 13:[:9223372036854775808:integer expression expected&lt;br /&gt;9223372036854775808&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;NOTE:&lt;/b&gt;My bash can handle upto 9223372036854775807.&lt;br /&gt;But with &lt;b&gt;bc&lt;/b&gt; command you can do more.&lt;/div&gt;&lt;br /&gt;# echo 'obase=2;9223372036854775808'|bc&lt;br /&gt;1000000000000000000000000000000000000000000000000000000000000000&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-8849759956256160232?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/8849759956256160232/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/decimal-to-binary-conversion-shell.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8849759956256160232'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8849759956256160232'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/decimal-to-binary-conversion-shell.html' title='Decimal to Binary Conversion Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1957797971293865066</id><published>2010-12-20T06:22:00.000-08:00</published><updated>2010-12-20T06:38:43.668-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Binary to Decimal Conversion Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  binary2dec.sh&lt;br /&gt;# USAGE:   binary2dec.sh Binary_Numbers(s)&lt;br /&gt;# PURPOSE: Binary to Decimal Conversion. Takes input as command line&lt;br /&gt;#          arguments.&lt;br /&gt;#                        \\\\ ////&lt;br /&gt;#                       \\  - -  //&lt;br /&gt;#                           @ @&lt;br /&gt;#                   ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Script Starts Here                           #&lt;br /&gt;#####################################################################&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    echo "Argument(s) not supplied "&lt;br /&gt;    echo "Usage: binary2dec.sh Binary_Number(s)"&lt;br /&gt;else&lt;br /&gt;    echo -e "\033[1mBINARY             \t\t DECIMAL\033[0m"&lt;br /&gt;&lt;br /&gt;    while [ $# -ne 0 ]&lt;br /&gt;    do&lt;br /&gt;         Binary=$1&lt;br /&gt;         Bnumber=$Binary&lt;br /&gt;         Decimal=0&lt;br /&gt;         power=1&lt;br /&gt;&lt;br /&gt;         while [ $Binary -ne 0 ]&lt;br /&gt;         do&lt;br /&gt;              rem=$(expr $Binary % 10 )&lt;br /&gt;              Decimal=$((Decimal+(rem*power)))&lt;br /&gt;              power=$((power*2))&lt;br /&gt;              Binary=$(expr $Binary / 10)&lt;br /&gt;         done&lt;br /&gt;&lt;br /&gt;         echo -e "$Bnumber             \t\t $Decimal"&lt;br /&gt;         shift&lt;br /&gt;    done&lt;br /&gt;&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;#NOTE:&lt;/b&gt; Using bc command you can directly get output at command line.&lt;br /&gt;# Here we're converting the binary number 111 to a base 10 (decimal)&lt;br /&gt;# number.&lt;br /&gt;#&lt;br /&gt;# $ echo 'ibase=2;obase=A;111' | bc&lt;br /&gt;# 7&lt;br /&gt;#&lt;br /&gt;#Note that the obase is "A" and not "10".The reason for this is you've&lt;br /&gt;# set the ibase to 2, so if you now had tried to use "10" as the value&lt;br /&gt;# for the obase, it would stay as "2", because "10" in base 2 is "2".&lt;br /&gt;# So you need to use hex to break out of binary mode or use obase=1010&lt;br /&gt;#&lt;br /&gt;# $ echo 'ibase=2;obase=1010;10000' | bc&lt;br /&gt;# 16&lt;br /&gt;# $ echo 'ibase=2;obase=1010;1000001' | bc&lt;br /&gt;# 65&lt;br /&gt;#&lt;br /&gt;# You can omit obase=A, because default obase is 10(decimal).&lt;br /&gt;# $ echo 'ibase=2;1000001' | bc&lt;br /&gt;# 65&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# sh binary2dec.sh 100 1111 1111111 1000000&lt;br /&gt;BINARY                           DECIMAL&lt;br /&gt;100                              4&lt;br /&gt;1111                             15&lt;br /&gt;1111111                          127&lt;br /&gt;1000000                          64&lt;br /&gt;&lt;br /&gt;# sh binary2dec.sh 1111111111111111111&lt;br /&gt;BINARY                           DECIMAL&lt;br /&gt;1111111111111111111              524287&lt;br /&gt;&lt;br /&gt;# sh binary2dec.sh 10000000000000000000&lt;br /&gt;BINARY                           DECIMAL&lt;br /&gt;binary2dec.sh:line 28:[:10000000000000000000:integer expression expected&lt;br /&gt;10000000000000000000&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;NOTE:&lt;/b&gt;My bash can handle upto 9223372036854775807.&lt;br /&gt;But with &lt;b&gt;bc&lt;/b&gt; command you can do more.&lt;/div&gt;&lt;br /&gt;# echo 'ibase=2;10000000000000000000'|bc&lt;br /&gt;524288&lt;br /&gt;# echo 'ibase=2;110000000000000000000'|bc&lt;br /&gt;1572864&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1957797971293865066?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1957797971293865066/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/binary-to-decimal-conversion-shell.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1957797971293865066'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1957797971293865066'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/binary-to-decimal-conversion-shell.html' title='Binary to Decimal Conversion Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6477235244400152026</id><published>2010-12-19T11:25:00.000-08:00</published><updated>2011-01-07T08:59:50.057-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>How to Validate Integer Input using Shell script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;h4&gt;Method 1:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  validinteger.sh&lt;br /&gt;# USAGE:   validinteger.sh [ Input value to be validated ]&lt;br /&gt;# PURPOSE: validate integer input, allow negative integers also&lt;br /&gt;#&lt;br /&gt;#             \\\\ ////&lt;br /&gt;#            \\  - -  //&lt;br /&gt;#                @ @&lt;br /&gt;#        ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;# Using the $? exit status variable or integer comparison operators, a&lt;br /&gt;# script may test if a parameter contains only digits, so it can be&lt;br /&gt;# treated as an integer.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    echo -n "Enter input to test: "&lt;br /&gt;    read Number&lt;br /&gt;else&lt;br /&gt;    Number=$1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# You can also use bellow one liner&lt;br /&gt;#[ $# -eq 0 ] &amp;&amp; { echo -n "Enter input:";read Number; } || Number=$1&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# Check the input is an integer or not&lt;br /&gt;&lt;br /&gt;if [ $Number -ne 0 -o $Number -eq 0 2&gt;/dev/null ]&lt;br /&gt;then&lt;br /&gt;&lt;br /&gt;# An integer is either equal to 0 or not equal to 0.&lt;br /&gt;# 2&gt;/dev/null suppresses error message.&lt;br /&gt;&lt;br /&gt;    echo "Supplied Input $Number is an Integer"&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;    echo "Supplied Input $Number is not an Integer."&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# You can compare number itself also&lt;br /&gt;&lt;br /&gt;#  if [ $Number -eq $Number 2&gt; /dev/null ]&lt;br /&gt;#  then&lt;br /&gt;#    echo "Supplied Input $Number is an Integer"&lt;br /&gt;#  else&lt;br /&gt;#    echo "Supplied Input $Number is not an Integer."&lt;br /&gt;#  fi&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# chmod 755 validinteger.sh&lt;br /&gt;# ./validinteger.sh -12345&lt;br /&gt;Supplied Input -12345 is an Integer&lt;br /&gt;# ./validinteger.sh 12345A&lt;br /&gt;Supplied Input 12345A is not an Integer.&lt;br /&gt;# ./validinteger.sh&lt;br /&gt;Enter input to test: 876549&lt;br /&gt;Supplied Input 876549 is an Integer&lt;br /&gt;# ./validinteger.sh&lt;br /&gt;Enter input to test: -345k123&lt;br /&gt;Supplied Input -345k123 is not an Integer.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 2 : &lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  validinteger2.sh&lt;br /&gt;# USAGE:   validinteger2.sh [ Input value to be validated ]&lt;br /&gt;# PURPOSE: validate integer input, allow negative integers also&lt;br /&gt;#&lt;br /&gt;#&lt;br /&gt;#             \\\\ ////&lt;br /&gt;#            \\  - -  //&lt;br /&gt;#                @ @&lt;br /&gt;#        ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;# In this method if you want to allow negative integers, it gets a&lt;br /&gt;# tiny bit more complicated. You must have basic knowledge of string&lt;br /&gt;# manipulation methods.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    echo -n "Enter input to test: "&lt;br /&gt;    read Number&lt;br /&gt;else&lt;br /&gt;    Number=$1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# Check first character is -&lt;br /&gt;&lt;br /&gt;if [ "${Number:0:1}" = "-" ]    #extract first character&lt;br /&gt;then&lt;br /&gt;    RemNumber="${Number:1}"     #extract except first char&lt;br /&gt;else&lt;br /&gt;    RemNumber="$Number"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# You can extract substring using bellow method also&lt;br /&gt;&lt;br /&gt;#if [ "$(echo $Number|cut -c 1)" = "-" ]&lt;br /&gt;#then&lt;br /&gt;#    RemNumber=$(echo $Number|cut -c 2-)&lt;br /&gt;#else&lt;br /&gt;#    RemNumber="$Number"&lt;br /&gt;#fi&lt;br /&gt;&lt;br /&gt;if [ -z "$RemNumber" ]&lt;br /&gt;then&lt;br /&gt;    echo "Supplied Input $Number is not an Integer"&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;    NoDigits="$(echo $RemNumber | sed 's/[[:digit:]]//g')"&lt;br /&gt;&lt;br /&gt;    if [ -z "$NoDigits" ]&lt;br /&gt;    then&lt;br /&gt;        echo "Supplied Input $Number is an Integer"&lt;br /&gt;    else&lt;br /&gt;        echo "Supplied Input $Number is not an Integer."&lt;br /&gt;    fi&lt;br /&gt;fi&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;# chmod 755 validinteger2.sh&lt;br /&gt;# ./validinteger2.sh -987612&lt;br /&gt;Supplied Input -987612 is an Integer&lt;br /&gt;# ./validinteger2.sh -A987612&lt;br /&gt;Supplied Input -A987612 is not an Integer.&lt;br /&gt;# ./validinteger2.sh&lt;br /&gt;Enter input to test: -123456789&lt;br /&gt;Supplied Input -123456789 is an Integer&lt;br /&gt;# ./validinteger2.sh&lt;br /&gt;Enter input to test: A234K78&lt;br /&gt;Supplied Input A234K78 is not an Integer.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 3: &lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  validinteger3.sh&lt;br /&gt;# USAGE:   validinteger3.sh [ Input value to be validated ]&lt;br /&gt;# PURPOSE: validate integer input, allow negative integers also&lt;br /&gt;#&lt;br /&gt;#                           \\\\ ////&lt;br /&gt;#                          \\  - -  //&lt;br /&gt;#                              @ @&lt;br /&gt;#                      ---oOOo-( )-oOOo---&lt;br /&gt;#&lt;br /&gt;# In this method input value validated using ASCII value range of&lt;br /&gt;# numbers (48 - 57).&lt;br /&gt;# Sample script provided by Bond, Thank you Bond.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    echo -n "Enter input to test: "&lt;br /&gt;    read Number&lt;br /&gt;else&lt;br /&gt;    Number=$1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# You can also use bellow one liner&lt;br /&gt;#[ $# -eq 0 ] &amp;&amp; { echo -n "Enter input:";read Number; } || Number=$1&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# Check first character is - ?&lt;br /&gt;# This is another method to extract substring using Substring Removal&lt;br /&gt;# method.&lt;br /&gt;&lt;br /&gt;  if [ "${Number%${Number#?}}" = "-" ]  #extract first char&lt;br /&gt;  then&lt;br /&gt;      RemNumber="${Number#?}"           #extract all except first char&lt;br /&gt;  else&lt;br /&gt;      RemNumber="$Number"&lt;br /&gt;  fi&lt;br /&gt;&lt;br /&gt;if [ -z "$RemNumber" ]&lt;br /&gt;then&lt;br /&gt;    echo "Supplied Input $Number is not an Integer"&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;    Length=${#RemNumber}              # Counting length of the string&lt;br /&gt;&lt;br /&gt;    for ((i=0; i &amp;lt; Length; i++))&lt;br /&gt;    do&lt;br /&gt;&lt;br /&gt;       char=${RemNumber:$i:1}          # Reads character one by one&lt;br /&gt;&lt;br /&gt;       code=`printf '%d' "'$char"`     # Get ASCII value of char&lt;br /&gt;&lt;br /&gt;       # Compare code with ASCII value range of Intergers ( 48 - 57 )&lt;br /&gt;&lt;br /&gt;       if [ $code -lt 48 ] || [ $code -gt 57 ]&lt;br /&gt;       then&lt;br /&gt;           echo "Supplied Input $Number is not an Integer"&lt;br /&gt;           exit 1&lt;br /&gt;       fi&lt;br /&gt;&lt;br /&gt;    done&lt;br /&gt;&lt;br /&gt;    echo "Supplied Input $Number is an Integer."&lt;br /&gt;&lt;br /&gt;fi&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;$ sh validinteger3.sh -1234&lt;br /&gt;Supplied Input -1234 is an Integer.&lt;br /&gt;$ sh validinteger3.sh -12-12&lt;br /&gt;Supplied Input -12-12 is not an Integer&lt;br /&gt;$ sh validinteger3.sh &lt;br /&gt;Enter input to test: -&lt;br /&gt;Supplied Input - is not an Integer&lt;br /&gt;$ sh validinteger3.sh &lt;br /&gt;Enter input to test: 123a456&lt;br /&gt;Supplied Input 123a456 is not an Integer&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6477235244400152026?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6477235244400152026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/12/how-to-validate-integer-input-using.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6477235244400152026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6477235244400152026'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/12/how-to-validate-integer-input-using.html' title='How to Validate Integer Input using Shell script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6827459535355511711</id><published>2010-07-31T16:18:00.000-07:00</published><updated>2010-07-31T16:41:21.306-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Shell Script to Check Whether a String is Palindrome or not</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;b&gt;Palindrome:&lt;/b&gt; A palindrome is a word, phrase, number or other sequence&lt;br /&gt;of units that can be read the same way in either direction (the adjus-&lt;br /&gt;tment of punctuation and spaces between words is generally permitted).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Examples: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Phrases:    Dammit, I'm mad!&lt;br /&gt;Quotations: Able was I ere I saw Elba.&lt;br /&gt;            Madam, I'm Adam.&lt;br /&gt;Names:      Some people have names that are palindromes.Lon Nol (1913-&lt;br /&gt;            1985) was Prime Minister of Cambodia.&lt;br /&gt;            Palindromic names are very common in Finland. Examples&lt;br /&gt;            include Emma Lamme,Sanna Rannas, Anni Linna and Asko Oksa.&lt;br /&gt;Words:      civic,radar,level,rotator,rececar,reviver.&lt;br /&gt;            The command "Level, madam, level!", composed only of words&lt;br /&gt;            that are themselves palindromes, is both a character-by-&lt;br /&gt;            character and a word-by-word palindrome.&lt;br /&gt;Numbers:    5335, 123454321&lt;br /&gt;Dates:      01/02/2010 (dd/mm/yyyy format)&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 1:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: palindrome1.sh&lt;br /&gt;# USAGE: palindrome.sh     or  palindrome.sh STRING&lt;br /&gt;# PURPOSE:  Script to test if a given string is a palindrome.&lt;br /&gt;#&lt;br /&gt;# In this script I uses the well known method, compare first character&lt;br /&gt;# with last character, up to middle of the string. One mismatch in the&lt;br /&gt;# scanning leads to immediate termination of the scanning as it is&lt;br /&gt;# not a palindrome. To extract character from string, I will use cut&lt;br /&gt;# command with the -c option with the position number.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;     echo -n "Enter a String: "&lt;br /&gt;     read orgstr&lt;br /&gt;else&lt;br /&gt;     orgstr=$*&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# You can also use single statement&lt;br /&gt;#[ $# -eq 0 ] &amp;&amp; (echo -n "Enter a String:"; read String) || String=$*&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Variable Initialization                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# Remove all punctuations from input string and convert upper case to&lt;br /&gt;# lower or lower case to upper.&lt;br /&gt;&lt;br /&gt;String="$(echo $orgstr | sed 's/[^[:alnum:]]//g' | \&lt;br /&gt;tr '[:upper:]' '[:lower:]')"&lt;br /&gt;&lt;br /&gt;Flag=0&lt;br /&gt;&lt;br /&gt;# Find length of the string.&lt;br /&gt;len=${#String}&lt;br /&gt;&lt;br /&gt;#You can also calculate string length using bellow commands.&lt;br /&gt;#len=`echo $str | wc -c`&lt;br /&gt;#len=$((len-1))&lt;br /&gt;&lt;br /&gt;#get the mid value up to which the comparison would be done.&lt;br /&gt;mid=$((len/2))&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;for ((i=1;i&amp;lt;=mid;i++))&lt;br /&gt;do&lt;br /&gt;   c1=`echo $String|cut -c$i`           # extracts from beginning&lt;br /&gt;   c2=`echo $String|cut -c$len`         # extracts from last&lt;br /&gt;&lt;br /&gt;   if [ $c1 != $c2 ]&lt;br /&gt;   then&lt;br /&gt;        Flag=1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        break 2             # &lt;b&gt;break N&lt;/b&gt; breaks out of N levels of loop.&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   let len--&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;if [ $Flag -eq 0 ]&lt;br /&gt;then&lt;br /&gt;     echo "\"$orgstr\" is a Palindrome"&lt;br /&gt;else&lt;br /&gt;     echo "\"$orgstr\" is not a Palindrome"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www ]# ./palindrome1.sh Dammit, I\'m mad!&lt;br /&gt;"Dammit, I'm mad!" is a Palindrome&lt;br /&gt;[root@www ]# ./palindrome1.sh&lt;br /&gt;Enter a String: 01/02/2010&lt;br /&gt;"01/02/2010" is a Palindrome&lt;br /&gt;[root@www ]# ./palindrome1.sh Hello world&lt;br /&gt;"Hello world" is not a Palindrome&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 2: &lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: palindrome2.sh&lt;br /&gt;# USAGE: palindrome.sh     or  palindrome.sh STRING&lt;br /&gt;# PURPOSE: Script to test if a given string is a palindrome.&lt;br /&gt;#&lt;br /&gt;# In this script I uses the well known method, compare first character&lt;br /&gt;# with last character, up to middle of the string. One mismatch in the&lt;br /&gt;# scanning leads to immediate termination of the  scanning as it is&lt;br /&gt;# not a palindrome. To extract a character from the string, I will use&lt;br /&gt;# string manipulation operations.So you need to know how to manipulate&lt;br /&gt;# strings to understand this script. I will give little bit of explan-&lt;br /&gt;# tion at the end of this script.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;[ $# -eq 0 ] &amp;&amp; { echo -n "Enter a String: "; read orgstr ;} || \&lt;br /&gt;orgstr=$*&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Variable Initialization                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# Remove all punctuations from input string and convert upper case to&lt;br /&gt;# lower or lower case to upper.&lt;br /&gt;&lt;br /&gt;String="$(echo $orgstr | sed 's/[^[:alnum:]]//g' | \&lt;br /&gt;tr '[:upper:]' '[:lower:]')"&lt;br /&gt;&lt;br /&gt;# Find length of the string.&lt;br /&gt;len=${#String}&lt;br /&gt;&lt;br /&gt;#get the mid value up to which the comparison would be done&lt;br /&gt;mid=$(($len/2))&lt;br /&gt;&lt;br /&gt;i=0&lt;br /&gt;Flag=0&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;while [ $i -lt $mid ]&lt;br /&gt;  do&lt;br /&gt;      fchar=${String:$i:1}&lt;br /&gt;      let i++&lt;br /&gt;      bchar=${String: -$i:1}&lt;br /&gt;      if [ "$fchar" != $bchar ]&lt;br /&gt;      then&lt;br /&gt;      Flag=1&lt;br /&gt;      break 2              # &lt;b&gt;break N&lt;/b&gt; breaks out of N levels of loop.&lt;br /&gt;      fi&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;if [ $Flag -eq 0 ]&lt;br /&gt;then&lt;br /&gt;     echo "\"$orgstr\" is a Palindrome"&lt;br /&gt;else&lt;br /&gt;     echo "\"$orgstr\" is not a Palindrome"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Substring Extraction:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;${string:position}&lt;br /&gt;    Extracts substring from $string at $position.&lt;br /&gt;${string:position:length}&lt;br /&gt;    Extracts $length characters of substring from $string at $position&lt;br /&gt;&lt;br /&gt;Bash numbers first character of string as '0'.&lt;br /&gt;&lt;br /&gt;${string: 0: 1} will extracts one character from the 0th character of&lt;br /&gt;the string, ie it will only get the 0th character. ${string: 2: 1}&lt;br /&gt;will get the third character. Also ${string: -1: 1} will extracts the&lt;br /&gt;last one character, ${string: -3:1} will get the third last character.&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note:  ${string: -1:1} in this construct don't forget to give space&lt;br /&gt;before -1, otherwise you will get full string.&lt;/div&gt;&lt;br /&gt;For example&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@localhost www]# tempvar=madam&lt;br /&gt;[root@localhost www]# echo ${tempvar: -1:1}&lt;br /&gt;m&lt;br /&gt;[root@localhost www]# echo ${tempvar:-1:1}&lt;br /&gt;madam&lt;br /&gt;&lt;br /&gt;You can also use following command&lt;br /&gt;&lt;br /&gt;[root@localhost www]# echo ${tempvar:(-1):1}&lt;br /&gt;m&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www ]# ./palindrome2.sh Able was I ere I saw Elba&lt;br /&gt;"Able was I ere I saw Elba" is a Palindrome&lt;br /&gt;[root@www ]# ./palindrome2.sh 123454321&lt;br /&gt;"123454321" is a Palindrome&lt;br /&gt;[root@www ]# ./palindrome2.sh&lt;br /&gt;Enter a String: 12345654321&lt;br /&gt;"12345654321" is a Palindrome&lt;br /&gt;[root@www ]# ./palindrome2.sh&lt;br /&gt;Enter a String: 1234564321&lt;br /&gt;"1234564321" is not a Palindrome&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 3: &lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: palindrome3.sh&lt;br /&gt;# USAGE: palindrome.sh     or  palindrome.sh STRING&lt;br /&gt;# PURPOSE: Script to test if a given string is a palindrome.&lt;br /&gt;#&lt;br /&gt;# This simply uses the 'rev' utility which is used to reverse lines of&lt;br /&gt;# a file. Then check if the reverse of the string is same as the&lt;br /&gt;# original.rev command is part of util-linux-ng or util-linux package.&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;if `which rev &amp;&gt;/dev/null`   # Checks rev command exist or not&lt;br /&gt;then&lt;br /&gt;     [ $# -eq 0 ] &amp;&amp; { echo -n "Enter a String: "; read orgstr ;} || \&lt;br /&gt;     orgstr=$*&lt;br /&gt;&lt;br /&gt;     String="$(echo $orgstr | sed 's/[^[:alnum:]]//g' | \&lt;br /&gt;     tr '[:upper:]' '[:lower:]')"&lt;br /&gt;&lt;br /&gt;     if [ "$(echo $String | rev)" = "$String" ]&lt;br /&gt;     then&lt;br /&gt;          echo "\"$orgstr\" is a palindrome"&lt;br /&gt;     else&lt;br /&gt;          echo "\"$orgstr\" is not a palindrome"&lt;br /&gt;     fi&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;     echo "Install util-linux or util-linux-ng package"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www ]# ./palindrome3.sh 01/02/2010&lt;br /&gt;"01/02/2010" is a palindrome&lt;br /&gt;[root@www ]# ./palindrome3.sh 01/03/2010&lt;br /&gt;"01/03/2010" is not a palindrome&lt;br /&gt;[root@www ]# ./palindrome3.sh&lt;br /&gt;Enter a String: Hello World&lt;br /&gt;"Hello World" is not a palindrome&lt;br /&gt;[root@www ]# ./palindrome3.sh&lt;br /&gt;Enter a String: rotator&lt;br /&gt;"rotator" is a palindrome&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 4:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: palindrome4.sh&lt;br /&gt;# USAGE: palindrome.sh     or  palindrome.sh STRING&lt;br /&gt;# PURPOSE:  Script to test if a given string is a palindrome.&lt;br /&gt;#&lt;br /&gt;# In this method we are not using 'rev' command to reverse the string.&lt;br /&gt;# Using Substring Removal method or Substring Extraction method we&lt;br /&gt;# will reverse the string, then compare it with oldstring.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Arguments Checking                           #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;[ $# -eq 0 ] &amp;&amp; { echo -n "Enter a String: "; read orgstr ;} || \&lt;br /&gt;orgstr=$*&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Variable Initialization                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;String="$(echo $orgstr | sed 's/[^[:alnum:]]//g' | \&lt;br /&gt;tr '[:upper:]' '[:lower:]')"&lt;br /&gt;&lt;br /&gt;oldstring=$String&lt;br /&gt;newstring=&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                     Main Script Starts Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;while [ -n "$String" ]&lt;br /&gt;do&lt;br /&gt;     temp=${String#?}&lt;br /&gt;     letter=${String%"$temp"}&lt;br /&gt;     String=$temp&lt;br /&gt;     newstring=${letter}${newstring}&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;if [ "$oldstring" = "$newstring" ]&lt;br /&gt;then&lt;br /&gt;     echo "\"$orgstr\" is a palindrome"&lt;br /&gt;else&lt;br /&gt;     echo "\"$orgstr\" is not a palindrome"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# ${string#substring} is a Substing Removal operation. If you want to&lt;br /&gt;# use Substring Extraction method, use bellow code.&lt;br /&gt;&lt;br /&gt;#i=0&lt;br /&gt;#while [ $i -lt ${#String} ]&lt;br /&gt;#do&lt;br /&gt;#    letter=${String:$i:1}&lt;br /&gt;#    newstring=${letter}${newstring}&lt;br /&gt;#    let i++;&lt;br /&gt;#done&lt;br /&gt;#&lt;br /&gt;#if [ "$String" = "$newstring" ]&lt;br /&gt;#then&lt;br /&gt;#     echo "\"$orgstr\" is a palindrome"&lt;br /&gt;#else&lt;br /&gt;#     echo "\"$orgstr\" is not a palindrome"&lt;br /&gt;#fi&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Substring Removal:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;${string#substring}&lt;br /&gt;    Strips shortest match of $substring from front of $string.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www]# tempvar=madam&lt;br /&gt;[root@www]# echo ${tempvar#m}&lt;br /&gt;adam&lt;br /&gt;[root@www]# echo ${tempvar#ma}&lt;br /&gt;dam&lt;br /&gt;[root@www]# echo ${tempvar#?}&lt;br /&gt;adam&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;${string%substring}&lt;br /&gt;   Strips shortest match of $substring from back of $string.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www]# temp=${tempvar#?}&lt;br /&gt;[root@www]# echo $temp&lt;br /&gt;adam&lt;br /&gt;[root@www]# echo ${tempvar%$temp}&lt;br /&gt;m&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www ]# ./palindrome4.sh Madam, I\'m Adam&lt;br /&gt;"Madam, I'm Adam" is a palindrome&lt;br /&gt;[root@www ]# ./palindrome4.sh Madam I Adam&lt;br /&gt;"Madam I Adam" is not a palindrome&lt;br /&gt;[root@www ]# ./palindrome4.sh&lt;br /&gt;Enter a String: 123454321&lt;br /&gt;"123454321" is a palindrome&lt;br /&gt;[root@www ]# ./palindrome4.sh&lt;br /&gt;Enter a String: 1234564321&lt;br /&gt;"1234564321" is not a palindrome&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6827459535355511711?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6827459535355511711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/07/shell-script-to-check-whether-string-is.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6827459535355511711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6827459535355511711'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/07/shell-script-to-check-whether-string-is.html' title='Shell Script to Check Whether a String is Palindrome or not'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7467514994206884293</id><published>2010-07-29T09:34:00.000-07:00</published><updated>2010-07-29T10:12:34.831-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Insertion Sort Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash#!/bin/bash&lt;br /&gt;# SCRIPT: insertionsort.sh&lt;br /&gt;#&lt;br /&gt;# LOGIC: Here, sorting takes place by inserting a particular element&lt;br /&gt;# at the appropriate position, that’s why the name insertion sorting.&lt;br /&gt;# In the First iteration, second element ARRAY[1] is compared with&lt;br /&gt;# the first element ARRAY[0]. In the second iteration third element&lt;br /&gt;# is compared with first and second element. In general, in every&lt;br /&gt;# iteration an element is compared with all the elements before it.&lt;br /&gt;# While comparing if it is found that the element can be inserted at&lt;br /&gt;# a suitable position, then space is created for it by shifting the&lt;br /&gt;# other elements one position up and inserts the desired element at&lt;br /&gt;# the suitable position. This procedure is repeated for all the&lt;br /&gt;# elements in the list.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;echo ${ARRAY[*]}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sortnumbers()&lt;br /&gt;{&lt;br /&gt;for((i=1;i&amp;lt;count;i++))&lt;br /&gt;do&lt;br /&gt;   Temp=${ARRAY[i]}&lt;br /&gt;   j=$((i-1))&lt;br /&gt;   while [ $Temp -lt ${ARRAY[j]} ]&lt;br /&gt;   do&lt;br /&gt;      ARRAY[j+1]=${ARRAY[j]}&lt;br /&gt;      let j--&lt;br /&gt;      if [ $j == -1 ]&lt;br /&gt;      then&lt;br /&gt;         break&lt;br /&gt;      fi&lt;br /&gt;   done&lt;br /&gt;   ARRAY[j+1]=$Temp&lt;br /&gt;done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Initialization                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "Enter numbers to be sorted"&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "---------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;echo "Numbers Before Sort:"&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;sortnumbers&lt;br /&gt;&lt;br /&gt;echo "Numbers After Sort: "&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;echo "---------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www blog]# sh insertionsort.sh&lt;br /&gt;Enter Numbers to be Sorted :&lt;br /&gt;12 76 34 -34 67 9 -56 5 99 -3 17&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;Numbers Before Sort:&lt;br /&gt;12 76 34 -34 67 9 -56 5 99 -3 17&lt;br /&gt;Numbers After Sort:&lt;br /&gt;-56 -34 -3 5 9 12 17 34 67 76 99&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;NOTE:&lt;/b&gt; If we complement the &lt;b&gt;while&lt;/b&gt; condition in this program, it will&lt;br /&gt;give out the sorted array in descending order. &lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7467514994206884293?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7467514994206884293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/07/insertion-sort-shell-script.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7467514994206884293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7467514994206884293'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/07/insertion-sort-shell-script.html' title='Insertion Sort Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-2767072689171001210</id><published>2010-07-29T09:29:00.000-07:00</published><updated>2010-07-29T09:34:12.864-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Selection Sort Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash#!/bin/bash&lt;br /&gt;# SCRIPT: selectionsort.sh&lt;br /&gt;#&lt;br /&gt;# LOGIC : Here, to sort the data in ascending order, the first element&lt;br /&gt;# ARRAY[0] is compared with all the other elements till the end of the&lt;br /&gt;# array. If it is greater than any other the elements then they are&lt;br /&gt;# interchanged. So after the first iteration of the outer for loop&lt;br /&gt;# smallest element will be placed at the first position. The same pro-&lt;br /&gt;# cedure is repeated for the other elements too.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;echo ${ARRAY[*]}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;swap()&lt;br /&gt;{&lt;br /&gt;temp=${ARRAY[$1]}&lt;br /&gt;&lt;br /&gt;ARRAY[$1]=${ARRAY[$2]}&lt;br /&gt;&lt;br /&gt;ARRAY[$2]=$temp&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sortnumbers()&lt;br /&gt;{&lt;br /&gt;for ((i=0;i&amp;lt;count;i++))&lt;br /&gt;do&lt;br /&gt;min=$i&lt;br /&gt;  for ((j=i+1;j&amp;lt;count;j++))&lt;br /&gt;  do&lt;br /&gt;     if [ ${ARRAY[j]} -lt ${ARRAY[min]} ]&lt;br /&gt;     then&lt;br /&gt;     min=$j&lt;br /&gt;     fi&lt;br /&gt;  done&lt;br /&gt;  swap $i $min&lt;br /&gt;done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Initialization                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "Enter Numbers to be Sorted : "&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "---------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;echo "Numbers Before Sort:"&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;sortnumbers&lt;br /&gt;&lt;br /&gt;echo "Numbers After Sort: "&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;echo "---------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www blog]# sh selectionsort.sh&lt;br /&gt;Enter Numbers to be Sorted :&lt;br /&gt;34 76 -8 12 23 5 9 -2 88 41 62&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;Numbers Before Sort:&lt;br /&gt;34 76 -8 12 23 5 9 -2 88 41 62&lt;br /&gt;Numbers After Sort:&lt;br /&gt;-8 -2 5 9 12 23 34 41 62 76 88&lt;br /&gt;---------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;NOTE:&lt;/b&gt; If we complement the &lt;b&gt;if&lt;/b&gt; condition in this program, it will give&lt;br /&gt;out the sorted array in descending order.&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-2767072689171001210?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2767072689171001210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/07/selection-sort-shell-script.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2767072689171001210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2767072689171001210'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/07/selection-sort-shell-script.html' title='Selection Sort Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-4534079556350470670</id><published>2010-07-29T09:23:00.000-07:00</published><updated>2010-07-29T09:29:09.669-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Bubble Sort Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: bubblesort.sh&lt;br /&gt;# LOGIC:&lt;br /&gt;# Bubble sort is a simple sorting, it works by repeatedly stepping&lt;br /&gt;# through the list to be sorted, comparing two items at a time and&lt;br /&gt;# swapping them if they are in the wrong order. If you are sorting&lt;br /&gt;# the data in Ascending order, at the end of the first pass, the&lt;br /&gt;# "heaviest" element has move to bottom. In the second pass, the&lt;br /&gt;# comparisons are made till the last but one position and now second&lt;br /&gt;# largest element is placed at the last but one position. And so&lt;br /&gt;# forth.&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                      Define Functions Here                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;echo ${ARRAY[*]}&lt;br /&gt;&lt;br /&gt;#You can also use bellow code&lt;br /&gt;#for ((i=0;i&amp;lt;count;i++))&lt;br /&gt;#do&lt;br /&gt;#echo -n " ${ARRAY[i]} "&lt;br /&gt;#done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;exchange()&lt;br /&gt;{&lt;br /&gt;temp=${ARRAY[$1]}&lt;br /&gt;&lt;br /&gt;ARRAY[$1]=${ARRAY[$2]}&lt;br /&gt;&lt;br /&gt;ARRAY[$2]=$temp&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sortnumbers()&lt;br /&gt;{&lt;br /&gt;for (( last=count-1;last&amp;gt;0;last--))&lt;br /&gt;do&lt;br /&gt;    for((i=0;i&amp;lt;last;i++))&lt;br /&gt;    do&lt;br /&gt;      j=$((i+1))&lt;br /&gt;      if [ ${ARRAY[i]} -gt ${ARRAY[j]} ]&lt;br /&gt;      then&lt;br /&gt;&lt;br /&gt;       exchange $i $j&lt;br /&gt;&lt;br /&gt;      fi&lt;br /&gt;    done&lt;br /&gt;done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Variable Initialization                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "Enter Numbers to be Sorted"&lt;br /&gt;&lt;br /&gt;read -a ARRAY&lt;br /&gt;&lt;br /&gt;count=${#ARRAY[@]}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo "--------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;echo "Numbers Before Sort:"&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;echo&lt;br /&gt;&lt;br /&gt;sortnumbers&lt;br /&gt;&lt;br /&gt;echo "Numbers After Sort: "&lt;br /&gt;&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;echo "--------------------------------------------------------------"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www blog]# sh bubblesort.sh&lt;br /&gt;Enter Numbers to be Sorted :&lt;br /&gt;78 34 12 98 21 8 36 98 12 88 7 5 61 -12 62 -1 77 -46&lt;br /&gt;------------------------------------------------------&lt;br /&gt;Numbers Before Sort:&lt;br /&gt;78 34 12 98 21 8 36 98 12 88 7 5 61 -12 62 -1 77 -46&lt;br /&gt;&lt;br /&gt;Numbers After Sort:&lt;br /&gt;-46 -12 -1 5 7 8 12 12 21 34 36 61 62 77 78 88 98 98&lt;br /&gt;------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;NOTE:&lt;/b&gt; If we complement the &lt;b&gt;if&lt;/b&gt; condition in this program, it will give&lt;br /&gt;out the sorted array in descending order.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method2: Without Using Arrays&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: bubblesort2.sh&lt;br /&gt;# Without using arrays&lt;br /&gt;#&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Define Functions Here                       #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;printnumbers()&lt;br /&gt;{&lt;br /&gt;k=1&lt;br /&gt;while [ $k -le $max ]&lt;br /&gt;do&lt;br /&gt;eval echo -n "\$x$k"&lt;br /&gt;echo -n " "&lt;br /&gt;let k++&lt;br /&gt;done&lt;br /&gt;echo&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                        Variable Initialization                    #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter Total Numbers to be Sorted : "&lt;br /&gt;read max&lt;br /&gt;count=1&lt;br /&gt;while [ $count -le $max ]&lt;br /&gt;do&lt;br /&gt;echo -n "Enter number $count: "&lt;br /&gt;read x$count&lt;br /&gt;let count++&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Main Script Starts Here                     #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -e "\nElements Before Sort"&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;for (( last=count-1;last&amp;gt;0;last--))&lt;br /&gt;do&lt;br /&gt;  for ((i=1;i&amp;lt;last;i++))&lt;br /&gt;  do&lt;br /&gt;     j=$((i+1))&lt;br /&gt;     eval sval=\$x$i&lt;br /&gt;     eval nval=\$x$j&lt;br /&gt;&lt;br /&gt;#The &lt;b&gt;eval&lt;/b&gt; command evaluates the command line to complete any shell&lt;br /&gt;#substitutions necessary and then executes the command. So $i and $j&lt;br /&gt;#substituted first then $x1 and $x2 evaluated.&lt;br /&gt;&lt;br /&gt;     if [ $sval -gt $nval ]&lt;br /&gt;     then&lt;br /&gt;     eval x$i=$nval&lt;br /&gt;     eval x$j=$sval&lt;br /&gt;     fi&lt;br /&gt;   done&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo  "Elements After Sort: "&lt;br /&gt;printnumbers&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;OUTPUT:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www shell]# sh bubblesort2.sh&lt;br /&gt;Enter Total Numbers to be Sorted : 6&lt;br /&gt;Enter number 1: 12&lt;br /&gt;Enter number 2: -4&lt;br /&gt;Enter number 3: 6&lt;br /&gt;Enter number 4: -11&lt;br /&gt;Enter number 5: 43&lt;br /&gt;Enter number 6: 9&lt;br /&gt;&lt;br /&gt;Elements Before Sort&lt;br /&gt;12 -4 6 -11 43 9&lt;br /&gt;Elements After Sort:&lt;br /&gt;-11 -4 6 9 12 43&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-4534079556350470670?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/4534079556350470670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/07/bubble-sort-shell-script.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4534079556350470670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4534079556350470670'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/07/bubble-sort-shell-script.html' title='Bubble Sort Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-3391906406692888809</id><published>2010-05-14T23:45:00.000-07:00</published><updated>2010-05-15T00:08:29.540-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>How to Read a File Line by Line in a Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt; There are many ways to handle any task on a Unix platform,  but some&lt;br /&gt;techniques that are used to process a file waste a lot of CPU time.&lt;br /&gt;Most of the wasted time is spent in unnecessary variable assignment and&lt;br /&gt;continuously opening and closing the same file over and over. Using a&lt;br /&gt;pipe also has a negative impact on the timing.&lt;br /&gt;&lt;br /&gt; In this article I will explain various techniques for parsing a file&lt;br /&gt;line by line. Some techniques are very fast and some make you wait for&lt;br /&gt;half a day. The techniques used in this article are measurable, and I&lt;br /&gt;tested each technique with &lt;b&gt;time&lt;/b&gt; command so that you can see which tec-&lt;br /&gt;hniques suits your needs.&lt;br /&gt;&lt;br /&gt; I don't explain in depth every thing, but if you know basic shell&lt;br /&gt;scripting, I hope you can understand easily.&lt;br /&gt;&lt;br /&gt; I extracted last five lines from my /etc/passwd file, and stored in a&lt;br /&gt;file "file_passwd".&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# tail -5 /etc/passwd &gt; file_passwd&lt;br /&gt;[root@www blog]# cat file_passwd&lt;br /&gt;venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;user2:x:504:501::/home/project/:/bin/bash&lt;/div&gt;&lt;br /&gt; I use this file whenever a sample file required.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 1:  PIPED while-read loop&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT:  method1.sh&lt;br /&gt;# PURPOSE: Process a file line by line with PIPED while-read loop.&lt;br /&gt;&lt;br /&gt;FILENAME=$1&lt;br /&gt;count=0&lt;br /&gt;cat $FILENAME | while read LINE&lt;br /&gt;do&lt;br /&gt;       let count++&lt;br /&gt;       echo "$count $LINE"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo -e "\nTotal $count Lines read"&lt;/div&gt;&lt;br /&gt; With catting a file and piping the file output to a while read loop a&lt;br /&gt;single line of text is read into a variable named LINE on each loop&lt;br /&gt;iteration. This continuous loop will run until all of the lines in the&lt;br /&gt;file have been processed one at a time.&lt;br /&gt;&lt;br /&gt; Bash can sometimes start a subshell in a PIPED "while-read" loop. So&lt;br /&gt;the variable set within the loop will be lost (unset) outside of the&lt;br /&gt;loop. Therefore, $count would return 0, the initialized value outside&lt;br /&gt;the loop.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# sh method1.sh file_passwd&lt;br /&gt;1 venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;2 padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;3 king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;4 user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;5 user2:x:504:501::/home/project/:/bin/bash&lt;br /&gt;&lt;br /&gt;Total 0 Lines read&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;Method 2:  Redirected "while-read" loop&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;#SCRIPT:  method2.sh&lt;br /&gt;#PURPOSE: Process a file line by line with redirected while-read loop.&lt;br /&gt;&lt;br /&gt;FILENAME=$1&lt;br /&gt;count=0&lt;br /&gt;&lt;br /&gt;while read LINE&lt;br /&gt;do&lt;br /&gt;      let count++&lt;br /&gt;      echo "$count $LINE"&lt;br /&gt;&lt;br /&gt;done &lt; $FILENAME&lt;br /&gt;&lt;br /&gt;echo -e "\nTotal $count Lines read"&lt;/div&gt;&lt;br /&gt; We still use the while read LINE syntax, but this time we feed the&lt;br /&gt;loop from the bottom (using file redirection) instead of using a pipe.&lt;br /&gt;You will find that this is one of the fastest ways to process each&lt;br /&gt;line of a file. The first time you see this it looks a little unusual,&lt;br /&gt;but it works very well.&lt;br /&gt;&lt;br /&gt; Unlike method 1, with method 2 you will get total number of lines out&lt;br /&gt;side of the loop.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# sh method2.sh file_passwd&lt;br /&gt;1 venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;2 padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;3 king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;4 user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;5 user2:x:504:501::/home/project/:/bin/bash&lt;br /&gt;&lt;br /&gt;Total 5 Lines read&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Note:&lt;/b&gt; In some older shell scripting languages, the redirected loop&lt;br /&gt;would also return as a subshell.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 3:while read LINE Using File Descriptors&lt;/h4&gt;&lt;br /&gt; A file descriptor is simply a number that the operating system assigns&lt;br /&gt;to an open file to keep track of it. Consider it a simplified version&lt;br /&gt;of a file pointer. It is analogous to a file handle in C.&lt;br /&gt;&lt;br /&gt; There are always three default "files" open, &lt;b&gt;stdin&lt;/b&gt; (the keyboard),&lt;br /&gt;&lt;b&gt;stdout&lt;/b&gt; (the screen), and &lt;b&gt;stderr&lt;/b&gt; (error messages output to the screen).&lt;br /&gt;These, and any other open files, can be redirected. Redirection simply&lt;br /&gt;means capturing output from a file, command, program, script, or even&lt;br /&gt;code block within a script  and sending it as input to another file,&lt;br /&gt;command, program, or script.&lt;br /&gt;&lt;br /&gt; Each open file gets assigned a file descriptor. The file descriptors&lt;br /&gt;for stdin,stdout, and stderr are 0,1, and 2, respectively. For opening&lt;br /&gt;additional files, there remain descriptors 3 to 9 (may be vary depend-&lt;br /&gt;ing on OS). It is sometimes useful to assign one of these additional&lt;br /&gt;file descriptors to stdin, stdout, or stderr as a temporary duplicate&lt;br /&gt;link. This simplifies restoration to normal after complex redirection&lt;br /&gt;and reshuffling .&lt;br /&gt;&lt;br /&gt; There are two steps in the method we are going to use. The first step&lt;br /&gt;is to close file descriptor 0 by redirecting everything to our new file&lt;br /&gt;descriptor 3. We use the following syntax for this step:&lt;br /&gt;&lt;br /&gt;  &lt;b&gt;exec 3&lt;&amp;0&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Now all of the keyboard and mouse input is going to our new file des-&lt;br /&gt;criptor 3. The second step is to send our input file, specified by the&lt;br /&gt;variable $FILENAME, into file descriptor 0 (zero), which is standard&lt;br /&gt;input. This second step is done using the following syntax:&lt;br /&gt;&lt;br /&gt;   &lt;b&gt; exec 0&lt;$FILENAME&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; At this point any command requiring input will receive the input from&lt;br /&gt;the $FILENAME file. Now is a good time for an example.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;#SCRIPT:  method3.sh&lt;br /&gt;#PURPOSE: Process a file line by line with while read LINE Using&lt;br /&gt;#File Descriptors&lt;br /&gt;&lt;br /&gt;FILENAME=$1&lt;br /&gt;count0=&lt;br /&gt;exec 3&lt;&amp;0&lt;br /&gt;exec 0&lt; $FILENAME&lt;br /&gt;&lt;br /&gt;while read LINE&lt;br /&gt;do&lt;br /&gt;        let count++&lt;br /&gt;        echo "$count $LINE"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;exec 0&lt;&amp;3&lt;br /&gt;echo -e "\nTotal $count Lines read"&lt;/div&gt;&lt;br /&gt; while loop reads one line of text at a time.But the beginning of this&lt;br /&gt;script does a little file descriptor redirection. The first exec comm-&lt;br /&gt;and redirects stdin to file descriptor 3. The second exec command red-&lt;br /&gt;irects the $FILENAME file into stdin, which is file descriptor 0. Now&lt;br /&gt;the while loop can just execute without our having to worry about how&lt;br /&gt;we assign a line of text to the LINE variable. When the while loop&lt;br /&gt;exits we redirect the previously reassigned stdin, which was sent to&lt;br /&gt;file descriptor 3, back to its original file descriptor 0.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; exec 0&lt;&amp;3&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In other words we set it back to the system’s default value.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@www tempdir]# sh method3.sh file_passwd &lt;br /&gt;1 venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;2 padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;3 king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;4 user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;5 user2:x:504:501::/home/project/:/bin/bash&lt;br /&gt;&lt;br /&gt;Total 5 Lines read&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 4: Process file line by line using awk&lt;/h4&gt;&lt;br /&gt; &lt;b&gt;awk&lt;/b&gt; is pattern scanning and text processing language. It is useful&lt;br /&gt;for manipulation of data files, text retrieval and processing. Good&lt;br /&gt;for manipulating and/or extracting fields (columns) in structured&lt;br /&gt;text files.&lt;br /&gt;&lt;br /&gt; Its name comes from the surnames of its authors: Alfred Aho, Peter&lt;br /&gt;Weinberger, and Brian Kernighan.&lt;br /&gt;&lt;br /&gt; I am not going to explain everything here.To know more about awk just&lt;br /&gt;Google it.&lt;br /&gt;&lt;br /&gt;At the command line, enter the following command:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ awk '{ print }' /etc/passwd &lt;/b&gt;&lt;br /&gt;&lt;br /&gt; You should see the contents of your &lt;b&gt;/etc/passwd&lt;/b&gt; file appear before&lt;br /&gt;your eyes.Now, for an explanation of what awk did. When we called awk,&lt;br /&gt;we specified /etc/passwd as our input file. When we executed awk, it&lt;br /&gt;evaluated the print command for each line in /etc/passwd, in order.All&lt;br /&gt;output is sent to stdout, and we get a result identical to catting&lt;br /&gt;/etc/passwd. Now, for an explanation of the { print } code block. In&lt;br /&gt;awk, curly braces are used to group blocks of code together, similar&lt;br /&gt;to C. Inside our block of code,we have a single print command. In awk,&lt;br /&gt;when a print command appears by itself, the full contents of the curr-&lt;br /&gt;ent line are printed.&lt;br /&gt;&lt;br /&gt;Here is another awk example that does exactly the same thing:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ awk '{ print $0 }' /etc/passwd &lt;/b&gt;&lt;br /&gt;&lt;br /&gt; In awk, the $0 variable represents the entire current line, so print&lt;br /&gt;and print $0 do exactly the same thing. Now is a good time for an&lt;br /&gt;example.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;#SCRIPT:  method4.sh&lt;br /&gt;#PURPOSE: Process a file line by line with awk&lt;br /&gt;&lt;br /&gt;FILENAME=$1&lt;br /&gt;&lt;br /&gt;awk '{kount++;print  kount, $0}&lt;br /&gt;     END{print "\nTotal " kount " lines read"}' $FILENAME&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# sh method4.sh file_passwd&lt;br /&gt;1 venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;2 padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;3 king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;4 user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;5 user2:x:504:501::/home/project/:/bin/bash&lt;br /&gt;&lt;br /&gt;Total 5 lines read&lt;/div&gt;&lt;br /&gt;Awk is really good at handling text that has been broken into multiple&lt;br /&gt;logical fields, and allows you to effortlessly reference each individ-&lt;br /&gt;ual field from inside your awk script. The following script will print&lt;br /&gt;out a list of all user accounts on your system:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;awk -F":" '{ print $1 "\t " $3  }' /etc/passwd&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Above, when we called awk, we use the -F option to specify ":" as the&lt;br /&gt;field separator. By default white space (blank line) act as filed sep-&lt;br /&gt;arator. You can set new filed separator with -F option. When awk proc-&lt;br /&gt;esses the print $1 "\t " $3 command, it will print out the first and&lt;br /&gt;third fields that appears on each line in the input file. "\t" is used&lt;br /&gt;to separate field with tab.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 5: Little tricky with head and tail &lt;br /&gt;commands&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;#SCRIPT:  method5.sh&lt;br /&gt;#PURPOSE: Process a file line by line with head and tail commands&lt;br /&gt;&lt;br /&gt;FILENAME=$1&lt;br /&gt;Lines=`wc -l &lt; $FILENAME`&lt;br /&gt;&lt;br /&gt;count=0&lt;br /&gt;&lt;br /&gt;while [ $count -lt $Lines ]&lt;br /&gt;do&lt;br /&gt;let count++&lt;br /&gt;LINE=`head -n $count $FILENAME | tail -1`&lt;br /&gt;echo "$count $LINE"&lt;br /&gt;done&lt;br /&gt;echo -e "\nTotal $count lines read"&lt;/div&gt;&lt;br /&gt;On each iteration head command extracts top $count lines, then tail&lt;br /&gt;command extracts bottom line from that lines. A very stupid method,&lt;br /&gt;but some people still using it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# sh method5.sh file_passwd&lt;br /&gt;1 venu:x:500:500:venu madhav:/home/venu:/bin/bash&lt;br /&gt;2 padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;3 king:x:502:503:king:/home/project:/bin/bash&lt;br /&gt;4 user1:x:503:501::/home/project/:/bin/bash&lt;br /&gt;5 user2:x:504:501::/home/project/:/bin/bash&lt;br /&gt;&lt;br /&gt;Total 5 lines read&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Time Comparison for the Five Methods&lt;/h4&gt;&lt;br /&gt; Now take a long breath, we are going test each technique. Before you&lt;br /&gt;get into test each method of parsing a file line by line create a large&lt;br /&gt;file that has the exact number of lines that you want to process.&lt;br /&gt;Use &lt;a href="http://bashscript.blogspot.com/2009/11/shell-script-to-create-file-with.html"&gt;bigfile.sh&lt;/a&gt; script to create a large file.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ sh bigfile.sh 900000 &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;bigfile.sh with 900000 lines as an argument,it has taken more than two&lt;br /&gt;hours to generate bigfile.4227. I don't know exactly how much time it&lt;br /&gt;has taken. This file is extremely large to parse a file line by line,&lt;br /&gt;but I needed a large file to get the timing data greater than zero.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# du -h bigfile.4227&lt;br /&gt;70M     bigfile.4227&lt;br /&gt;[root@www blog]# wc -l bigfile.4227&lt;br /&gt;900000 bigfile.4227&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;[root@www blog]# time ./method1.sh bigfile.4227 &gt;/dev/null&lt;br /&gt;&lt;br /&gt;real    6m2.911s&lt;br /&gt;user    2m58.207s&lt;br /&gt;sys     2m58.811s&lt;br /&gt;[root@www blog]# time ./method2.sh bigfile.4227 &gt; /dev/null&lt;br /&gt;&lt;br /&gt;real    2m48.394s&lt;br /&gt;user    2m39.714s&lt;br /&gt;sys     0m8.089s&lt;br /&gt;[root@www blog]# time ./method3.sh bigfile.4227 &gt; /dev/null&lt;br /&gt;&lt;br /&gt;real    2m48.218s&lt;br /&gt;user    2m39.322s&lt;br /&gt;sys     0m8.161s&lt;br /&gt;[root@www blog]# time ./method4.sh bigfile.4227 &gt; /dev/null&lt;br /&gt;&lt;br /&gt;real    0m2.054s&lt;br /&gt;user    0m1.924s&lt;br /&gt;sys     0m0.120s&lt;br /&gt;[root@www blog]# time ./method5.sh bigfile.4227 &gt; /dev/null&lt;br /&gt;I waited more than half day, still i didn't get result, then I created&lt;br /&gt;a 10000-line file to test this method.&lt;br /&gt;[root@www tempdir]# time ./method5.sh file.10000 &gt; /dev/null&lt;br /&gt;&lt;br /&gt;real    2m25.739s&lt;br /&gt;user    0m21.857s&lt;br /&gt;sys     1m12.705s&lt;/div&gt;&lt;br /&gt;Method 4 came in first place,it has taken very less time 2.05 seconds,&lt;br /&gt;but we can't compare Method 4 with other methods, because awk is not&lt;br /&gt;just a command, but a programming language too.&lt;br /&gt;&lt;br /&gt; Method 2 and method 3 are tied for second place, they  produce mostly&lt;br /&gt;the same real execution time at 2 minutes and 48 seconds . Method 1&lt;br /&gt;came in third at 6 minutes and 2.9 seconds.&lt;br /&gt;&lt;br /&gt; Method 5 has taken more than half a day. 2 minutes 25 seconds to pro-&lt;br /&gt;cess just a 10000 line file, how stupid it is.&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;Note: If file contain escape characters, use &lt;b&gt;read -r&lt;/b&gt; instead of &lt;b&gt;read&lt;/b&gt;,&lt;br /&gt;then Backslash does not act as an escape character. The back-slash is&lt;br /&gt;considered to be part of the line. In  particular, a backslash-newline&lt;br /&gt;pair may not be used as a line continuation.&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-3391906406692888809?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/3391906406692888809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/05/how-to-read-file-line-by-line-in-shell.html#comment-form' title='18 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3391906406692888809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/3391906406692888809'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/05/how-to-read-file-line-by-line-in-shell.html' title='How to Read a File Line by Line in a Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>18</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-8811811508200624535</id><published>2010-04-20T06:05:00.000-07:00</published><updated>2010-04-20T06:29:22.861-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Shell Script to Display  Time and Date in the Top Right Corner of the Screen.</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;h4&gt;Using tput command:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: digclock.sh&lt;br /&gt;# USAGE:  ./digiclock &amp;&lt;br /&gt;# PURPOSE: Displays time and date in the top right corner of the&lt;br /&gt;#          screen using tput command.&lt;br /&gt;# To stop this digclock use command "kill pid"&lt;br /&gt;################################################################&lt;br /&gt;&lt;br /&gt;####################### VARIABLE DECLARATION ###################&lt;br /&gt;&lt;br /&gt; # To place the clock on the appropriate column, subtract the&lt;br /&gt; # length of $Time and $Date, which is 22, from the total number&lt;br /&gt; # of columns&lt;br /&gt;&lt;br /&gt;  Columns=$(tput cols)&lt;br /&gt;  Startpoint=$(($Columns-22))&lt;br /&gt;&lt;br /&gt; # If you're in an X Window System terminal,you can resize the&lt;br /&gt; # window, and the clock will adjust its position because it is&lt;br /&gt; # displayed at the last column minus 22 characters.&lt;br /&gt;&lt;br /&gt;  Color1=`tput setab 2`        # Green background color for time&lt;br /&gt;  Color2=`tput setab 6`        # Cyan  background color for date&lt;br /&gt;  Normal=`tput sgr0`           # back to normal screen colors&lt;br /&gt;&lt;br /&gt;####################### MAIN PROGRAM ###########################&lt;br /&gt;&lt;br /&gt;# The script is executed inside a while without conditions&lt;br /&gt;&lt;br /&gt;while :&lt;br /&gt;do&lt;br /&gt;  Time=`date +%r`&lt;br /&gt;  Date=`date +"%d-%m-%Y"`&lt;br /&gt;  tput sc                   #Save the cursor position&amp;attributes&lt;br /&gt;  tput cup 0 $Startpoint&lt;br /&gt;&lt;br /&gt; # You can also use bellow one liner&lt;br /&gt; # tput cup 0 $((`tput cols`-22))&lt;br /&gt; # But it is not efficient to calculate cursor position for each&lt;br /&gt; # iteration. That's why I placed variable assignment before&lt;br /&gt; # beginning of the loop.&lt;br /&gt;&lt;br /&gt; # print time and date in the top right corner of the screen.&lt;br /&gt;&lt;br /&gt;  echo -n $Color1$Time $Color2$Date$Normal&lt;br /&gt;&lt;br /&gt; # restore the cursor to whatever was its previous position&lt;br /&gt;&lt;br /&gt;  tput rc&lt;br /&gt;&lt;br /&gt; # Delay for 1 second&lt;br /&gt;&lt;br /&gt;  sleep 1&lt;br /&gt;&lt;br /&gt;done&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Using ANSI escape sequences:&lt;/h4&gt;&lt;br /&gt; The ANSI escape sequences don't work in all terminal emulators, but&lt;br /&gt;they do fine in xterm. Here's the script:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: digclock.sh&lt;br /&gt;# USAGE:  ./digiclock &amp;&lt;br /&gt;# PURPOSE: Displays time and date in the top right corner of the&lt;br /&gt;#          screen using ANSI escape sequences.&lt;br /&gt;# To stop this digclock use command kill pid.&lt;br /&gt;################################################################&lt;br /&gt;&lt;br /&gt;#################### VARIABLE DECLARATION ######################&lt;br /&gt;&lt;br /&gt; # To place the clock on the appropriate column, subtract the&lt;br /&gt; # length of $Time and $Date, which is 22, from the total number&lt;br /&gt; # of columns&lt;br /&gt;&lt;br /&gt;  Columns=$(tput cols)&lt;br /&gt;  Startpoint=$(($Columns-22))&lt;br /&gt;&lt;br /&gt; # If you're in an X Window System terminal,you can resize the&lt;br /&gt; # window, and the clock will adjust its position because it is&lt;br /&gt; # displayed at the last column minus 22 characters.&lt;br /&gt;&lt;br /&gt;########################### MAIN PROGRAM #######################&lt;br /&gt;&lt;br /&gt;# The script is executed inside a while without conditions.&lt;br /&gt;&lt;br /&gt;while :&lt;br /&gt;do&lt;br /&gt;  Time=`date +%r`&lt;br /&gt;  Date=`date +"%d-%m-%Y"`&lt;br /&gt;  echo -en "\033[s"    #save current screen position &amp; attributes&lt;br /&gt;&lt;br /&gt;  tput cup 0 $Startpoint&lt;br /&gt;&lt;br /&gt; # You can also use bellow one liner.&lt;br /&gt; # tput cup 0 $((`tput cols`-22))&lt;br /&gt; # But it is not efficient to calculate cursor position for each&lt;br /&gt; # iteration. That's why I placed variable assignment before&lt;br /&gt; # beginning of the loop&lt;br /&gt;&lt;br /&gt; # print time and date in the top right corner of the screen&lt;br /&gt;&lt;br /&gt;  echo -en "\033[42m$Time \033[46m$Date\033[0m"&lt;br /&gt;&lt;br /&gt; #restore current screen position &amp; attributes&lt;br /&gt;&lt;br /&gt;  echo -e -n "\033[u"&lt;br /&gt;&lt;br /&gt; #Delay for 1 second&lt;br /&gt;&lt;br /&gt;  sleep 1&lt;br /&gt;done&lt;/div&gt;&lt;br /&gt; Save the script as &lt;b&gt;digclock.sh&lt;/b&gt;,change permissions to 755 using &lt;b&gt;chmod&lt;/b&gt;,&lt;br /&gt;and run it with &lt;b&gt;./digclock.sh &amp;&lt;/b&gt; or &lt;b&gt;. digclock.sh &amp;&lt;/b&gt; or &lt;b&gt;sh digclock &amp;&lt;/b&gt;.&lt;br /&gt;The time and date should now appear at the top right of your screen.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Output:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/S82qjtCPBNI/AAAAAAAAAsU/IoJ9wwWkbqg/s1600/clock.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 56px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/S82qjtCPBNI/AAAAAAAAAsU/IoJ9wwWkbqg/s400/clock.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5462209453431391442" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt; When you run &lt;b&gt;digclock.sh&lt;/b&gt;, the terminal will return the job number and&lt;br /&gt;process identifier (PID) of the &lt;b&gt;digclock.sh&lt;/b&gt; process. From above output&lt;br /&gt;you can find job number is "1" and PID is "15800".&lt;br /&gt;&lt;br /&gt;&lt;b&gt;You can end the execution of the script by two ways:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;1. Using the &lt;b&gt;kill&lt;/b&gt; command and specifying the job number or process ID.&lt;br /&gt;   If you don't remember job number or PID, you can get job number by&lt;br /&gt;   running &lt;b&gt;jobs&lt;/b&gt; command and PID by &lt;b&gt;ps&lt;/b&gt; command.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ jobs&lt;br /&gt;[1]+  Running                 ./digclock.sh &amp;&lt;br /&gt;$ shell]# ps | grep digclock&lt;br /&gt;15800 pts/1    00:00:00 digclock.sh&lt;/div&gt;&lt;br /&gt; To kill this job/process, either &lt;b&gt;kill %1&lt;/b&gt; or &lt;b&gt;kill 15800&lt;/b&gt; works.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ kill %1&lt;br /&gt;$&lt;br /&gt;[1]+  Terminated              ./digclock.sh&lt;br /&gt;              or&lt;br /&gt;$ kill 15800&lt;br /&gt;$&lt;br /&gt;[1]+  Terminated              ./digclock.sh&lt;/div&gt;&lt;br /&gt;2. Using the &lt;b&gt;fg&lt;/b&gt; command. The &lt;b&gt;fg&lt;/b&gt; command switches a job running in the&lt;br /&gt;   background into the foreground, Then press &lt;b&gt;Ctrl+c&lt;/b&gt; to terminate the&lt;br /&gt;   job. If no job number is specified, then &lt;b&gt;fg&lt;/b&gt; command acts upon the&lt;br /&gt;   currently running job.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ fg 1&lt;br /&gt;./digclock.sh&lt;br /&gt;&lt;br /&gt;Ctrl+c&lt;br /&gt;$&lt;/div&gt;&lt;br /&gt; With this script, you can display not only a clock, but other useful&lt;br /&gt;information as well. For example,monitoring free space with &lt;b&gt;df&lt;/b&gt; command&lt;br /&gt;or CPU's load average with &lt;b&gt;uptime&lt;/b&gt; command. Samba, Apache, and many&lt;br /&gt;other servers have status commands where you can extract pieces of&lt;br /&gt;information to show this way.&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-8811811508200624535?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/8811811508200624535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/04/shell-script-to-display-time-and-date.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8811811508200624535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8811811508200624535'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/04/shell-script-to-display-time-and-date.html' title='Shell Script to Display  Time and Date in the Top Right Corner of the Screen.'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_ioCu0w5ZnYE/S82qjtCPBNI/AAAAAAAAAsU/IoJ9wwWkbqg/s72-c/clock.PNG' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-2557437381638152348</id><published>2010-04-18T08:19:00.000-07:00</published><updated>2011-01-15T06:20:06.033-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>How to Handle Cursor Movement in a Shell Script</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;&lt;h4&gt;Using ANSI escape sequences :&lt;/h4&gt;&lt;br /&gt; ANSI escape sequences or &lt;b&gt;tput&lt;/b&gt; allow you to move the cursor around the&lt;br /&gt;screen at will. This is more useful for full screen user interfaces&lt;br /&gt;generated by shell scripts, but can also be used in prompts.&lt;br /&gt;&lt;br /&gt;The movement escape sequences are as follows:&lt;br /&gt;&lt;br /&gt;        - Position the Cursor:&lt;br /&gt;          \033[&amp;lt;L&amp;gt;;&amp;lt;C&amp;gt;H&lt;br /&gt;             Or&lt;br /&gt;          \033[&amp;lt;L&amp;gt;;&amp;lt;C&amp;gt;f&lt;br /&gt;          puts the cursor at line L and column C.&lt;br /&gt;        - Move the cursor up N lines:&lt;br /&gt;          \033[&amp;lt;N&amp;gt;A&lt;br /&gt;        - Move the cursor down N lines:&lt;br /&gt;          \033[&amp;lt;N&amp;gt;B&lt;br /&gt;        - Move the cursor forward N columns:&lt;br /&gt;          \033[&amp;lt;N&amp;gt;C&lt;br /&gt;        - Move the cursor backward N columns:&lt;br /&gt;          \033[&amp;lt;N&amp;gt;D&lt;br /&gt;        - Clear the screen, move to (0,0):&lt;br /&gt;          \033[2J&lt;br /&gt;        - Erase to end of line:&lt;br /&gt;          \033[K&lt;br /&gt;        - Save cursor position:&lt;br /&gt;          \033[s&lt;br /&gt;        - Restore cursor position:&lt;br /&gt;          \033[u&lt;br /&gt;&lt;br /&gt; The latter two codes are NOT honored by many terminal emulators. The&lt;br /&gt;only ones that I'm aware of that do are xterm and nxterm - even though&lt;br /&gt;the majority of terminal emulators are based on xterm code. As far as&lt;br /&gt;I can tell, rxvt, kvt, xiterm, and Eterm do not support them. They are&lt;br /&gt;supported on the console.&lt;br /&gt;&lt;br /&gt;$ echo -en "\033[s\033[7B\033[1;34m BASH BASH\033[u\033[0m"&lt;br /&gt;&lt;br /&gt; Above line  saves the current cursor position(\033[s), then move the&lt;br /&gt;cursor seven lines down the screen(\033[7B),print the word "BASH BASH"&lt;br /&gt;in dark blue color(\033[1;34m), and then return to where it started to&lt;br /&gt;produce a normal prompt(\033[u), and also back to the normal color&lt;br /&gt;(\033[0m).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Example:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ echo -en "\033[s\033[7B\033[1;34m BASH BASH\033[u\033[0m"&lt;br /&gt;$&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: Blue;"&gt;BASH BASH&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;Using tput command:&lt;/h4&gt;&lt;br /&gt; As with so many things in Unix, there is more than one way to achieve&lt;br /&gt;the same ends.A utility called &lt;b&gt;tput&lt;/b&gt; can also be used to move the cursor&lt;br /&gt;around the screen, get back information about the status of the termi-&lt;br /&gt;nal, or set colors.&lt;br /&gt;&lt;br /&gt; &lt;b&gt;man tput&lt;/b&gt; doesn't go into much detail about the available commands,but&lt;br /&gt;&lt;b&gt;man terminfo&lt;/b&gt; will give you a huge list of capabilities, many of which&lt;br /&gt;are device independent, and therefore better than the escape sequences&lt;br /&gt;previously mentioned.&lt;br /&gt;&lt;br /&gt;Here is some useful tput capabilities:&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;tput Cursor Movement Capabilities:&lt;/h5&gt;&lt;br /&gt;&lt;b&gt;tput cup Y X&lt;/b&gt;&lt;br /&gt;        Move cursor to screen location X,Y (top left is 0,0)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput sc&lt;/b&gt;&lt;br /&gt;        Save the cursor position&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput rc&lt;/b&gt;&lt;br /&gt;        Restore the cursor position&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput lines&lt;/b&gt;&lt;br /&gt;        Output the number of lines of the terminal&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput cols&lt;/b&gt;&lt;br /&gt;        Output the number of columns of the terminal&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput cub N&lt;/b&gt;&lt;br /&gt;        Move N characters left&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput cuf N&lt;/b&gt;&lt;br /&gt;        Move N characters right&lt;br /&gt;&lt;b&gt;tput cuu N&lt;/b&gt;&lt;br /&gt;        up N lines&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput cud N&lt;/b&gt;&lt;br /&gt;        down N lines&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;tput Colour Capabilities :&lt;/h5&gt;&lt;br /&gt;&lt;b&gt;tput setab [1-7] &lt;/b&gt;&lt;br /&gt;        Set a background colour using ANSI escape&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput setb [1-7] &lt;/b&gt;&lt;br /&gt;        Set a background colour&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput setaf [1-7] &lt;/b&gt;&lt;br /&gt;        Set a foreground colour using ANSI escape&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput setf [1-7] &lt;/b&gt;&lt;br /&gt;        Set a foreground colour&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;tput Text Mode Capabilities: &lt;/h5&gt;&lt;br /&gt;&lt;b&gt;tput bold &lt;/b&gt;&lt;br /&gt;        Set bold mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput dim &lt;/b&gt;&lt;br /&gt;        turn on half-bright mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput smul &lt;/b&gt;&lt;br /&gt;        begin underline mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput rmul &lt;/b&gt;&lt;br /&gt;        exit underline mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput rev &lt;/b&gt;&lt;br /&gt;        Turn on reverse mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput smso &lt;/b&gt;&lt;br /&gt;        Enter standout mode (bold on rxvt)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput rmso &lt;/b&gt;&lt;br /&gt;        Exit standout mode&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput sgr0 &lt;/b&gt;&lt;br /&gt;        Turn off all attributes (doesn't work quite as expected).&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;tput Clear and Insert Capabilities :&lt;/h5&gt;&lt;br /&gt;&lt;b&gt;tput ech N &lt;/b&gt;&lt;br /&gt;        Erase N characters&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput clear &lt;/b&gt;&lt;br /&gt;        clear screen and home cursor&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput el1 &lt;/b&gt;&lt;br /&gt;        Clear to beginning of line&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput el &lt;/b&gt;&lt;br /&gt;        clear to end of line&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput ed &lt;/b&gt;&lt;br /&gt;        clear to end of screen&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput ich N &lt;/b&gt;&lt;br /&gt;        insert N characters (moves rest of line forward!)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;tput il N &lt;/b&gt;&lt;br /&gt;        insert N lines&lt;br /&gt;&lt;br /&gt;  This is by no means a complete list of what terminfo and tput allow,&lt;br /&gt;in fact it's only the beginning. man tput and man terminfo if you want&lt;br /&gt;to know more.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Example:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ tput sc;tput cup 4 35;tput setaf 2;echo "Hello World";\&lt;br /&gt;&gt; tput rc;tput sgr0&lt;br /&gt;$ &lt;br /&gt;&lt;br /&gt;   &lt;span style="color: Green;"&gt;                                Hello World&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Bellow example shows two different ways to achieve same task:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ tput sc;tput  cud 7;tput setaf 4;echo BASH BASH; tput rc;\&lt;br /&gt;&gt; tput sgr0&lt;br /&gt;$ echo -en "\033[s\033[7B\033[0;34m BASH BASH\033[u\033[0m"&lt;/div&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-2557437381638152348?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2557437381638152348/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/04/how-to-handle-cursor-movement-in-shell.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2557437381638152348'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2557437381638152348'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/04/how-to-handle-cursor-movement-in-shell.html' title='How to Handle Cursor Movement in a Shell Script'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-9044038767825826682</id><published>2010-03-28T11:26:00.000-07:00</published><updated>2010-03-28T11:44:06.910-07:00</updated><title type='text'>Creating a User Group and Shared Directory</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt; To understand this article you should to be familiar with Linux adva-&lt;br /&gt;nced file permissions, otherwise go throw bellow link before following&lt;br /&gt;this article.&lt;br /&gt;&lt;a href="http://bashscript.blogspot.com/2010/03/unixlinux-advanced-file-permissions.html"&gt;Advanced File Permissions&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;My Obejective: &lt;/b&gt;&lt;br /&gt; I have a folder which I want to share with "rw" permissions for a&lt;br /&gt;selected group of users. Let's say the folder is &lt;b&gt;/home/project&lt;/b&gt; and I&lt;br /&gt;want to share it with the group development. What I want is not only&lt;br /&gt;having users accessing files in /home/project with rw access, but also&lt;br /&gt;to ensure that all files created in /home/project will have ownership&lt;br /&gt;&lt;b&gt;username:development&lt;/b&gt;  and permissions &lt;b&gt;-rw-rw-r--.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Sharing a directory among users in same group is one of the essential&lt;br /&gt;tasks.To let a group of users work on a set of files without infringing&lt;br /&gt;on security, you'll have to do this:&lt;br /&gt;&lt;br /&gt;1.Create a common group for these users in /etc/group&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# groupadd development&lt;br /&gt;check group created or not&lt;br /&gt;# tail -1 /etc/group&lt;br /&gt;development:x:501:&lt;/div&gt;&lt;br /&gt;2.Add user project administrator (padmin) and setup password&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# useradd -g development -d /home/project -c "Project Admin" \&lt;br /&gt;-m padmin&lt;br /&gt;# tail -1 /etc/passwd&lt;br /&gt;padmin:x:501:501:Project Admin:/home/project:/bin/bash&lt;br /&gt;#passwd padmin&lt;/div&gt;&lt;br /&gt;3.Create separate user accounts for the rest of users but specify the&lt;br /&gt;  same home directory.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# useradd -d /home/project/ -g development user1&lt;br /&gt;# passwd user1&lt;br /&gt; Add another user:&lt;br /&gt;# useradd -d /home/project/ -g development user2&lt;br /&gt;# passwd user2&lt;/div&gt;&lt;br /&gt;Create as many user accounts as you want.&lt;br /&gt;&lt;br /&gt;4.Make sure the home directory and all subdirectories are not owned by&lt;br /&gt;  any of the users. Use &lt;b&gt;chown&lt;/b&gt; to surrender ownership to &lt;b&gt;padmin&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# chown padmin:development /home/project/&lt;br /&gt;# ls -ld /home/project/&lt;br /&gt;drwxrwxr-x 18 padmin development 4096 Mar 28 16:18 /home/project/&lt;/div&gt;&lt;br /&gt;5.Make the directories group-writable and set their SGID and Sticky&lt;br /&gt;  Bits with &lt;b&gt;chmod 3775&lt;/b&gt;  (1 for sticky and 2 for SGID).&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# chmod -R 3775 /home/project/&lt;br /&gt;# ls -ld /home/project/&lt;br /&gt;drwxrwsr-t 18 padmin development 4096 Mar 28 18:22 /home/project/&lt;/div&gt;&lt;br /&gt; In this scenario, every user of the group has write permission on the&lt;br /&gt;directory and can create files and directories, but can only delete&lt;br /&gt;those he owns. SGID bit ensures  that all files created in&lt;br /&gt;&lt;b&gt;/home/project&lt;/b&gt; will have ownership &lt;b&gt;username:development&lt;/b&gt;  and Sticky bit&lt;br /&gt;ensures that only owner can delete files those he owns&lt;br /&gt;&lt;br /&gt; Note that setting the SGID permission on a directory only affects the&lt;br /&gt;groupID of new files and subdirectories created after the SGID bit is&lt;br /&gt;set, and is not applied to existing entities. Setting the setgid bit&lt;br /&gt;on existing subdirectories must be done manually.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Can You Inherit File Permissions?&lt;/h4&gt;&lt;br /&gt; When you create a file or directories under a directory the default&lt;br /&gt;permission for them will be determined by your &lt;b&gt;umask&lt;/b&gt;, files or&lt;br /&gt;directories won't inherit parent directory permissions, only SGID bit&lt;br /&gt;inherited by newly created directories under it. So even your shared&lt;br /&gt;directory has group writable, you can't edit other users files.&lt;br /&gt;&lt;br /&gt;Login as &lt;b&gt;user1&lt;/b&gt; and create a &lt;b&gt;temp&lt;/b&gt; file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# su – user1&lt;br /&gt;$ touch temp ; ls -l temp&lt;br /&gt;-rw-r--r-- 1 user1 development 0 Mar 28 18:54 temp&lt;/div&gt;&lt;br /&gt;Now logins as &lt;b&gt;user2&lt;/b&gt; and try to edit &lt;b&gt;temp&lt;/b&gt; file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# su - user2&lt;br /&gt;$ cat &gt; temp&lt;br /&gt;-bash: temp: Permission denied&lt;/div&gt;&lt;br /&gt; There is no way to inherit permissions from a directory, it's contro-&lt;br /&gt;lled by the process's &lt;b&gt;umask&lt;/b&gt;. But there is a way to make file permissi-&lt;br /&gt;ons group writable when it is created.Add &lt;b&gt;umask 002&lt;/b&gt; command to &lt;b&gt;.bashrc&lt;/b&gt;&lt;br /&gt;file if it exist, otherwise create it and add the command.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# su - padmin&lt;br /&gt;$ ls -l .bashrc&lt;br /&gt;-rwxrwsr-t 1 padmin development 124 Mar 28 13:05 .bashrc&lt;br /&gt;$ cat &gt;&gt; .bashrc&lt;br /&gt;umask 002&lt;br /&gt;Ctrl+d&lt;/div&gt;&lt;br /&gt;Now login as &lt;b&gt;user1&lt;/b&gt; and create a temp file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# su - user1&lt;br /&gt;$ touch temp ; ls -l temp&lt;br /&gt;-rw-rw-r-- 1 user1 development 0 Mar 28 19:38 temp&lt;/div&gt;&lt;br /&gt;File created with default group writable permissions.&lt;br /&gt;&lt;br /&gt;Note: Inform already logged in users to logout and login again.&lt;br /&gt;&lt;br /&gt;  &lt;b&gt; Have you thought about using ACLs? They will give you much finer&lt;br /&gt;grained control over the permissions you can set on files and directo-&lt;br /&gt;ries. ACLs will also allow you to set a default mask for any given&lt;br /&gt;directory.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; To know more about ACLs(Access Control Lists) Google it as Linux acls&lt;br /&gt; or wait for my next article.&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-9044038767825826682?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/9044038767825826682/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/03/creating-user-group-and-shared.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9044038767825826682'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9044038767825826682'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/03/creating-user-group-and-shared.html' title='Creating a User Group and Shared Directory'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-612808398648198109</id><published>2010-03-27T10:51:00.000-07:00</published><updated>2010-12-20T14:25:07.350-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>UNIX/Linux Advanced File Permissions - SUID,SGID and Sticky Bit</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;   After you have worked for a while with Linux you discover probably&lt;br /&gt;that there is much more to file permissions than just the "rwx" bits.&lt;br /&gt;When you look around in your file system you will see "s" and "t"&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$  ls -ld /tmp&lt;br /&gt;drwxrwxrwt 29 root root 36864 Mar 21 19:49 /tmp&lt;br /&gt;&lt;br /&gt;$  which passwd&lt;br /&gt;/usr/bin/passwd&lt;br /&gt;&lt;br /&gt;$  ls -l /usr/bin/passwd&lt;br /&gt;-rwsr-xr-x 1 root root 22984 Jan  6  2007 /usr/bin/passwd&lt;/div&gt;&lt;br /&gt; What is this "s" and "t" bit? The vector of permission bits is really&lt;br /&gt;4 * 3 bits long. Yes there are 12 permission bits,not just 9.The first&lt;br /&gt;three bits are special and are frequently zero. And you almost always&lt;br /&gt;learn about the trailing 9 bits first.Some people stop there and never&lt;br /&gt;learn those first three bits.&lt;br /&gt;&lt;br /&gt; The forth permission bit is used only when a special mode of a file&lt;br /&gt;needs to be set. It has the value 4 for SUID, 2 for SGID and 1 for the&lt;br /&gt;sticky bit. The other 3 bits have their usual significance.&lt;br /&gt;&lt;br /&gt; Here we will discuss about the 3 special attributes other than the&lt;br /&gt;common read/write/execute:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;     1.Set-User-Id (SUID)&lt;br /&gt;        2.Set-Group-Id (SGID)&lt;br /&gt;        3.Sticky Bit&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Set-User_Id (SUID): Power for a Moment:  &lt;/h4&gt;&lt;br /&gt; By default, when a user executes a file, the process which results in&lt;br /&gt;this execution has the same permissions as those of the user. In fact,&lt;br /&gt;the process inherits his default group and user identification.&lt;br /&gt;&lt;br /&gt; If you set the SUID attribute on an executable file, the process res-&lt;br /&gt;ulting in its execution doesn't use the user's identification but the&lt;br /&gt;user identification of the file owner.&lt;br /&gt;&lt;br /&gt; The SUID mechanism,invented by Dennis Ritchie,is a potential security&lt;br /&gt;hazard. It lets a user acquire hidden powers by running such a file&lt;br /&gt;owned by root.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ls -l /etc/passwd /etc/shadow /usr/bin/passwd&lt;br /&gt;-rw-r--r-- 1 root root  2232 Mar 15 00:26 /etc/passwd&lt;br /&gt;-r-------- 1 root root  1447 Mar 19 19:01 /etc/shadow&lt;/div&gt;&lt;br /&gt; The listing shows that &lt;b&gt;passwd&lt;/b&gt; is readable by all, but &lt;b&gt;shadow&lt;/b&gt; is unre-&lt;br /&gt;adable by group and others. When a user running the program belongs to&lt;br /&gt;one of these two categories (probably, others), so access fails in the&lt;br /&gt;read test on shadow. suppose normal user wants to change his password,&lt;br /&gt;How can he do that? He can do that by running /usr/bin/passwd. Many&lt;br /&gt;UNIX/Linux programs have a special permission mode that lets users&lt;br /&gt;update sensitive system files –like /etc/shadow --something they can't&lt;br /&gt;do directly with an editor. This is true of the &lt;b&gt;passwd&lt;/b&gt; program.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ls -l /usr/bin/passwd&lt;br /&gt;-rwsr-xr-x 1 root root 22984 Jan  6  2007 /usr/bin/passwd&lt;/div&gt;&lt;br /&gt;The &lt;b&gt;s&lt;/b&gt; letter in the user category of the permission field represents a&lt;br /&gt;special mode known as the set-user-id (SUID). This mode lets a process&lt;br /&gt;have the privileges of the owner of the file during the instance of&lt;br /&gt;the program. Thus when a non privileged user executes &lt;b&gt;passwd&lt;/b&gt;, the eff-&lt;br /&gt;ective UID of the process is not the user's, but of root's – the owner&lt;br /&gt;of the program. This SUID privilege is then used by &lt;b&gt;passwd&lt;/b&gt; to edit&lt;br /&gt;/etc/shadow.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;What is effective user-id:&lt;/h5&gt;&lt;br /&gt; Every process really has two user IDs: the effective user ID and the&lt;br /&gt;real user ID. (Of course, there's also an effective group ID and real&lt;br /&gt;group ID.Just about everything that's true about user IDs is also true&lt;br /&gt;about group IDs) Most of the time,the kernel checks only the effective&lt;br /&gt;user ID. For example, if a process tries to open a file, the kernel&lt;br /&gt;checks the effective user ID when deciding whether to let the process&lt;br /&gt;access the file.&lt;br /&gt;&lt;br /&gt;  Save the following script under the name &lt;b&gt;reids.pl&lt;/b&gt; and make it&lt;br /&gt;executable &lt;b&gt;(chmod 755 reids.pl). &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/usr/bin/perl&lt;br /&gt;# print real UID&lt;br /&gt;print "Real UID: $&lt;\n";&lt;br /&gt;# print real GID&lt;br /&gt;print "Real GID: $(\n";&lt;br /&gt;# print effective UID&lt;br /&gt;print "Effective UID: $&gt;\n";&lt;br /&gt;# print effective GID&lt;br /&gt;print "Effective GID: $)\n";&lt;/div&gt;&lt;br /&gt;&lt;b&gt;check file permissions:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ls -l reids.pl&lt;br /&gt;-rwxr-xr-x 1 venu venu 203 Mar 24 10:40 reids.pl&lt;/div&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Note:&lt;/b&gt; For security reasons the s-bit works only when used on binaries&lt;br /&gt;(compiled code) and not on scripts (an exception are perl scripts).&lt;br /&gt;Scripts,i.e. programs that cannot be executed by the kernel directory&lt;br /&gt;but need an interpreter such as the Bourne shell or Java,can have&lt;br /&gt;their setuid bit set, but it doesn't have any effect. There are some&lt;br /&gt;platforms that honor the s bits even on scripts ( some System V vari-&lt;br /&gt;ants, for example), but most systems don't because it has proven such&lt;br /&gt;a security headache - most interpreters simply aren't written with&lt;br /&gt;much security in mind. Set the SUID bit on shell script is useless,&lt;br /&gt;that's why I am using perl script here.&lt;/div&gt;&lt;br /&gt; When you run the script you will see that the process that runs it&lt;br /&gt;gets your user-ID and your group-ID:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ./reids.pl&lt;br /&gt;Real UID: 500&lt;br /&gt;Real GID: 500 500&lt;br /&gt;Effective UID: 500&lt;br /&gt;Effective GID: 500 500&lt;/div&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Note: &lt;/b&gt;If you get an error like this:&lt;br /&gt;&lt;b&gt;Can't do setuid (cannot exec sperl)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In Debian install perl-suid using following command:&lt;br /&gt;&lt;b&gt;apt-get install perl-suid&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In Centos install perl-suidperl using following command:&lt;br /&gt;&lt;b&gt;yum install perl-suidperl&lt;/b&gt;&lt;/div&gt;&lt;br /&gt; Now change owner ship to  another user (Do it as an administrator).&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# chown king /home/venu/reids.pl&lt;br /&gt;# ls -l /home/venu/reids.pl&lt;br /&gt;-rwxr-xr-x 1 king venu 203 Mar 24 10:40 /home/venu/reids.pl&lt;/div&gt;&lt;br /&gt;Now run the script again.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ./reids.pl&lt;br /&gt;Real UID: 500&lt;br /&gt;Real GID: 500 500&lt;br /&gt;Effective UID: 500&lt;br /&gt;Effective GID: 500 500&lt;/div&gt;&lt;br /&gt; What you observed, the output of the program depends only on the user&lt;br /&gt;that runs it and not the one who owns the file.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;How to assign SUID permission: &lt;/h5&gt;&lt;br /&gt; The SUID for any file can be set (mostly by the superuser) with a&lt;br /&gt;special syntax of the &lt;b&gt;chmod&lt;/b&gt; command. This syntax uses the character &lt;b&gt;s&lt;/b&gt;&lt;br /&gt;as the permission. Now add SUID permission to the script &lt;b&gt;reids.pl&lt;/b&gt; :&lt;br /&gt;&lt;br /&gt;&lt;b&gt;# chmod u+s /home/venu/reids.pl&lt;/b&gt;     (Do it from root account)&lt;br /&gt;&lt;br /&gt;Now return from the super user mode to the usual non privileged mode.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ls -l reids.pl&lt;br /&gt;-rwsr-xr-x 1 king venu 203 Mar 24 10:40 reids.pl&lt;/div&gt;&lt;br /&gt; To assign SUID in an absolute manner, simply prefix 4 to whatever&lt;br /&gt;octal string you would otherwise use (like 4755 instead of 755).&lt;br /&gt;&lt;br /&gt; The file &lt;b&gt;reids.pl&lt;/b&gt; is owned by &lt;b&gt;king&lt;/b&gt; and has the s-bit set where norma-&lt;br /&gt;lly the x is for the owner of the file. This causes the file to be&lt;br /&gt;executed under the user-ID of the user that owns the file rather than&lt;br /&gt;the user that executes the file. If &lt;b&gt;venu&lt;/b&gt; runs the program then this&lt;br /&gt;looks as follows:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ perl reids.pl&lt;br /&gt;Real UID: 500&lt;br /&gt;Real GID: 500 500&lt;br /&gt;Effective UID: 503&lt;br /&gt;Effective GID: 500 500&lt;/div&gt;&lt;br /&gt; Effective user id of process is 503, this is not the venu's , but of&lt;br /&gt;king's - the owner of the program. As you can see this is a very powe-&lt;br /&gt;rful feature especially if root owns the file with s-bit set. Any user&lt;br /&gt;can then do things that normally only root can do.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Caution:&lt;/b&gt;    When you write a SUID program then you must make sure that&lt;br /&gt;it can only be used for the purpose that you intended it to be used.&lt;br /&gt;As administrator, you must keep track of all SUID programs owned by&lt;br /&gt;root that a user may try to create or copy. The find command easily&lt;br /&gt;locate them:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;# find /home -perm -4000 -print | mail root&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  The extra octal bit (4) signifies the SUID mode, but find treats the&lt;br /&gt;  "–" before 4000 as representing any other permissions.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Set-Group_Id (SGID):&lt;/h4&gt;&lt;br /&gt; The set-group-id (SGID) is similar to SUID except that a program with&lt;br /&gt;SGID set allows the user to have the same power as the group which&lt;br /&gt;owns the program. The SGID bit is 2,and some typical examples could be&lt;br /&gt;&lt;b&gt;chmod g+s reids.pl or chmod 2755 reids.pl.&lt;/b&gt;&lt;br /&gt;You can remove SGID bit using following commands:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ chmod g-s reids.pl&lt;br /&gt;$ chmod 755 reids.pl       (Absolute manner)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; It is really useful in case you have a real multi-user setup where&lt;br /&gt;users access each others files. As a single homeuser I haven't really&lt;br /&gt;found a lot of use for SGID. But the basic concept is the same as the&lt;br /&gt;SUID,Similar to SUID, SGID also grants privileges and access rights to&lt;br /&gt;the process running the command, but instead of receiving those of the&lt;br /&gt;file's owner it receives those of the file's group. In other words,the&lt;br /&gt;process group owner will be set to the file's group. &lt;br /&gt;&lt;br /&gt; I explain it with an example. I have created two user accounts &lt;b&gt;king &lt;/b&gt;&lt;br /&gt;and &lt;b&gt;venu&lt;/b&gt; with same home directory &lt;b&gt;project&lt;/b&gt;. king belongs to king and&lt;br /&gt;development groups, venu belongs to venu and development groups.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# groups king venu&lt;br /&gt;king : king development&lt;br /&gt;venu : venu development&lt;/div&gt;&lt;br /&gt;venu's default group is venu and king's default group is king.&lt;br /&gt;&lt;br /&gt; Login as &lt;b&gt;king&lt;/b&gt; and create &lt;b&gt;reids.pl&lt;/b&gt;  file again and make it executable&lt;br /&gt;&lt;b&gt;(using  chmod 755 reids.pl) .&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ id&lt;br /&gt;uid=503(king) gid=503(king) groups=501(development),503(king)&lt;br /&gt;$ ls -l reids.pl&lt;br /&gt;-rwxr-xr-x 1 king development 203 Mar 25 19:00 reids.pl&lt;/div&gt;&lt;br /&gt;Now login as &lt;b&gt;venu&lt;/b&gt; and run the program:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ id&lt;br /&gt;uid=501(venu) gid=504(venu) groups=501(development),504(venu)&lt;br /&gt;$ perl reids.pl&lt;br /&gt;Real UID: 501&lt;br /&gt;Real GID: 504 504 501&lt;br /&gt;Effective UID: 501&lt;br /&gt;Effective GID: 504 504 501&lt;/div&gt;&lt;br /&gt;The effective GID of the process is the venu's,but not of the king's &lt;br /&gt;-the owner of the program.&lt;br /&gt;&lt;br /&gt;Now login as &lt;b&gt;king&lt;/b&gt; and assign SGID bit to &lt;b&gt;reids.pl&lt;/b&gt; program:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 2755 reids.pl; ls -l reids.pl&lt;br /&gt;-rwxr-sr-x 1 king development 203 Mar 25 19:00 reids.pl&lt;/div&gt;&lt;br /&gt;Now login as &lt;b&gt;venu&lt;/b&gt; and run the &lt;b&gt;reids.pl&lt;/b&gt; program:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ perl reids.pl&lt;br /&gt;Real UID: 501&lt;br /&gt;Real GID: 504 504 501&lt;br /&gt;Effective UID: 501&lt;br /&gt;Effective GID: 501 504 501&lt;/div&gt;&lt;br /&gt; Real GID and Effective GID are different,here Effective GID is the&lt;br /&gt;king's - the owner of the program.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Set SGID on a directory:&lt;/h5&gt;&lt;br /&gt; When SGID is set on a directory it has a special meaning. Files crea-&lt;br /&gt;ted in a directory with SGID set will inherit the same group ownership&lt;br /&gt;as the directory itself,not the group of the user who created the file.&lt;br /&gt;If the SGID is not set the file's group ownership corresponds to the&lt;br /&gt;user's default group.&lt;br /&gt;&lt;br /&gt; In order to set the SGID on a directory or to remove it, use the&lt;br /&gt;following commands:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod g+s directory     or     $ chmod 2755 directory&lt;br /&gt;$ chmod g-s directory     or     $ chmod 755 directory&lt;/div&gt;&lt;br /&gt; As I mentioned earlier &lt;b&gt;venu&lt;/b&gt; and king's home directory is same that is&lt;br /&gt;&lt;b&gt;/home/project&lt;/b&gt;. I changed group ownership of /home/project directory&lt;br /&gt;to &lt;b&gt;development&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# ls -ld /home/project/&lt;br /&gt;drwxrwxr-x 16 root development 4096 Mar 26 00:22 /home/project/&lt;/div&gt;&lt;br /&gt;Now login as &lt;b&gt;king&lt;/b&gt; and create a temp file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ whoami&lt;br /&gt;king&lt;br /&gt;$ pwd&lt;br /&gt;/home/project/&lt;br /&gt;$ touch temp; ls -l temp&lt;br /&gt;-rw-r--r-- 1 king king 0 Mar 26 12:34 temp&lt;/div&gt;&lt;br /&gt; You can see from the ls output that the group owner for project is&lt;br /&gt;&lt;b&gt;development&lt;/b&gt;, and that the SGID bit has not been set on the directory&lt;br /&gt;yet. When &lt;b&gt;king&lt;/b&gt; creates a file in project, the group for the file is&lt;br /&gt;&lt;b&gt;king&lt;/b&gt; (king's primary gid).&lt;br /&gt;&lt;br /&gt; Set SGID bit on project directory. For that login as administrator&lt;br /&gt;and set SGID bit using following command:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# chmod g+s /home/project/&lt;br /&gt;# ls -ld /home/project/&lt;br /&gt;drwxrwsr-x 15 root development 4096 Mar 26 12:34 /home/project/&lt;/div&gt;&lt;br /&gt; From the ls output above, you know the SGID bit is set because of the&lt;br /&gt;&lt;b&gt;s&lt;/b&gt; in the third position of the group permission set,which replaces the&lt;br /&gt;&lt;b&gt;x&lt;/b&gt; in the group permissions.&lt;br /&gt;&lt;br /&gt;Now login as &lt;b&gt;king&lt;/b&gt; and create temp2 file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ whoami&lt;br /&gt;king&lt;br /&gt;$ touch temp2; ls -l temp2&lt;br /&gt;-rw-r--r-- 1 king development 0 Mar 26 13:49 temp2&lt;/div&gt;&lt;br /&gt; Notice the group ownership for temp2 file. It inherits group permiss-&lt;br /&gt;ion from the parent directory.&lt;br /&gt;&lt;br /&gt; Enabling SGID on a directory is extremely useful when you have a&lt;br /&gt;group of users with different primary groups working on the same set&lt;br /&gt;of files.&lt;br /&gt;&lt;br /&gt;        For system security reasons it is not a good idea to set many&lt;br /&gt;program's set user or group ID bits any more than necessary,since this&lt;br /&gt;can allow an unauthorized user privileges in sensitive system areas.If&lt;br /&gt;the program has a flaw that allows the user to break out of the inten-&lt;br /&gt;ded use of the program, then the system can be compromised.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Sticky bit:&lt;/h4&gt;&lt;br /&gt; The sticky bit(also called the saved text bit) is the last permission&lt;br /&gt;bit remaining to be discussed. It applies to both regular files and&lt;br /&gt;directories. When applied to a regular file, it ensures that the text&lt;br /&gt;image of a program with the bit set is permanently kept in the swap&lt;br /&gt;area so that it can be reloaded quickly when the program's turn to use&lt;br /&gt;the CPU arrives. Previously, it made sense to have this bit set for&lt;br /&gt;programs like vi and emacs. Today,machines with ultra-fast disk drives&lt;br /&gt;and lots of cheap memory don't need this bit for ordinary files and&lt;br /&gt;that is also useless.&lt;br /&gt;&lt;br /&gt; However, the sticky bit become a useful security feature when used&lt;br /&gt;with a directory. The UNIX/Linux system allows users to create files&lt;br /&gt;in /tmp, but none can delete files not owned by him. That's possible&lt;br /&gt;because sticky bit set for /tmp directory.&lt;br /&gt;&lt;br /&gt; The /tmp directory is typically world-writable and looks like this&lt;br /&gt;in a listing:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# ls -ld /tmp&lt;br /&gt;drwxrwxrwt 32 root root 36864 Mar 27 12:38 /tmp&lt;/div&gt;&lt;br /&gt; Everyone can read,write and access the directory.The &lt;b&gt;t&lt;/b&gt; indicates that&lt;br /&gt;only the user (root and owner of the directory,of course) that created&lt;br /&gt;a file in this directory can delete that file.&lt;br /&gt;&lt;br /&gt; In order to set or to remove the sticky bit, use the following&lt;br /&gt;commands:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod +t directory     or    $ chmod 1754 directory&lt;br /&gt;$ chmod -t directory     or    $ chmod  754 directory&lt;/div&gt;&lt;br /&gt;&lt;b&gt; Note:&lt;/b&gt; 754 permissions for a directory are powerful enough to guard&lt;br /&gt;your directories from intruders with malicious intentions, that's why&lt;br /&gt;I used 754 as default,if yow want you can change it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I logged in as &lt;b&gt;king&lt;/b&gt; and created a &lt;b&gt;temp&lt;/b&gt; file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ whoami&lt;br /&gt;king&lt;br /&gt;$ pwd&lt;br /&gt;/home/project/&lt;br /&gt;$ touch temp; ls -l&lt;br /&gt;-rw-r--r-- 1 king  king           0 Mar 27 13:44 temp&lt;/div&gt;&lt;br /&gt;Now logged in as &lt;b&gt;venu&lt;/b&gt; and try to delete &lt;b&gt;temp&lt;/b&gt; file.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ whoami&lt;br /&gt;venu&lt;br /&gt;$ rm temp&lt;br /&gt;rm: remove write-protected regular empty file `temp'? Y&lt;br /&gt;$ ls temp&lt;br /&gt;ls: temp: No such file or directory&lt;/div&gt;&lt;br /&gt;So what happened? venu deleted file owned by king.&lt;br /&gt;&lt;br /&gt;Assign sticky bit to the &lt;b&gt;project&lt;/b&gt; directory.As a owner of the directory&lt;br /&gt;or administrator.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;# chmod +t /home/project&lt;br /&gt;# ls -ld /home/project/&lt;br /&gt;drwxrwxr-t 15 root development 4096 Mar 27 13:46 /home/project/&lt;/div&gt;&lt;br /&gt; From the &lt;b&gt;ls&lt;/b&gt; output above, you know the sticky bit is set because of&lt;br /&gt;the &lt;b&gt;t&lt;/b&gt; in the third position of the &lt;b&gt;other&lt;/b&gt; permission set,which replaces&lt;br /&gt;the &lt;b&gt;x&lt;/b&gt; in the &lt;b&gt;other&lt;/b&gt; permissions.&lt;br /&gt;&lt;br /&gt;Now repeat same steps again,then you get the following message:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ whoami&lt;br /&gt;venu&lt;br /&gt;$ ls -l temp&lt;br /&gt;-rw-r--r-- 1 king king 0 Mar 27 17:36 temp&lt;br /&gt;$ rm temp&lt;br /&gt;rm: remove write-protected regular empty file `temp'? y&lt;br /&gt;rm: cannot remove `temp': Operation not permitted&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Observation:&lt;/b&gt; Login as normal user and create a file.&lt;br /&gt;[venu@localhost ~]$ touch sample&lt;br /&gt;[venu@localhost ~]$ ls -l sample &lt;br /&gt;-rw-rw-r-- 1 venu venu 0 Dec 21 03:41 sample&lt;br /&gt;&lt;br /&gt;Now change permissions to 644&lt;br /&gt;&lt;br /&gt;[venu@localhost ~]$ chmod 644 sample &lt;br /&gt;[venu@localhost ~]$ ls -l sample &lt;br /&gt;-rw-r--r-- 1 venu venu 0 Dec 21 03:41 sample&lt;br /&gt;&lt;br /&gt;Now assign SUID permission.&lt;br /&gt;&lt;br /&gt;[venu@localhost ~]$ chmod u+s sample &lt;br /&gt;[venu@localhost ~]$ ls -l sample &lt;br /&gt;-rwSr--r-- 1 venu venu 0 Dec 21 03:41 sample&lt;br /&gt;&lt;br /&gt;After setting SUID, if you see 'S' then it means that the file has no &lt;br /&gt;executable permissions for that user.&lt;br /&gt;&lt;br /&gt;Now remove SUID permission and change permissions to 744. Then assign &lt;br /&gt;SUID permission. You should see a smaller 's' in the executable permi-&lt;br /&gt;ssion position.&lt;br /&gt;&lt;br /&gt;[venu@localhost ~]$ chmod u-s sample &lt;br /&gt;[venu@localhost ~]$ chmod 744 sample &lt;br /&gt;[venu@localhost ~]$ chmod u+s sample &lt;br /&gt;[venu@localhost ~]$ ls -l sample &lt;br /&gt;-rwsr--r-- 1 venu venu 0 Dec 21 03:41 sample&lt;br /&gt;&lt;br /&gt;Same is applicable for SGID and Stickybit.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-612808398648198109?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/612808398648198109/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/03/unixlinux-advanced-file-permissions.html#comment-form' title='32 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/612808398648198109'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/612808398648198109'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/03/unixlinux-advanced-file-permissions.html' title='UNIX/Linux Advanced File Permissions - SUID,SGID and Sticky Bit'/><author><name>venu k</name><uri>http://www.blogger.com/profile/15099300607950419203</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>32</thr:total></entry></feed>
