<?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-01-29T09:59:04.494-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?max-results=100'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><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>100</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='0 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>0</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='1 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>1</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='2 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>2</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='17 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>17</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='30 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>30</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7769727689197515601</id><published>2010-03-18T08:41:00.000-07:00</published><updated>2010-12-20T14:01:56.582-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Linux File and Directory Permissions</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;What are file permissions:&lt;/h4&gt;&lt;br /&gt;&lt;b&gt;Every file or folder in Linux has access permissions. There are three&lt;br /&gt;types of permissions (what allowed to do with a file):&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;read access (symbolized by the letter r)&lt;br /&gt;write access (symbolized by the letter w)&lt;br /&gt;execute access (symbolized by the letter x)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Permissions are defined for three types of users:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;the owner of the file (symbolized by the letter u )&lt;br /&gt;the group that the owner belongs to (symbolized by the letter g)&lt;br /&gt;other users (symbolized by the letter o)&lt;br /&gt;&lt;br /&gt;Thus, Linux file permissions are nine bits of information. The table&lt;br /&gt;below shows the syntax:&lt;br /&gt;&lt;table border="1"&gt;&lt;tr ALIGN="center"&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt;&lt;tr ALIGN="center"&gt;&lt;td&gt;File&lt;/td&gt;&lt;td COLSPAN="3"&gt;User Permissions&lt;/td&gt;&lt;td COLSPAN="3"&gt;Group Permissions&lt;/td&gt;&lt;td COLSPAN="3"&gt;Other Permissions&lt;/td&gt;&lt;/tr&gt;&lt;tr ALIGN="center"&gt;&lt;td&gt;Type&lt;/td&gt;&lt;td&gt;Read&lt;/td&gt;&lt;td&gt;Write&lt;/td&gt;&lt;td&gt;Execute&lt;/td&gt;&lt;td&gt;Read&lt;/td&gt;&lt;td&gt;Write&lt;/td&gt;&lt;td&gt;Execute&lt;/td&gt;&lt;td&gt;Read&lt;/td&gt;&lt;td&gt;Write&lt;/td&gt;&lt;td&gt;Execute&lt;/td&gt;&lt;/tr&gt;&lt;tr ALIGN="center"&gt;&lt;td&gt;d&lt;/td&gt;&lt;td&gt;r&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;e&lt;/td&gt;&lt;td&gt;r&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;e&lt;/td&gt;&lt;td&gt;r&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;e&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&amp;#45; &amp;gt; The first character is not a permission bit, it simply indicates&lt;br /&gt;    the type of file as indicated in the table below:&lt;br /&gt;&lt;table&gt;&lt;tr align="center"&gt;&lt;th width="86"&gt;&lt;strong&gt;Character&lt;/strong&gt;&lt;/th&gt; &lt;th width="172"&gt;&lt;strong&gt;Type of file&lt;/strong&gt;&lt;/th&gt;&lt;/th&gt;&lt;tr&gt;&lt;td align="center"&gt;- &lt;/td&gt;&lt;td&gt;regular file&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td align="center"&gt;d&lt;/td&gt;&lt;td&gt;directory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;l&lt;/td&gt;&lt;td&gt;symbolic link&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;s&lt;/td&gt;&lt;td&gt;socket&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;p&lt;/td&gt;&lt;td&gt;named pipe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;c&lt;/td&gt;&lt;td&gt;character device file (unbuffered)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;b&lt;/td&gt;&lt;td&gt;blocked device file (buffered)&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Remaining nine bits are permission bits.&lt;br /&gt;&lt;br /&gt;&amp;#45;&amp;gt; Characters 2-4 show owner of the file permissions.&lt;br /&gt;    Character 2 indicates read permission, character 3 indicates write&lt;br /&gt;     permission, and character 4 indicates execute permission.&lt;br /&gt;&amp;#45;&amp;gt; Characters 5-7 show group permissions(group to which belong the file).&lt;br /&gt;     Character 5=read, 6=write, 7=execute .&lt;br /&gt;&amp;#45;&amp;gt; Characters 8-10 show permissions for all other users. (users who are&lt;br /&gt;  neither a member of the Group nor the owner of the file)&lt;br /&gt;     Character 8=read, 9=write, 10=execute.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Ownership:&lt;/b&gt; When you create a file, you automatically become its&lt;br /&gt;owner. The owner has full authority to tamper with a file's contents&lt;br /&gt;and permissions –a privilege not available with others except the root&lt;br /&gt;user.Similarly, You can create, modify  or remove files in a directory&lt;br /&gt;if you are the owner of the directory.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Group Ownership:&lt;/b&gt; When creating a user account,the system admin-&lt;br /&gt;istrator also assigns the user to some group. People working on a&lt;br /&gt;project are generally assigned a common group, and all files created&lt;br /&gt;by group members (who have separate user ids) will have the same group&lt;br /&gt;owner. However, make no mistake : The privileges of the group are set&lt;br /&gt;by the owner of the file and not by the group members.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;ls -l   : Listing File Attributes&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ ls -l sample&lt;br /&gt;-rwxr-xr-- 1 king development 0 Mar 15 00:26 sample&lt;br /&gt;&lt;/div&gt;&lt;br /&gt; ls -l displays most attributes of a file – like its permissions,size&lt;br /&gt;and ownership details. If file is a directory,to list attributes of a&lt;br /&gt;directory use “ls -ld”.&lt;br /&gt;&lt;br /&gt; As you can see in this example, the "ls -l" command gives a lot of&lt;br /&gt;information about the file "sample":&lt;br /&gt;&lt;br /&gt;     - Its name, "sample";&lt;br /&gt;     - Its permissions, "-rwxr-xr--";&lt;br /&gt;     - Its owner, "king";&lt;br /&gt;     - Its group, "development";&lt;br /&gt;     - And other information time,size,date etc..&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Permissions: rwx r-x r--&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;r&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;td&gt;Read access is allowed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;b&gt;w&lt;/b&gt;&lt;/td&gt;&lt;td&gt;Write access is allowed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;    &lt;td align="center"&gt;&lt;b&gt;x&lt;/b&gt;&lt;/td&gt;&lt;td&gt;Execute access is allowed&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;  &lt;tr&gt;&lt;td align="center"&gt;&lt;b&gt;-&lt;/b&gt;&lt;/td&gt;&lt;td&gt;No permissions&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt; The first group (rwx) has all three permissions.The file is readable,&lt;br /&gt;writable and executable by the owner of the file,&lt;b&gt;King&lt;/b&gt;. But do we Know&lt;br /&gt;who the owner is? Yes we do. The third column shows king as the owner&lt;br /&gt;and the first permissions group applies to king. You have to log in&lt;br /&gt;with the username king for these privileges to apply to you.&lt;br /&gt;&lt;br /&gt; The second group (r-x) has a hyphen in the middle slot, which indica-&lt;br /&gt;tes the absence of write permission by the group owner of the file.&lt;br /&gt;This group owner is &lt;b&gt;development&lt;/b&gt;, and all users belonging to the devel-&lt;br /&gt;opment group have read and execute permissions only.&lt;br /&gt;&lt;br /&gt; The third group (r--) has the write and execute bits absent. This set&lt;br /&gt;of permissions is applicable to &lt;b&gt;others&lt;/b&gt; i.e., those who are neither the&lt;br /&gt;owner king nor belong to the development group. So this file is not&lt;br /&gt;world writable.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;chmod: CHANGING FILE PERMISSIONS&lt;/h4&gt;&lt;br /&gt;  If you are owner of the file you can set different permissions for&lt;br /&gt;the three categories of users --owner,group, and others.It's important&lt;br /&gt;that you understand them because a little learning here can be a&lt;br /&gt;dangerous thing.A faulty file permission is a sure recipe for disaster.&lt;br /&gt;&lt;br /&gt; The &lt;b&gt;chmod&lt;/b&gt; (change mode) command is used to set the permissions of&lt;br /&gt;one or more files for all three categories of users.It can be run only&lt;br /&gt;by the user(the owner) and the superuser. The command can be used in&lt;br /&gt;two ways:&lt;br /&gt;&lt;br /&gt;     1.In a relative manner by specifying the changes to the current&lt;br /&gt;       permissions.&lt;br /&gt;     2.In an absolute manner by specifying the final permissions.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Relative Permissions: &lt;/h5&gt;&lt;br /&gt;  When changing permissions in a relative manner, &lt;b&gt;chmod&lt;/b&gt; only changes&lt;br /&gt;the permissions specified in the command line and leaves the other&lt;br /&gt;permissions unchanged. In this mode it uses the following syntax:&lt;br /&gt;&lt;br /&gt;   &lt;b&gt;chmod category operation permission filename(s).&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  Bellow table shows letters to represent category, operation and&lt;br /&gt;permission:&lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;th align="center" width="86"&gt;&lt;strong&gt;Category&lt;/strong&gt;&lt;/th&gt;&lt;th align="center" width="172"&gt;&lt;strong&gt;Operation&lt;/strong&gt;&lt;/th&gt;&lt;th align="center" width="172"&gt;&lt;strong&gt;Permission&lt;/strong&gt;&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;u   User &lt;/td&gt;&lt;td&gt;+   Assigns permission&lt;/td&gt;&lt;td&gt;r   Read Permission&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;g   Group &lt;/td&gt;&lt;td&gt;-   Removes permission&lt;/td&gt;&lt;td&gt;w   Write Permission&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;o   Other &lt;/td&gt;&lt;td&gt;=   Assigns absolute permission&lt;/td&gt;&lt;td&gt;x   Execute Permission&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;a   All(ugo)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt; Now let's consider an example. First create a file temp.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ cat &gt; temp&lt;br /&gt;Hello world&lt;br /&gt;Ctrl+d&lt;br /&gt;$ ls -l temp&lt;br /&gt;-rw-r--r-- 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt; To assign execute permissions to the User of the file temp, we need&lt;br /&gt;to frame a suitable expression by using appropriate characters from&lt;br /&gt;each of the three columns of above Table. Since the file needs to be&lt;br /&gt;executable only by the user,the expression required is u+x:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod u+x temp&lt;br /&gt;$ ls -l temp&lt;br /&gt;-rwxr--r-- 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt;  The command assigns(+) execute(x) permissions to the user(u), but&lt;br /&gt;other permissions remain unchanged. To enable group and others to exe-&lt;br /&gt;cute this file, you have to use multiple characters to represent the&lt;br /&gt;user category(ugo) or simply use a it implies ugo.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod ugo+x temp&lt;br /&gt;$ ls -l temp&lt;br /&gt;-rwxr-xr-x 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt;&lt;b&gt;chmod&lt;/b&gt; command also accept more than one file name in the command line.&lt;br /&gt;When you need to assign the same set of permissions to a group files,&lt;br /&gt;all the file names have to be specified with a single &lt;b&gt;chmod&lt;/b&gt; command:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ chmod u+x temp1 temp2 temp3&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Permissions are removed with the - operator. To remove the read&lt;br /&gt;permission from both group and others, use the expression go-r:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod go-r temp ; ls -l temp&lt;br /&gt;-rwx--x--x 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt;  &lt;b&gt;chmod&lt;/b&gt; also accepts multiple expressions delimited by commas. For&lt;br /&gt;instance, to restore the original permissions to the file temp,you&lt;br /&gt;have to remove the execute permission from all(a-x) and assign read&lt;br /&gt;permission to group and others(go+r):&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod a-x,go+r temp; ls -l temp&lt;br /&gt;-rw-r--r-- 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt; More than one permission can also be set; u+rwx is a valid &lt;b&gt;chmod&lt;/b&gt;&lt;br /&gt;expression.So setting write and execute permissions for others is no&lt;br /&gt;problem:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod o+rwx temp; ls -l temp&lt;br /&gt;-rw-r--rwx 1 root root 12 Mar 16 13:32 temp&lt;/div&gt;&lt;br /&gt;&lt;h5&gt;Absolute Permissions:&lt;/h5&gt;&lt;br /&gt;    Some times you don't need to now what a file's current permissions&lt;br /&gt;are,but want to set all nine permission bits explicitly.The expression&lt;br /&gt;used by &lt;b&gt;chmod&lt;/b&gt; here is a string of three octal numbers(base 8).Each type&lt;br /&gt;of permission is assigned a number as shown :&lt;br /&gt;&lt;br /&gt;       I. Read permission    - 4&lt;br /&gt;      II. Write permission   - 2&lt;br /&gt;     III. Execute permission - 1&lt;br /&gt;&lt;br /&gt;     For each category we add the numbers that represent the assigned&lt;br /&gt;permissions. For instance, 6 represents read and write permissions,and&lt;br /&gt;7 represents all permissions.&lt;br /&gt;&lt;br /&gt;This table shows what numeric values mean:&lt;br /&gt;&lt;br /&gt;&lt;table &gt;&lt;tr&gt;&lt;th align="center"&gt;Octal digit&lt;/th&gt;&lt;th align="center"&gt;Text equivalent&lt;/th&gt;&lt;th align="center"&gt;Meaning&lt;/th&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;0&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;---&lt;/tt&gt;&lt;/td&gt;    &lt;td &gt;All types of access are denied&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;1&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;--x&lt;/tt&gt;&lt;/td&gt; &lt;td &gt;Execute access is allowed only&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;2&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;-w-&lt;/tt&gt;&lt;/td&gt;    &lt;td &gt;Write access is allowed only&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;3&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;-wx&lt;/tt&gt;&lt;/td&gt;&lt;td &gt;Write and execute access are allowed&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;4&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;r--&lt;/tt&gt;&lt;/td&gt;&lt;td &gt;Read access is allowed only&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;5&lt;/td&gt; &lt;td style="text-align:center;" &gt;&lt;tt&gt;r-x&lt;/tt&gt;&lt;/td&gt;&lt;td &gt;Read and execute access are allowed&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;6&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;rw-&lt;/tt&gt;&lt;/td&gt;&lt;td &gt;Read and write access are allowed&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;" &gt;7&lt;/td&gt;&lt;td style="text-align:center;" &gt;&lt;tt&gt;rwx&lt;/tt&gt;&lt;/td&gt; &lt;td &gt;Everything is allowed&lt;/td&gt;  &lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;   We see that "1" stands for execute only, "2" stands for write only,&lt;br /&gt;"4" stands for read only.To combine the permissions you can simply add&lt;br /&gt;1, 2 and 4 to get a needed combination. For instance, to get read and&lt;br /&gt;write permissions,you add 4 (read) and 2 (write), thus getting 6 (read&lt;br /&gt;and write). To get read and execute permissions, you add  4 (read) and&lt;br /&gt;1 (execute), thus getting 5 (read and execute).&lt;br /&gt;&lt;br /&gt; To take a simple example,if a file has read and write permissions for&lt;br /&gt;the user, the octal representation of the user's permissions will be&lt;br /&gt;4 + 2 = 6. When this exercise is repeated for the other categories, the&lt;br /&gt;result will be a three character string representing three octal digi-&lt;br /&gt;ts, with each octal digit indicating the permissions for the category.&lt;br /&gt;The sequence followed is user,group and others. You can use this method&lt;br /&gt;to assign read and write permissions to all three categories.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Examples:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  To assign all permissions to the owner,read and write permissions to&lt;br /&gt;the group,and only execute permission to the others, use this:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 761 sample ; ls -l sample&lt;br /&gt;-rwxrw---x 1 king development 0 Mar 15 00:26 sample&lt;/div&gt;&lt;br /&gt; Assign yourself full access to read and modify the file,allow members&lt;br /&gt;of the group to read it and do not allow any others access:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 640 sample ; ls -l sample&lt;br /&gt;-rw-r----- 1 king development 0 Mar 15 00:26 sample&lt;/div&gt;&lt;br /&gt;Some octal permissions and their meaning:&lt;br /&gt;&lt;br /&gt;&lt;table &gt;&lt;tr&gt;&lt;th&gt;Permissions&lt;/th&gt;&lt;th&gt;Meaning&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;"&gt;&lt;b&gt;644&lt;/b&gt;&lt;/td&gt;&lt;td &gt;owner: read and write permissions,&lt;br&gt;group: only read permissions,&lt;br&gt;others: only read permissions.&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;"&gt;&lt;b&gt;755&lt;/b&gt;&lt;/td&gt;&lt;td &gt;    owner: read, write and execute permissions,&lt;br&gt;group: read and execute permissions,&lt;br&gt;      others: read and execute permissions.&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align:center;"&gt;&lt;b&gt;754&lt;/b&gt;&lt;/td&gt;&lt;td &gt; owner: read, write and execute permissions,&lt;br&gt; group: read and execute permissions,&lt;br&gt;others: only read permissions.&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;  As long as you're the owner of a file, you can use the chmod command&lt;br /&gt;to set the permissions any way you like.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;umask: Default file and Directory permissions: &lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;  The UNIX system has the following default permissions for all files&lt;br /&gt;and directories:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;rw-rw-rw-(octal 666) for regular files.&lt;br /&gt;rwxrwxrwx(octal 777) for directories.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  However,you don't see these permissions when you create a file or a&lt;br /&gt;directory. To understand this let us first create an empty file called&lt;br /&gt;&lt;b&gt;sample&lt;/b&gt; using the &lt;b&gt;touch&lt;/b&gt; command and then try to list it.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ touch sample ; ls -l sample&lt;br /&gt;-rw-r--r-- 1 king development 0 Mar 18 00:41 sample&lt;/div&gt;&lt;br /&gt; How come that the file permissions for this file have been set to 644&lt;br /&gt;What Unix does is it uses the value stored in a variable called &lt;b&gt;umask&lt;/b&gt;&lt;br /&gt;to decide the default permissions. The &lt;b&gt;umask&lt;/b&gt; value tells Unix which of&lt;br /&gt;the three permissions are to be denied rather than granted.The current&lt;br /&gt;value of &lt;b&gt;umask&lt;/b&gt; can be easily determined by just typing &lt;b&gt;umask&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;# umask&lt;br /&gt;0022&lt;/b&gt;  (Default umask value for admin is 0022)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ umask&lt;br /&gt;0002&lt;/b&gt;  (Default umask value for normal user is 0002)&lt;br /&gt;&lt;br /&gt; Here, the first 0 indicates that what follows is an octal number.This&lt;br /&gt;octal number which has to be subtracted from the system default to ob-&lt;br /&gt;tain the actual default. This becomes 664 (666-002) for ordinary files&lt;br /&gt;and 775 (755-002) for directories.When you crate a file on this system&lt;br /&gt;,it will have the permissions rw-rw-r--.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;umask&lt;/b&gt; is a shell built-in command though it also exists as an external&lt;br /&gt;command. A user can also use this command to set a new default. Here's&lt;br /&gt;an extreme setting:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ umask 000&lt;/b&gt;                           All read-write permissions on&lt;br /&gt;&lt;br /&gt;  A umask value of 000 means that you haven't subtracted anything, and&lt;br /&gt;this could be a dangerous. The system's default then applies (666 for&lt;br /&gt;files and 777 for directories). All files and directories are then&lt;br /&gt;writable by all;  nothing could be worse than that! However,  a mask&lt;br /&gt;value of 666 or 777 doesn't make much sense either; you'll then be cr-&lt;br /&gt;eating files and directories with no permissions.&lt;br /&gt;&lt;br /&gt;   For instance, if you want all new directories to get permissions&lt;br /&gt;rwxr-xr--- and files to get permissions rw-r----- by default(modes 750&lt;br /&gt;and 640), you'll need to use a umask value which removes all rights to&lt;br /&gt;other,and write permissions to the group : 027. The command to use is:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ umask 027&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; One important thing to remember is that,no one not even the administ&lt;br /&gt;rator can turn on permissions not specified in the system wide default&lt;br /&gt;settings. However you can always use &lt;b&gt;chmod&lt;/b&gt; as and when required. The&lt;br /&gt;system wide umask setting is placed in one of the machine's startup&lt;br /&gt;scripts, and is automatically made available to all users.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Directory Permissions:&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;    Unix treats every thing as a file, directories too are treated by&lt;br /&gt;Unix as files.A directory, as Unix perceives, is a file which contains&lt;br /&gt;the names of the files present in the directory.Hence a read permission&lt;br /&gt;on a directory allows the listing of the directory contents and&lt;br /&gt;nothing else.&lt;br /&gt;&lt;br /&gt;     Directories also have their own permissions and the significance&lt;br /&gt;of these permissions differ from those of ordinary files. Read and&lt;br /&gt;write access to an ordinary file are also influenced by the permissio-&lt;br /&gt;ns of the directory housing them. It's possible that a file can't be&lt;br /&gt;accessed even though it has read permission, and can be removed even&lt;br /&gt;when it's is write protected.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Here are some typical permissions required on directories:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To understand permissions first create temp directory and create some&lt;br /&gt;files in that directory.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ mkdir temp&lt;br /&gt;$ cd temp&lt;br /&gt;$ touch a b&lt;br /&gt;$ pwd&lt;br /&gt;/home/project/temp&lt;br /&gt;$ ls -l&lt;br /&gt;total 8&lt;br /&gt;-rw-r--r-- 1 king development 0 Mar 18 18:56 a&lt;br /&gt;-rw-r--r-- 1 king development 0 Mar 18 18:56 b&lt;/div&gt;&lt;br /&gt;&amp;#45;&amp;gt; Execute permission is required for a user to cd into a directory.&lt;br /&gt;&lt;br /&gt;    change &lt;b&gt;temp&lt;/b&gt; directory permissions to 400(Read only permission).&lt;br /&gt;Then try to change directory, you won't be permitted.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 400 temp&lt;br /&gt;$ cd temp&lt;br /&gt;cd: temp: Permission denied&lt;br /&gt;$ ls temp&lt;br /&gt;a  b &lt;/div&gt;&lt;br /&gt;&amp;#45;&amp;gt;  Read permission is required for a user to use a command such as &lt;b&gt;ls&lt;/b&gt;&lt;br /&gt;to view the files contained in a directory.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 300 temp&lt;br /&gt;$ ls temp&lt;br /&gt;ls: temp: Permission denied&lt;br /&gt;$ cd temp&lt;br /&gt;$ pwd&lt;br /&gt;/home/project/temp&lt;/div&gt;&lt;br /&gt;&amp;#45;&amp;gt; Execute-only permission allows a user to access the files in a&lt;br /&gt;directory as long as the user knows the names of the files in the&lt;br /&gt;directory, and the user is allowed to read the files.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ chmod 100 temp&lt;br /&gt;$ ls temp/&lt;br /&gt;ls: temp/: Permission denied&lt;br /&gt;$ cat temp/a&lt;br /&gt;Hello world&lt;/div&gt;&lt;br /&gt;&amp;#45;&amp;gt; Write permission allows the user to create, delete, or modify any&lt;br /&gt;files or sub directories, even if the file or sub directory is owned by&lt;br /&gt;another user.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Difference in access permissions for files and &lt;br /&gt;directories:&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt; Access permissions for files and folders mean different things from&lt;br /&gt;the user standpoint. The table below shows the difference.&lt;br /&gt;&lt;br /&gt;&lt;table &gt;&lt;tr&gt;&lt;th width="86"&gt;&lt;strong&gt;Access type&lt;/strong&gt;&lt;/th&gt;&lt;th align="center" width="172"&gt;&lt;strong&gt;File&lt;/strong&gt;&lt;/th&gt;&lt;th align="center" width="300"&gt;&lt;strong&gt;Directory&lt;/strong&gt;&lt;/th&gt;        &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Read &lt;/td&gt;&lt;td&gt;If the file contents can&lt;br/&gt; be read&lt;/td&gt; &lt;td&gt; If the directory listing can &lt;br/&gt;be obtained&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write &lt;/td&gt;&lt;td&gt;If user or process can &lt;br/&gt;write to the file&lt;br/&gt;(change its contents)&lt;/td&gt;&lt;td&gt;If user or process can change&lt;br/&gt;directory contents somehow: create&lt;br/&gt;new or delete existing files in the&lt;br/&gt;directory or rename files.  &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Execute &lt;/td&gt;&lt;td&gt;If the file can &lt;br/&gt;be executed&lt;/td&gt;&lt;td&gt;If user or process can access the&lt;br/&gt;directory, that is, go to it (make &lt;br/&gt;it to be the current working direc&lt;br/&gt;tory)&lt;/td&gt;&lt;/tr&gt;&lt;/table&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-7769727689197515601?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7769727689197515601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/03/linux-file-and-directory-permissions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7769727689197515601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7769727689197515601'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/03/linux-file-and-directory-permissions.html' title='Linux File and Directory Permissions'/><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-764692414899023086</id><published>2010-02-27T09:27:00.000-08:00</published><updated>2010-02-27T10:26:55.994-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Shell Script to Upload a File to the Remote FTP Server</title><content type='html'>&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# SCRIPT  : upload.sh&lt;br /&gt;# USAGE   : ./upload.sh [-d remote directory path] -f "File-to-upload"&lt;br /&gt;#           -d option is optional by default it takes servers home dir&lt;br /&gt;#                               or&lt;br /&gt;#           ./upload.sh "Filename-to-upload"&lt;br /&gt;#&lt;br /&gt;# PURPOSE : Upload file to remote ftp server.&lt;br /&gt;&lt;br /&gt;############### DEFINE FUNCTIONS HERE ##############&lt;br /&gt;&lt;br /&gt;Usage()&lt;br /&gt;{&lt;br /&gt;  echo "                     USAGE                                  "&lt;br /&gt;  echo " $0 [-d \"Remote Directory Path\"] -f \"Filename-to-upload\""&lt;br /&gt;  echo "-------------------------- or -----------------------------"&lt;br /&gt;  echo " $0 Filename-to-upload"&lt;br /&gt;  exit 1&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;################# ARGUMENTS CHECKING #################&lt;br /&gt;&lt;br /&gt;while getopts d:f: choice&lt;br /&gt;do&lt;br /&gt;   case $choice in&lt;br /&gt;   d) Dname=$OPTARG;;&lt;br /&gt;   f) Fname=$OPTARG;;&lt;br /&gt;   esac&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;[ $# -eq 1 ] &amp;&amp; Fname=$1&lt;br /&gt;[ -z $Dname ] &amp;&amp; Dname="/"                  #change it to suit&lt;br /&gt;[ -z $Fname ] &amp;&amp; echo "Bad Arguments" &amp;&amp; Usage&lt;br /&gt;&lt;br /&gt;[ ! -e $Fname ] &amp;&amp; echo "$0: $Fname  no such file" &amp;&amp; exit 1&lt;br /&gt;&lt;br /&gt;############### DEFINE VARIABLES HERE ################&lt;br /&gt;&lt;br /&gt;Server="ftp.checksoftware.com"               #Remote ftp server.&lt;br /&gt;Username="anonymous"                         #Remote ftp user name.&lt;br /&gt;Password="venuk@gmail.com"                   #Remote ftp Password.&lt;br /&gt;&lt;br /&gt;# change above to suit&lt;br /&gt;&lt;br /&gt;############### MAIN PROGRAM STARTS HERE #############&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;# Remove comment lines within the here document before run the script&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;ftp -n $Server &amp;lt;&amp;lt;ServerEnd&lt;br /&gt;user "$Username" "$Password"&lt;br /&gt;&lt;br /&gt;# binary command Set the file transfer type to support binary file&lt;br /&gt;# transfer.So you can transfer all type of files.&lt;br /&gt;&lt;br /&gt;binary&lt;br /&gt;&lt;br /&gt;# hash command is used for printing hash-sign("#") for each data block&lt;br /&gt;# transferred. The size of data block is system dependent.In my box it&lt;br /&gt;# is 1024 bytes. hash command is a toggle switch, by default it is set&lt;br /&gt;# off most systems.&lt;br /&gt;&lt;br /&gt;hash                           # comment it if you don't need it&lt;br /&gt;&lt;br /&gt;cd $Dname&lt;br /&gt;&lt;br /&gt;# pwd command prints current directory on the remote host. This shows&lt;br /&gt;# your file is uploading to which directory.&lt;br /&gt;&lt;br /&gt;pwd                            # comment it if you don't need it&lt;br /&gt;&lt;br /&gt;put "$Fname"&lt;br /&gt;bye&lt;br /&gt;ServerEnd&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;/span&gt;&lt;/pre&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;pre  style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;[root@www ~]# ./upload.sh &lt;br /&gt;Bad Arguments&lt;br /&gt;                     USAGE                                  &lt;br /&gt; ./upload.sh [-d "Remote Directory Path"] -f "Filename-to-upload"&lt;br /&gt;-------------------------- or -----------------------------&lt;br /&gt; ./upload.sh Filename-to-upload&lt;br /&gt;[root@www ~]# ./upload.sh -f samplepic.jpg -d /incoming&lt;br /&gt;Hash mark printing on (1024 bytes/hash mark).&lt;br /&gt;257 "/incoming" is current directory.&lt;br /&gt;###############################################&lt;br /&gt;[root@www ~]# ./upload.sh samplepic.jpg &lt;br /&gt;Hash mark printing on (1024 bytes/hash mark).&lt;br /&gt;257 "/" is current directory.&lt;br /&gt;samplepic.jpg: Permission denied&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-764692414899023086?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/764692414899023086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/02/shell-script-to-upload-file-to-remote.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/764692414899023086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/764692414899023086'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/02/shell-script-to-upload-file-to-remote.html' title='Shell Script to Upload a File to the Remote FTP Server'/><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-532716372639893393</id><published>2010-01-01T02:17:00.000-08:00</published><updated>2011-01-15T06:06:17.026-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Shell Colors: colorizing 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;      Shell scripts commonly used ANSI escape codes for color output.&lt;br /&gt;Following table shows Numbers representing colors in Escape Sequences.&lt;center&gt;&lt;div class="tabledata"&gt;&lt;TABLE border=1 bgcolor=#8eba10&gt;&lt;br /&gt;&lt;tr bgcolor="#38a607"&gt;&lt;br /&gt;&lt;th&gt;Color&lt;/th&gt;&lt;br /&gt;&lt;th&gt;Foreground&lt;/th&gt;&lt;br /&gt;&lt;th&gt;Background&lt;/th&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Black&lt;/TD&gt;  &lt;TD&gt;30&lt;/TD&gt; &lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Red&lt;/TD&gt; &lt;TD&gt;31&lt;/TD&gt; &lt;TD&gt;41&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Green&lt;/TD&gt;  &lt;TD&gt;32&lt;/TD&gt; &lt;TD&gt;42&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Yellow&lt;/TD&gt;  &lt;TD&gt;33&lt;/TD&gt; &lt;TD&gt;43&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Blue&lt;/TD&gt;  &lt;TD&gt;34&lt;/TD&gt; &lt;TD&gt;44&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Magenta&lt;/TD&gt;  &lt;TD&gt;35&lt;/TD&gt; &lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;Cyan&lt;/TD&gt;  &lt;TD&gt;36&lt;/TD&gt; &lt;TD&gt;46&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;TR&gt;&lt;TD&gt;White&lt;/TD&gt;  &lt;TD&gt;37&lt;/TD&gt; &lt;TD&gt;47&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;&lt;/TABLE&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt; The numbers in the above table work for xterm terminal.Result may vary&lt;br /&gt;for other terminal emulators.&lt;br /&gt;&lt;br /&gt;Use the following template for writing colored text.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[COLORm Sample text"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The "\033[" begins the escape sequence.You can also use "\e[" instead&lt;br /&gt;of "\033[". COLOR specifies a foreground color, according to the table&lt;br /&gt;above.The "m" terminates escape sequence, and text begins immediately&lt;br /&gt;after that.&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Note:&lt;/b&gt; With an echo, the -e option enables the escape sequences.You can&lt;br /&gt;also use printf instead of echo.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;printf "\e[COLORm sample text\n"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To print Green text&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[32m Hello World"&lt;/b&gt;&lt;br /&gt;             or&lt;br /&gt;&lt;b&gt;printf "\e[32m Hello World"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The problem with above statement is that the blue color that starts&lt;br /&gt;with the 32 color code is never switched back to the regular color, so&lt;br /&gt;any text you type after the prompt and even prompt also is still in the&lt;br /&gt;Green color.&lt;br /&gt;&lt;br /&gt;To return to the plain, normal mode, we have yet another sequence.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[0m"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Now you won't see anything new on the screen, as this echo statement&lt;br /&gt;was not passed any string to display. But it has done its job, which&lt;br /&gt;was to restore the normal viewing mode. Whatever yor type now will be&lt;br /&gt;avoid of any fancy effects.&lt;br /&gt;&lt;br /&gt;Escape sequence also allow you to control the manner in which &lt;br /&gt;characters are displayed on the screen.&lt;br /&gt;&lt;br /&gt;The following table summarizes numbers representing text attributes&lt;br /&gt;in Escape Sequences.&lt;br /&gt;&lt;br/&gt;&lt;br/&gt;&lt;br /&gt;&lt;center&gt;&lt;div class="tabledata"&gt;&lt;TABLE border=1 bgcolor=#8eba10&gt;&lt;br /&gt;&lt;tr bgcolor="#38a607"&gt;&lt;br /&gt;&lt;th&gt;ANSI CODE&lt;/th&gt;&lt;br /&gt;&lt;th&gt;Meaning&lt;/th&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;Normal Characters&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Bold Characters&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;Underlined Characters&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;Blinking Characters&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Reverse video Characters&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Note:&lt;/b&gt; Blink attribute doesn't work in any terminal emulator, but it&lt;br /&gt;will work on the console.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Combining all these Escape Sequences, you can get more fancy effect.&lt;br /&gt;Use the following template for writing colored text on a colored&lt;br /&gt;background.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[COLOR1;COLOR2m sample text\033[0m"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The semicolon separated numbers "COLOR1" and "COLOR2" specify a&lt;br /&gt;foreground and a background color.The order of the numbers does not&lt;br /&gt;matter, since the foreground and background numbers fall in non-&lt;br /&gt;overlapping ranges."m" terminates the escape sequence, and the text&lt;br /&gt;begins immediately after that.Although setting the colors separately&lt;br /&gt;also work (i.e. \033[44m\033[32m).&lt;br /&gt;&lt;br /&gt;There are some differences between colors when combining colors with&lt;br /&gt;bold text attribute.&lt;br /&gt;&lt;br /&gt;The following table summarises these differences.&lt;center&gt;&lt;div class="tabledata"&gt;&lt;TABLE border=1 bgcolor=#8eba10&gt;&lt;br /&gt;&lt;tr bgcolor="#38a607"&gt;&lt;br /&gt;&lt;th&gt;Bold off&lt;/th&gt;&lt;br /&gt;&lt;th&gt;color&lt;/th&gt;&lt;br /&gt;&lt;th&gt;Bold on&lt;/th&gt;&lt;br /&gt;&lt;th&gt;color&lt;/th&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;30&lt;/td&gt;&lt;td&gt;Balck&lt;/td&gt;&lt;td&gt;1;30&lt;/td&gt;&lt;td&gt;Dark Gray&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;31&lt;/td&gt;&lt;td&gt;Red&lt;/td&gt;&lt;td&gt;1;31&lt;/td&gt;&lt;td&gt;Dark Red&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;32&lt;/td&gt;&lt;td&gt;Green&lt;/td&gt;&lt;td&gt;1;32&lt;/td&gt;&lt;td&gt;Dark Green&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;33&lt;/td&gt;&lt;td&gt;Brown&lt;/td&gt;&lt;td&gt;1;33&lt;/td&gt;&lt;td&gt;Yellow&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;34&lt;/td&gt;&lt;td&gt;Blue&lt;/td&gt;&lt;td&gt;1;34&lt;/td&gt;&lt;td&gt;Dark Blue&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;35&lt;/td&gt;&lt;td&gt;Magenta&lt;/td&gt;&lt;td&gt;1;35&lt;/td&gt;&lt;td&gt;Dark Magenta&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;36&lt;/td&gt;&lt;td&gt;Cyan&lt;/td&gt;&lt;td&gt;1;30&lt;/td&gt;&lt;td&gt;Dark Cyan&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;0;37&lt;/td&gt;&lt;td&gt;Light Gray&lt;/td&gt;&lt;td&gt;1;30&lt;/td&gt;&lt;td&gt;White&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;The following shell script prints all the colors and codes on the&lt;br /&gt;screen.&lt;br /&gt;&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:14px;"&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# This script echoes colors and codes&lt;br /&gt;&lt;br /&gt;echo -e "\n\033[4;31mLight Colors\033[0m  \t\t\033[1;4;31mDark Colors\033[0m"&lt;br /&gt;&lt;br /&gt;echo -e "\e[0;30;47m Black    \e[0m 0;30m \t\e[1;30;40m Dark Gray  \e[0m 1;30m"&lt;br /&gt;echo -e "\e[0;31;47m Red      \e[0m 0;31m \t\e[1;31;40m Dark Red   \e[0m 1;31m"&lt;br /&gt;echo -e "\e[0;32;47m Green    \e[0m 0;32m \t\e[1;32;40m Dark Green \e[0m 1;32m"&lt;br /&gt;echo -e "\e[0;33;47m Brown    \e[0m 0;33m \t\e[1;33;40m Yellow     \e[0m 1;33m"&lt;br /&gt;echo -e "\e[0;34;47m Blue     \e[0m 0;34m \t\e[1;34;40m Dark Blue  \e[0m 1;34m"&lt;br /&gt;echo -e "\e[0;35;47m Magenta  \e[0m 0;35m \t\e[1;35;40m DarkMagenta\e[0m 1;35m"&lt;br /&gt;echo -e "\e[0;36;47m Cyan     \e[0m 0;36m \t\e[1;36;40m Dark Cyan  \e[0m 1;36m"&lt;br /&gt;echo -e "\e[0;37;47m LightGray\e[0m 0;37m \t\e[1;37;40m White      \e[0m 1;37m"&lt;/div&gt;&lt;/span&gt;&lt;/pre&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://3.bp.blogspot.com/_ioCu0w5ZnYE/S8vivSas0II/AAAAAAAAAsM/GRwhr_XI_9o/s1600/colors.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 223px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/S8vivSas0II/AAAAAAAAAsM/GRwhr_XI_9o/s400/colors.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5461708275142414466" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h5&gt;Some examples:&lt;/h5&gt;&lt;br /&gt;Block background and white text&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[40;37m Hello World\033[0m"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Reverse video text attribute option interchanges fg and bg colors.&lt;br /&gt;Bellow statement prints block on white&lt;br /&gt;&lt;br /&gt;&lt;b&gt;echo -e "\033[40;37;7m Hello World\033[0m"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;echo -e "\033[33;44m Yellow text on blue background\033[0m"&lt;br /&gt;echo -e "\033[1;33;44m Bold yellow text on blue background\033[0m"&lt;br /&gt;echo -e "\033[1;4;33;44mBold yellow underlined text on blue background\033[0m"&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;The "tput" command:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;         Other than echo there is a command called tput using which we&lt;br /&gt;can control the way the output is displayed on the screen.But it is&lt;br /&gt;less flexible than ANSI escape sequences.&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-532716372639893393?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/532716372639893393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/532716372639893393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/532716372639893393'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html' title='Shell Colors: colorizing 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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_ioCu0w5ZnYE/S8vivSas0II/AAAAAAAAAsM/GRwhr_XI_9o/s72-c/colors.PNG' height='72' width='72'/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1559181891211147219</id><published>2009-12-28T22:46:00.000-08:00</published><updated>2009-12-29T01:17:56.364-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Copy Progress Bar Shell Script V2</title><content type='html'>This is the upgrade version of the ddcopy_progress.sh script posted on 2nd November.&lt;br /&gt;&lt;pre style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;# SCRIPT:  ddcopy.sh&lt;br /&gt;# PURPOSE: Copies files and shows the progress of copying.&lt;br /&gt;#&lt;br /&gt;#............................USAGE...........................&lt;br /&gt;# This script copies only files not directories&lt;br /&gt;# ddcopy &amp;lt;Source filename or Path&amp;gt;  &amp;lt;Destination filename or Path&amp;gt;&lt;br /&gt;# ddcopy &amp;lt;Source filename or Path&amp;gt;  &amp;lt;Destination Directory or Path&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;##################################################################&lt;br /&gt;#                  Arguments Checking                            #&lt;br /&gt;##################################################################&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]           # Look for parameters supplied or not&lt;br /&gt;then&lt;br /&gt;    echo "$0: missing file operand"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ $# -eq 1 ]           # Look for exactly one parameter&lt;br /&gt;then&lt;br /&gt;    echo  "$0 : missing destination file operand after '$1'"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ $# -eq 2 ]&lt;br /&gt;then&lt;br /&gt;  Source="$1"&lt;br /&gt;  Dest="$2"&lt;br /&gt;  if [ ! -e "$1" ]           # Check source file exist or not&lt;br /&gt;  then&lt;br /&gt;      echo "$0: cannot stat '$1': No such file or directory"&lt;br /&gt;      exit 1&lt;br /&gt;  elif [ -d "$1" ]&lt;br /&gt;  then&lt;br /&gt;      echo "$0: Source file '$1' is a directory"&lt;br /&gt;      exit 1&lt;br /&gt;  fi&lt;br /&gt;&lt;br /&gt;   egrep -q "^\/|\/$" &lt;&lt;&lt;$2&lt;br /&gt;&lt;br /&gt;   if [ $? -eq 0 -a ! -d $2 ]&lt;br /&gt;   then&lt;br /&gt;       echo "$0: cannot create regular file '$2' : Is a directory"&lt;br /&gt;       exit 1&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   if [ -d "$2" ]&lt;br /&gt;   then&lt;br /&gt;  filename=`basename "$1"`&lt;br /&gt;  $(egrep -q "\/$" &lt;&lt;&lt;$2) &amp;&amp; Dest="$2$filename" || Dest="$2/$filename"&lt;br /&gt;   fi&lt;br /&gt;&lt;br /&gt;   if [ -e "$Dest" -a -f "$Dest" ]&lt;br /&gt;   then&lt;br /&gt;      echo -n "$0: $Dest already exist : overwrite '$Dest' (y/n)?  "&lt;br /&gt;      read answer&lt;br /&gt;      if [ "$answer" = "n" -o "$answer" = "N" ]&lt;br /&gt;      then&lt;br /&gt;          exit 1&lt;br /&gt;      fi&lt;br /&gt;   fi&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;###################################################################&lt;br /&gt;#               DEFINE VARIABLES HERE                             #&lt;br /&gt;###################################################################&lt;br /&gt;&lt;br /&gt;Seek=0               # skip BLOCKS bs-sized blocks at start of output&lt;br /&gt;Skip=0               # skip BLOCKS bs-sized blocks at start of input&lt;br /&gt;Bsize=128000         # block size&lt;br /&gt;size=`stat -c %s "$Source"`&lt;br /&gt;&lt;br /&gt;# Bellow 25MB files copied directly with copy command&lt;br /&gt;# If you want progress bar for &lt; 25MB files also, remove bellow&lt;br /&gt;# if block&lt;br /&gt;&lt;br /&gt;if [ $size -lt 25000000 ]&lt;br /&gt;then&lt;br /&gt;    cp "$Source" "$Dest"&lt;br /&gt;    exit 0&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;blocks=$((size/Bsize))      # total blocks of input file&lt;br /&gt;&lt;br /&gt;lastblock=$((size%Bsize))   # last block,  which size  &lt; $Bsize&lt;br /&gt;&lt;br /&gt;# If last block size is &gt; 0 then add 1 block to existing blocks.&lt;br /&gt;&lt;br /&gt;if [ $lastblock -gt 0 ]&lt;br /&gt;then&lt;br /&gt;    let blocks++&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# I am dividing screen width 60 as 20 parts.Each part is filled&lt;br /&gt;# with a colour after each iteration.&lt;br /&gt;&lt;br /&gt;# Make blocks count dividable by 20.&lt;br /&gt;&lt;br /&gt;addblocks=$((blocks%20))&lt;br /&gt;if [ $addblocks -gt 0 ]&lt;br /&gt;then&lt;br /&gt;   adjustblocks=$((20-addblocks))&lt;br /&gt;   blocks=$((blocks+adjustblocks))&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;Count=$((blocks/20))&lt;br /&gt;&lt;br /&gt;# Count variable contain number of blocks to be copied for&lt;br /&gt;# each iteration&lt;br /&gt;&lt;br /&gt;###################################################################&lt;br /&gt;#            MAIN PROGRAM STARTS HERE                             #&lt;br /&gt;###################################################################&lt;br /&gt;&lt;br /&gt;printf "\e[40m\e[43m%60s\n\e[A" " "&lt;br /&gt;&lt;br /&gt;for ((i=1;i&lt;=20;i++))&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;  dd if="$Source" of="$Dest" bs=128kB seek=$Seek skip=$Skip \&lt;br /&gt;  count=$Count 2&gt;/dev/null&lt;br /&gt;&lt;br /&gt;   Skip=$((Skip+$Count))&lt;br /&gt;   Seek=$((Seek+$Count))&lt;br /&gt;   j=$((j+3))                     # 60/20 each part is 3 chars length&lt;br /&gt;&lt;br /&gt;   printf "\e[40m\e[7m%${j}s\n\e[A" " "&lt;br /&gt;#  echo -e "\033[7m   \c"&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;printf "\e[0m\n"&lt;br /&gt;#echo -e "\033[0m"&lt;br /&gt;&lt;/span&gt;&lt;/pre&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;b&gt;Screen Shot1:&lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Szmsg3RgfPI/AAAAAAAAArs/odKws7Ruwxg/s1600-h/1.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 80px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Szmsg3RgfPI/AAAAAAAAArs/odKws7Ruwxg/s400/1.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5420553307110276338" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Screen Shot2:&lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_ioCu0w5ZnYE/SzmtGrxynkI/AAAAAAAAAr0/AYESggojiw8/s1600-h/2.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 80px;" src="http://4.bp.blogspot.com/_ioCu0w5ZnYE/SzmtGrxynkI/AAAAAAAAAr0/AYESggojiw8/s400/2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5420553956859485762" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Screen Shot3:&lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SzmtUNWjL3I/AAAAAAAAAr8/sM4gO9KvyBY/s1600-h/3.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 80px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SzmtUNWjL3I/AAAAAAAAAr8/sM4gO9KvyBY/s400/3.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5420554189210333042" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1559181891211147219?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1559181891211147219/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/12/copy-progress-bar-shell-script-v2.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1559181891211147219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1559181891211147219'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/12/copy-progress-bar-shell-script-v2.html' title='Copy Progress Bar Shell Script V2'/><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/Szmsg3RgfPI/AAAAAAAAArs/odKws7Ruwxg/s72-c/1.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-5020809468391398490</id><published>2009-11-24T14:20:00.000-08:00</published><updated>2009-12-02T09:25:08.676-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Linux Fun'/><title type='text'>Linux Baby Rocker</title><content type='html'>Hello friends, &lt;br /&gt;I have watched this video on Youtube.I hope you will also enjoy it.&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-f197a24fb66dfb9f" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;&lt;param name="bgcolor" value="#FFFFFF"&gt;&lt;param name="allowfullscreen" value="true"&gt;&lt;param name="flashvars" value="flvurl=http://v19.nonxt7.googlevideo.com/videoplayback?id%3Df197a24fb66dfb9f%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1330259488%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6F6B2591FA7058D1B4243D9AB8B793BC9C0123C9.585D93371B414150502AF82F797317C75B30DD4%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Df197a24fb66dfb9f%26offsetms%3D5000%26itag%3Dw160%26sigh%3DDLEpopJH77BBgasPkfi3PLiTkgo&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"width="320" height="266" bgcolor="#FFFFFF"flashvars="flvurl=http://v19.nonxt7.googlevideo.com/videoplayback?id%3Df197a24fb66dfb9f%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1330259488%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6F6B2591FA7058D1B4243D9AB8B793BC9C0123C9.585D93371B414150502AF82F797317C75B30DD4%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Df197a24fb66dfb9f%26offsetms%3D5000%26itag%3Dw160%26sigh%3DDLEpopJH77BBgasPkfi3PLiTkgo&amp;autoplay=0&amp;ps=blogger"allowFullScreen="true" /&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;Original link for this video is http://www.youtube.com/watch?v=bYcF_xX2DE8&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-5020809468391398490?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/5020809468391398490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/linux-baby-rocker.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5020809468391398490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5020809468391398490'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/linux-baby-rocker.html' title='Linux Baby Rocker'/><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-9207553467180717093</id><published>2009-11-17T11:58:00.000-08:00</published><updated>2010-08-20T08:09:29.456-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Shell Script To Produce Prime Numbers Within A Range</title><content type='html'>&lt;pre  style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: primenumbers.sh&lt;br /&gt;# USAGE : ./primenumbers.sh &amp;lt;Range Value&amp;gt&lt;br /&gt;#                      or;&lt;br /&gt;#         ./ primenumbers.sh &amp;lt;Start Range Value&amp;gt; &amp;lt;End Range Value&amp;gt;&lt;br /&gt;# PURPOSE: Produce prime numbers within a range lmit.&lt;br /&gt;&lt;br /&gt;############### DEFINE FUNCTIONS HERE ##############&lt;br /&gt;&lt;br /&gt;Usage()&lt;br /&gt;{&lt;br /&gt;    echo "********BAD ARGUMENTS**********"&lt;br /&gt;    echo "Usage: scriptname &amp;lt;Range Value &amp;gt;"&lt;br /&gt;    echo "               or                   "&lt;br /&gt;    echo "Usage: scriptname &amp;lt;Start Range Value&amp;gt; &amp;lt;End Range Value&amp;gt;"&lt;br /&gt;    exit 1&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;################# ARGUMENTS CHECKING #################&lt;br /&gt;&lt;br /&gt;[ $# -gt 2 -o $# -lt 1 ] &amp;&amp; Usage&lt;br /&gt;[ $# -eq 1 ] &amp;&amp; Bnum=2 &amp;&amp;  Enum=$1&lt;br /&gt;[ $# -eq 2 ] &amp;&amp; Bnum=$1  Enum=$2 &amp;&amp; [ $1 -gt $2 ] &amp;&amp; Usage&lt;br /&gt;[ $1 -lt 2 ] &amp;&amp; Bnum=2&lt;br /&gt;&lt;br /&gt;############### MAIN PROGRAM STARTS HERE #############&lt;br /&gt;&lt;br /&gt;count=1&lt;br /&gt;&lt;br /&gt;while [ $Bnum -le $Enum ]&lt;br /&gt;do&lt;br /&gt;num=$Bnum&lt;br /&gt;Prime="yes"&lt;br /&gt;i=1&lt;br /&gt;&lt;br /&gt;#while [ $i -lt $((num/2)) ]&lt;br /&gt;while [ $((i*i)) -lt $((num-1)) ]&lt;br /&gt;do&lt;br /&gt;  let i++&lt;br /&gt;  if [ $((num%i)) -eq 0 ]          # you can also use `expr $num % $i`&lt;br /&gt;  then&lt;br /&gt;      Prime="no"&lt;br /&gt;      break&lt;br /&gt;  fi&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;[ "$Prime" = "yes" ] &amp;&amp; printf "%5d " $num &amp;&amp; let count++&lt;br /&gt;&lt;br /&gt;# count is used to print 10 values in a row&lt;br /&gt;&lt;br /&gt;[ $count -eq 11 ] &amp;&amp; count=1 &amp;&amp;  echo&lt;br /&gt;let Bnum++&lt;br /&gt;done&lt;br /&gt;echo&lt;br /&gt;&lt;/span&gt;&lt;/pre&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;pre&gt;&lt;br /&gt;&lt;b&gt;$ ./primenumbers.sh 1 350&lt;/b&gt;&lt;br /&gt;    2     3     5     7    11    13    17    19    23    29 &lt;br /&gt;   31    37    41    43    47    53    59    61    67    71 &lt;br /&gt;   73    79    83    89    97   101   103   107   109   113 &lt;br /&gt;  127   131   137   139   149   151   157   163   167   173 &lt;br /&gt;  179   181   191   193   197   199   211   223   227   229 &lt;br /&gt;  233   239   241   251   257   263   269   271   277   281 &lt;br /&gt;  283   293   307   311   313   317   331   337   347   349 &lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ ./primenumbers.sh 1900 2500&lt;/b&gt;&lt;br /&gt; 1901  1907  1913  1931  1933  1949  1951  1973  1979  1987 &lt;br /&gt; 1993  1997  1999  2003  2011  2017  2027  2029  2039  2053 &lt;br /&gt; 2063  2069  2081  2083  2087  2089  2099  2111  2113  2129 &lt;br /&gt; 2131  2137  2141  2143  2153  2161  2179  2203  2207  2213 &lt;br /&gt; 2221  2237  2239  2243  2251  2267  2269  2273  2281  2287 &lt;br /&gt; 2293  2297  2309  2311  2333  2339  2341  2347  2351  2357 &lt;br /&gt; 2371  2377  2381  2383  2389  2393  2399  2411  2417  2423 &lt;br /&gt; 2437  2441  2447  2459  2467  2473  2477 &lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ ./primenumbers.sh 200&lt;/b&gt;&lt;br /&gt;    2     3     5     7    11    13    17    19    23    29 &lt;br /&gt;   31    37    41    43    47    53    59    61    67    71 &lt;br /&gt;   73    79    83    89    97   101   103   107   109   113 &lt;br /&gt;  127   131   137   139   149   151   157   163   167   173 &lt;br /&gt;  179   181   191   193   197   199 &lt;br /&gt;&lt;br /&gt;&lt;b&gt;[root@localhost shell]# ./primenumbers.sh 200 100&lt;/b&gt;&lt;br /&gt;********BAD ARGUMENTS**********&lt;br /&gt;Usage: scriptname &amp;lt;Range Value&amp;gt;&lt;br /&gt;               or                   &lt;br /&gt;Usage: scriptname &amp;lt;Start Range Value&amp;gt; &amp;lt;End Range Value&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;NOTE:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;this script can't find the highest prime number,but it can figure out the highest prime number your system is capable of calculating.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-9207553467180717093?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/9207553467180717093/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-produce-prime-numbers.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9207553467180717093'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/9207553467180717093'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-produce-prime-numbers.html' title='Shell Script To Produce Prime Numbers Within A 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>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1390579089985938776</id><published>2009-11-17T08:46:00.000-08:00</published><updated>2011-06-19T12:34:09.399-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Find Prime Number</title><content type='html'>&lt;pre  style="font-family:"Times New Roman";"&gt;&lt;span style="font-size:15px;"&gt;&lt;b&gt;Reposted on 18-06-2011 and Method 4 was deleted.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The simplest primality test is as follows: Given an input number n,&lt;br /&gt;check whether any integer m from 2 to n − 1 divides n. If n is divisi-&lt;br /&gt;ble by any m then n is composite, otherwise it is prime.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 1:&lt;/h4&gt;&lt;br /&gt;Now most people who write finding  prime number in shell script(Almost&lt;br /&gt;in all languages) may start with something simple, like:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: prime1.sh&lt;br /&gt;# USAGE : ./prime1.sh&lt;br /&gt;# PURPOSE: Finds whether given number is prime or not&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter a number: "&lt;br /&gt;read num&lt;br /&gt;i=2&lt;br /&gt;&lt;br /&gt;while [ $i -lt $num ]&lt;br /&gt;do&lt;br /&gt;  if [ `expr $num % $i` -eq 0 ]&lt;br /&gt;  then&lt;br /&gt;      echo "$num is not a prime number"&lt;br /&gt;      echo "Since it is divisible by $i"&lt;br /&gt;      exit&lt;br /&gt;  fi&lt;br /&gt;  i=`expr $i + 1`&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "$num is a prime number "&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 ]# ./prime1.sh&lt;br /&gt;Enter a number: 1879&lt;br /&gt;1879 is a prime number&lt;br /&gt;[root@venu ]# ./prime1.sh&lt;br /&gt;Enter a number: 119&lt;br /&gt;119 is not a prime number&lt;br /&gt;Since it is divisible by 7&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Using here string you can supply input non interactively.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;[root@venu ]# ./prime1.sh  &lt;&lt;&lt;2239&lt;br /&gt;Enter a number: 2239 is a prime number&lt;br /&gt;&lt;br /&gt; However, rather than testing all m up to n−1, it is only necessary to&lt;br /&gt;test m up to &lt;b&gt;sqrt n&lt;/b&gt;: if n is composite then it can be factored into&lt;br /&gt;two values,at least one of which must be less than or equal to &lt;b&gt;sqrt n&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt; Don't use method 1, it is very worst method, I hope that you will&lt;br /&gt;agree with me at the end of this post.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 2a:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: prime2a.sh&lt;br /&gt;# USAGE : ./prime2a.sh&lt;br /&gt;# PURPOSE: Finds whether given number is prime or not&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter a number: "&lt;br /&gt;read num&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       Integer Validation                          #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# If you are a  beginner you can skip this integer validation block.&lt;br /&gt;&lt;br /&gt;expr $num + 1  &amp;&amp;gt; /dev/null&lt;br /&gt;&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;[ $num -lt 2 ] &amp;&amp; echo "Values &amp;lt; 2 are not prime numbers" &amp;&amp; exit 1&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                Main Script Starts Here                            #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;i=2&lt;br /&gt;sqrtofnum=`echo "sqrt($num)"|bc`&lt;br /&gt;&lt;br /&gt;while [ $i -le $sqrtofnum ]&lt;br /&gt;do&lt;br /&gt;  if [ `expr $num % $i` -eq 0 ]&lt;br /&gt;  then&lt;br /&gt;      echo "$num is not a prime number"&lt;br /&gt;      echo "Since it is divisible by $i"&lt;br /&gt;      exit&lt;br /&gt;  fi&lt;br /&gt;  let i++&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "$num is a prime number "&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 ]# ./prime2a.sh &lt;&lt;&lt;169&lt;br /&gt;Enter a number: 169 is not a prime number&lt;br /&gt;Since it is divisible by 13&lt;br /&gt;[root@venu ]# ./prime2a.sh &lt;&lt;&lt;3181&lt;br /&gt;Enter a number: 3181 is a prime number&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Here is same method without using bc command:&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 2b:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: prime2b.sh&lt;br /&gt;# USAGE : ./prime2b.sh&lt;br /&gt;# PURPOSE: Finds whether given number is prime or not&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;echo -n "Enter a number: "&lt;br /&gt;read num&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                 Integer Validation                                #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# If you are a beginner you can skip integer validation block.&lt;br /&gt;&lt;br /&gt;expr $num + 1  &amp;&amp;gt; /dev/null&lt;br /&gt;&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;[ $num -lt 2 ] &amp;&amp; echo "Values &amp;lt; 2 are not prime numbers" &amp;&amp; exit 1&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                Main Script Starts Here                            #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;i=1&lt;br /&gt;newnum=$((num-1))&lt;br /&gt;&lt;br /&gt;while [ $((i*i)) -lt $newnum ]&lt;br /&gt;do&lt;br /&gt;  let i++&lt;br /&gt;  if [ `expr $num % $i` -eq 0 ]&lt;br /&gt;  then&lt;br /&gt;      echo "$num is not a prime number"&lt;br /&gt;      echo "Since it is divisible by $i"&lt;br /&gt;      exit&lt;br /&gt;  fi&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "$num is a prime number "&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 ]# ./prime2b.sh &lt;&lt;&lt;361&lt;br /&gt;Enter a number: 361 is not a prime number&lt;br /&gt;Since it is divisible by 19&lt;br /&gt;[root@venu ]# ./prime2b.sh &lt;&lt;&lt;3571&lt;br /&gt;Enter a number: 3571 is a prime number&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 3a:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: prime3a.sh &amp;lt; Number to check &amp;gt;&lt;br /&gt;# USAGE : ./prime3a.sh&lt;br /&gt;# PURPOSE: Finds whether a given number is prime or not.&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       ARGUMENTS CHECKING                          #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# If you are a beginner you can skip Arguments checking part.&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;    echo "Usage: scriptname &amp;lt;number to check&amp;gt;"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;expr $1 + 1  &amp;&amp;gt; /dev/null&lt;br /&gt;&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;#####################################################################&lt;br /&gt;#                   MAIN PROGRAM STARTS HERE                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;num=$1&lt;br /&gt;sqrtofnum=`echo "sqrt($num)" | bc `&lt;br /&gt;i=2&lt;br /&gt;&lt;br /&gt;while [ $i -le $sqrtofnum ]&lt;br /&gt;do&lt;br /&gt;  if [ $(((num/i)*i)) -eq $num ]&lt;br /&gt;  then&lt;br /&gt;      echo "$num is not a prime number"&lt;br /&gt;      echo "Since it is divisible by $i"&lt;br /&gt;      exit 0&lt;br /&gt;  fi&lt;br /&gt;&lt;br /&gt;  let i++           # you can also use i=`expr $i + 1`&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "$num is a prime number"&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 ]# ./prime3a.sh 529&lt;br /&gt;529 is not a prime number&lt;br /&gt;Since it is divisible by 23&lt;br /&gt;[root@venu ]# ./prime3a.sh 3571&lt;br /&gt;3571 is a prime number&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Here is same method without using bc command:&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Method 3b:&lt;/h4&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# SCRIPT: prime3b.sh&lt;br /&gt;# USAGE : ./prime3b.sh &amp;lt;Number to check &amp;gt;&lt;br /&gt;# PURPOSE: Finds whether a given number is prime or not.&lt;br /&gt;&lt;br /&gt;#####################################################################&lt;br /&gt;#                       ARGUMENTS CHECKING                          #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;# If you are a beginner you can skip Arguments checking part.&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;    echo "Usage: scriptname &amp;lt;number to check&amp;gt;"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;expr $1 + 1  &amp;&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;#####################################################################&lt;br /&gt;#                   MAIN PROGRAM STARTS HERE                        #&lt;br /&gt;#####################################################################&lt;br /&gt;&lt;br /&gt;num=$1&lt;br /&gt;newnum=$((num-1))&lt;br /&gt;i=1&lt;br /&gt;&lt;br /&gt;while [ $((i*i)) -lt $newnum ]&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;  let i++           # you can also use i=`expr $i + 1`&lt;br /&gt;&lt;br /&gt;  if [ $(((num/i)*i)) -eq $num ]&lt;br /&gt;  then&lt;br /&gt;      echo "$num is not a prime number"&lt;br /&gt;      echo "Since it is divisible by $i"&lt;br /&gt;      exit 0&lt;br /&gt;  fi&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "$num is a prime number"&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 ]# ./prime3b.sh 12009&lt;br /&gt;12009 is not a prime number&lt;br /&gt;Since it is divisible by 3&lt;br /&gt;[root@venu ]# ./prime3b.sh 16127&lt;br /&gt;16127 is a prime number&lt;br /&gt;&lt;br /&gt;Now using &lt;b&gt;time&lt;/b&gt; command to test which script runs fast.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;OUTPUT 1:&lt;/span&gt;&lt;br /&gt;[root@venu ]# time sh prime1.sh &lt;&lt;&lt;99991&lt;br /&gt;Enter a number: 99991 is a prime number&lt;br /&gt;&lt;br /&gt;real    5m6.590s&lt;br /&gt;user    1m13.882s&lt;br /&gt;sys     3m29.807s&lt;br /&gt;[root@venu ]# time sh prime2a.sh &lt;&lt;&lt;99991&lt;br /&gt;Enter a number: 99991 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.558s&lt;br /&gt;user    0m0.159s&lt;br /&gt;sys     0m0.409s&lt;br /&gt;[root@venu ]# time sh prime2b.sh &lt;&lt;&lt;99991&lt;br /&gt;Enter a number: 99991 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.534s&lt;br /&gt;user    0m0.141s&lt;br /&gt;sys     0m0.405s&lt;br /&gt;[root@venu ]# time sh prime3a.sh 99991&lt;br /&gt;99991 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.037s&lt;br /&gt;user    0m0.034s&lt;br /&gt;sys     0m0.003s&lt;br /&gt;[root@venu ]# time sh prime3b.sh 99991&lt;br /&gt;99991 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.041s&lt;br /&gt;user    0m0.034s&lt;br /&gt;sys     0m0.007s&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;OUTPUT 2:&lt;/span&gt;&lt;br /&gt;[root@venu ]# time sh prime1.sh &lt;&lt;&lt;319993&lt;br /&gt;Enter a number: 319993 is a prime number&lt;br /&gt;&lt;br /&gt;real    16m44.993s&lt;br /&gt;user    4m3.943s&lt;br /&gt;sys     12m17.365s&lt;br /&gt;[root@venu ]# time sh prime2a.sh &lt;&lt;&lt;319993&lt;br /&gt;Enter a number: 319993 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.883s&lt;br /&gt;user    0m0.218s&lt;br /&gt;sys     0m0.529s&lt;br /&gt;[root@venu ]# time sh prime2b.sh &lt;&lt;&lt;319993&lt;br /&gt;Enter a number: 319993 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.739s&lt;br /&gt;user    0m0.238s&lt;br /&gt;sys     0m0.494s&lt;br /&gt;[root@venu ]# time sh prime3a.sh 319993&lt;br /&gt;319993 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.072s&lt;br /&gt;user    0m0.054s&lt;br /&gt;sys     0m0.007s&lt;br /&gt;[root@venu ]# time sh prime3b.sh 319993&lt;br /&gt;319993 is a prime number&lt;br /&gt;&lt;br /&gt;real    0m0.089s&lt;br /&gt;user    0m0.061s&lt;br /&gt;sys     0m0.008s&lt;br /&gt;&lt;br /&gt;I hope, now you understood, Just one line of code makes the difference.&lt;br /&gt;Method 1 takes more and more time to find prime number.&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-1390579089985938776?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1390579089985938776/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-find-prime-number.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1390579089985938776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1390579089985938776'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-find-prime-number.html' title='Shell Script to Find Prime 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>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-2019909514968011396</id><published>2009-11-15T01:19:00.000-08:00</published><updated>2011-01-15T06:09:56.645-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>How to Pass Arguments to 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;Like UNIX commands, shell scripts also accept arguments from the command line.&lt;br /&gt;They can, therefore, run non interactively and be used with redirection and&lt;br /&gt;pipelines.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:22px; font-weight: bold;"&gt;Positional Parameters:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Arguments are passed from the command line into a shell program using the&lt;br /&gt;positional parameters $1 through to $9. Each parameter corresponds to the&lt;br /&gt;position of the argument on the command line.&lt;br /&gt;&lt;br /&gt;The first argument is read by the shell into the parameter $1, The second&lt;br /&gt;argument into $2, and so on. After $9, the arguments must be enclosed in&lt;br /&gt;brackets, for example, ${10}, ${11}, ${12}.Some shells doesn't support this&lt;br /&gt;method. In that case, to refer to parameters with numbers greater than 9, use&lt;br /&gt;the shift command; this shifts the parameter list to the left. $1 is lost,while&lt;br /&gt;$2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter&lt;br /&gt;becomes $9 and can then be referred to.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Example:&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# Call this script with at least 3 parameters, for example&lt;br /&gt;# sh scriptname 1 2 3&lt;br /&gt;&lt;br /&gt;echo "first parameter is $1"&lt;br /&gt;&lt;br /&gt;echo "Second parameter is $2"&lt;br /&gt;&lt;br /&gt;echo "Third parameter is $3"&lt;br /&gt;&lt;br /&gt;exit 0&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;[root@localhost ~]# sh parameters.sh 47 9 34&lt;br /&gt;&lt;br /&gt;first parameter is 47&lt;br /&gt;&lt;br /&gt;Second parameter is 9&lt;br /&gt;&lt;br /&gt;Third parameter is 34&lt;br /&gt;&lt;br /&gt;[root@localhost ~]# sh parameters.sh 4 8 3&lt;br /&gt;&lt;br /&gt;first parameter is 4&lt;br /&gt;&lt;br /&gt;Second parameter is 8&lt;br /&gt;&lt;br /&gt;Third parameter is 3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; In addition to these positional parameters, there are a few other special&lt;br /&gt;parameters used by shell.Their significance is noted bellow.&lt;br /&gt;&lt;br /&gt;$* - It stores the complete set of positional parameters as a single string.&lt;br /&gt;$@ - Same as $*, But there is subtle difference when enclosed in double quotes.&lt;br /&gt;$# - It is set to the number of arguments specified.This lets you design scripts&lt;br /&gt;     that check whether the right number of arguments have been entered.&lt;br /&gt;$0 - Refers to the name of the script itself.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:22px; font-weight: bold;"&gt;Setting Values of Positional Parameters&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You can't technically call positional parameters as shell variables because all&lt;br /&gt;variable names start with a letter. For instance you can't assign values to $1,&lt;br /&gt;$2.. etc. $1=100 or $2=venu is simply not done. There is one more way to assign&lt;br /&gt;values to the positional parameters, using the set command.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ set Helping hands are holier than praying lips&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The above command sets the value $1 with “Helping” , $2 with “hands” and so on.&lt;br /&gt;To verify, use echo statement to display their values.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ echo $1 $2 $3 $4 $5 $6 $7&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Helping hands are holier than praying lips&lt;br /&gt;&lt;br /&gt;You can simply use $* or $@&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ echo $*&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Helping hands are holier than praying lips&lt;br /&gt;&lt;br /&gt;&lt;b&gt;$ echo $@&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Helping hands are holier than praying lips&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:22px; font-weight: bold;"&gt;Using Shift on Positional Parameters&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We have used set command to set upto 9 words. But we can use it for more.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ set A friend who helps when one is in trouble is a real friend&lt;br /&gt;&lt;br /&gt;$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11&lt;br /&gt;&lt;br /&gt;A friend who helps when one is in trouble A0 A1&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Observe that last two words in the output. These occurred in the output because&lt;br /&gt;at a time we can access only 9 positional parameters. When we tried to refer to&lt;br /&gt;$10 it was interpreted by the shell as if you wanted to out put the value of $1&lt;br /&gt;and a 0. Hence we got A0 in the output.&lt;br /&gt;Does that mean the words following the ninth word have been lost? No. If not,&lt;br /&gt;then where have they gone?. They are very much there, safe with the shell But to&lt;br /&gt;reach them we must do the following.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$shift 4&lt;br /&gt;&lt;br /&gt;$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9&lt;br /&gt;&lt;br /&gt;when one is in trouble is a real friend&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now where have the first seven words gone? They have been shifted out.The first&lt;br /&gt;four words lost for ever, as we did not take the precaution to store them else-&lt;br /&gt;where. What should we have done is:&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ set A friend who helps when one is in trouble is a real friend&lt;br /&gt;&lt;br /&gt;$ a=$1&lt;br /&gt;&lt;br /&gt;$ b=$2&lt;br /&gt;&lt;br /&gt;$ c=$3&lt;br /&gt;&lt;br /&gt;$ d=$4&lt;br /&gt;&lt;br /&gt;$ shift 4&lt;br /&gt;&lt;br /&gt;$ echo $a $b $c $d $1 $2 $3 $4 $5 $6 $7 $8 $9&lt;br /&gt;&lt;br /&gt;A friend who helps when one is in trouble is a real friend&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="noteblock"&gt;&lt;b&gt;Note:&lt;/b&gt;In the Korn  and bash shells you can refer directly to arguments where&lt;br /&gt;n is greater than 9 using braces. For example, to refer to the 57th positional&lt;br /&gt;parameter, use the notation ${57}.some shells may not support this method.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ set A friend who helps when one is in trouble is a real friend&lt;br /&gt;&lt;br /&gt;$ echo ${12}&lt;br /&gt;&lt;br /&gt;real&lt;br /&gt;&lt;br /&gt;$ echo ${13}&lt;br /&gt;&lt;br /&gt;friend&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;   Bracket notation for positional parameters leads to a fairly simple way of&lt;br /&gt;referencing the last argument passed to a script on the command line.&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;$ echo ${!#}&lt;br /&gt;&lt;br /&gt;friend&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:22px; font-weight: bold;"&gt;$* and $@&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let us look at the subtle difference between $* and $@.&lt;br /&gt;&lt;br /&gt;First create three files  fileA, fileB, “file temp”&lt;br /&gt;cat &gt; fileA&lt;br /&gt;I LOVE INDIA&lt;br /&gt;Ctrl+d&lt;br /&gt;&lt;br /&gt;cat &gt; fileB&lt;br /&gt;HELLO WORLD&lt;br /&gt;Ctrl+d&lt;br /&gt;&lt;br /&gt;cat &gt; temp\ file&lt;br /&gt;This file name contains blank space&lt;br /&gt;Ctrl+d&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Example: &lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# Usage: sh arguments.sh fileA fileB temp\ file&lt;br /&gt;&lt;br /&gt;echo -e "\033[1mDisplay files content using \$* \033[0m"&lt;br /&gt;&lt;br /&gt;cat $*&lt;br /&gt;&lt;br /&gt;echo -e "\033[1mDisplay files content using \$@ \033[0m"&lt;br /&gt;&lt;br /&gt;cat $@&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Run the script with file names as arguments.&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;[root@localhost ~]# sh arguments.sh fileA fileB temp\ file&lt;br /&gt;&lt;b&gt;Display files content using $* &lt;/b&gt;&lt;br /&gt;I LOVE INDIA&lt;br /&gt;HELLO WORLD&lt;br /&gt;cat: temp: No such file or directory&lt;br /&gt;cat: file: No such file or directory&lt;br /&gt;&lt;b&gt;Display files content  using $@ &lt;/b&gt;&lt;br /&gt;I LOVE INDIA&lt;br /&gt;HELLO WORLD&lt;br /&gt;cat: temp: No such file or directory&lt;br /&gt;cat: file: No such file or directory&lt;br /&gt;&lt;br /&gt;&lt;b&gt;So there is no difference between cat $* and cat $@.&lt;br /&gt;&lt;br /&gt;Modify arguments.sh script as&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;#!/bin/bash&lt;br /&gt;# Usage: sh arguments.sh fileA fileB temp\ file&lt;br /&gt;&lt;br /&gt;echo -e "\033[1mDisplay files content using \"\$*\" \033[0m"&lt;br /&gt;&lt;br /&gt;cat "$*"&lt;br /&gt;&lt;br /&gt;echo -e "\033[1mDisplay files content  using \"\$@\" \033[0m"&lt;br /&gt;&lt;br /&gt;cat "$@"&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now again run the script with file names as arguments.&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;[root@localhost ~]# sh arguments.sh fileA fileB temp\ file &lt;br /&gt;&lt;b&gt;Display files content using "$*" &lt;/b&gt;&lt;br /&gt;cat: fileA fileB temp file: No such file or directory&lt;br /&gt;&lt;b&gt;Display files content  using "$@" &lt;/b&gt;&lt;br /&gt;I LOVE INDIA&lt;br /&gt;HELLO WORLD&lt;br /&gt;This file name contain blank space&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Now there is a difference, the two cat commands would become:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;   cat “fileA fileB temp file”&lt;br /&gt;   cat fileA fileB 'temp file'&lt;br /&gt;&lt;br /&gt; On execution, the first of these commands would give an error since there does&lt;br /&gt;not exist a file with the name “fileA fileB temp file”. As against this, the&lt;br /&gt;second cat command would display the contents of the files fileA, fileB and&lt;br /&gt;“temp file”. So what you observed ,when not enclosed within "double quotes"&lt;br /&gt;$* and $@ behaves exactly similarly, and when enclosed in "double quotes" there&lt;br /&gt;is a subtle difference.&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-2019909514968011396?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2019909514968011396/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html#comment-form' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2019909514968011396'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2019909514968011396'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html' title='How to Pass Arguments to 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>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-8763767404903907627</id><published>2009-11-12T01:17:00.000-08:00</published><updated>2009-11-15T01:22:26.999-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Check Whether Given Year is a Leap Year or Not?</title><content type='html'>&lt;pre  style="font-family:"Times New Roman";"&gt;&lt;span style="font-weight:500"&gt;&lt;span style="font-size:14px;"&gt;&lt;b&gt;&lt;h1&gt;Method 1:&lt;/h1&gt;&lt;/b&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;#  SCRIPT : leapyear.sh&lt;br /&gt;#  PURPOSE: Checks whether given year is a leap year or not&lt;br /&gt;#  USAGE  : sh leapyear.sh  &lt;Year value to check&gt;&lt;br /&gt;#           If Year value not supplied, it takes current year as default&lt;br /&gt;#&lt;br /&gt;# This script based on, One year has the length of 365 days, 5 hours,&lt;br /&gt;# 48 minutes and 47 seconds. Because this is rather non-functional,&lt;br /&gt;# a normal year has been given 365 days and a leap year 366 days.&lt;br /&gt;&lt;br /&gt;#####################################################&lt;br /&gt;############### DEFINE FUNCTIONS HERE ####################&lt;br /&gt;#####################################################&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;Leap()&lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;#  This function tells you if the argument is a leap year or not...&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;YEAR=$1&lt;br /&gt;NO_OF_DAYS=$(cal $YEAR |egrep "^[ 0-9][0-9]| [ 0-9][0-9]$" |wc -w)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;# A single parameter to cal specifies the year (1 - 9999) to be displayed;&lt;br /&gt;# note the year must be fully specified: “cal 89” will not display a calendar&lt;br /&gt;# for 1989.&lt;br /&gt;&lt;br /&gt;# Use egrep or grep -E both are same&lt;br /&gt;&lt;br /&gt;# cal command pipes total months and dates to egrep, egrep remove all lines&lt;br /&gt;# not starting with number or ending with number, then pipes remaining lines&lt;br /&gt;# to wc command. wc command counts total number of words(days).&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;if [[ $NO_OF_DAYS -eq 365 ]]&lt;br /&gt;then&lt;br /&gt;       echo "$YEAR Is Not A Leap Year "&lt;br /&gt;else&lt;br /&gt;       echo "$YEAR Is A Leap Year"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;#####################################################&lt;br /&gt;############### MAIN PROGRAM STARTS HERE #################&lt;br /&gt;#####################################################&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;Year=${1:-$(date +%Y)}&lt;br /&gt;if [ $Year -lt 1 -o $Year -gt 9999 ]&lt;br /&gt;then&lt;br /&gt;    echo "Illegal Year Value: Use 1-9999"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;Leap $Year&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="font-family:"Times New Roman",Georgia,Serif;"&gt;&lt;span style="font-size:14px;"&gt;&lt;b&gt;&lt;h1&gt;Method 2:&lt;/h1&gt;&lt;/b&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;#  SCRIPT : leapyear.sh&lt;br /&gt;#  PURPOSE: Checks whether given year is a leap year or not&lt;br /&gt;#  USAGE  : sh leapyear.sh  &lt;Year value to check&gt;&lt;br /&gt;#           If Year value not supplied, it takes current year as default&lt;br /&gt;#&lt;br /&gt;#  This script based on,A leap year comes every 4 years,&lt;br /&gt;#  but not every 100 years, then again every 400 years.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#####################################################&lt;br /&gt;############### ARGUMENTS CHECKING #####################&lt;br /&gt;#####################################################&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;YEAR=${1:-`date +%Y`}&lt;br /&gt;&lt;br /&gt;if [ $YEAR -lt 1 -o $YEAR -gt 9999 ]&lt;br /&gt;then&lt;br /&gt;     echo "Illegal Year Value: Use 1-9999"&lt;br /&gt;     exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;#####################################################&lt;br /&gt;############### DECLARE VARIABLES HERE ###################&lt;br /&gt;#####################################################&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;rem1=$((YEAR%4))&lt;br /&gt;rem2=$((YEAR%100))&lt;br /&gt;rem3=$((YEAR%400))&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;#####################################################&lt;br /&gt;############## MAIN PROGRAM STARTS HERE ##################&lt;br /&gt;#####################################################&lt;br /&gt;&lt;span style="font-weight:bold"&gt;&lt;br /&gt;if [ ${rem3} = "0" ]&lt;br /&gt;then&lt;br /&gt;       echo "$YEAR Is A Leap Year"&lt;br /&gt;       exit&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ ${rem2} = "0" -a ${rem3} != "0" ]&lt;br /&gt;then&lt;br /&gt;       echo "$YEAR Is Not A Leap Year"&lt;br /&gt;       exit&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ ${rem1} = "0" -a ${rem2} != "0" ]&lt;br /&gt;then&lt;br /&gt;       echo "$YEAR Is A Leap Year"&lt;br /&gt;else&lt;br /&gt;       echo "$YEAR Is Not A Leap Year"&lt;br /&gt;fi&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&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@localhost shell]# ./leapyear.sh &lt;br /&gt;2009 Is Not A Leap Year&lt;br /&gt;[root@localhost shell]# ./leapyear.sh 2100&lt;br /&gt;2100 Is Not A Leap Year&lt;br /&gt;[root@localhost shell]# ./leapyear.sh 2104&lt;br /&gt;2104 Is A Leap Year&lt;br /&gt;[root@localhost shell]# ./leapyear.sh 21000&lt;br /&gt;Illegal Year Value: Use 1-9999&lt;br /&gt;[root@localhost shell]# ./leapyear.sh 1996&lt;br /&gt;1996 Is A Leap Year&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-8763767404903907627?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/8763767404903907627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-check-whether-given.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8763767404903907627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8763767404903907627'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-check-whether-given.html' title='Shell Script to Check Whether Given Year is a Leap Year 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-851155064432954563</id><published>2009-11-02T04:21:00.000-08:00</published><updated>2009-11-02T05:38:48.846-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Copy Progress Bar Shell Script</title><content type='html'>This script not replaces &lt;span style="font-style:italic;"&gt;cp&lt;span style="font-weight:bold;"&gt;&lt;/span&gt;&lt;/span&gt; command. This is a sample script for practice purpose.&lt;br /&gt;I am going to develop more elegant script, which fulfil most of the &lt;span style="font-style:italic;"&gt;cp&lt;span style="font-weight:bold;"&gt;&lt;/span&gt;&lt;/span&gt; command strengths.&lt;br /&gt;I will post complete version of the script as soon as possible,&lt;br /&gt;I shall really appreciate all comments and suggestions to improve this script.&lt;br /&gt;&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;# SCRIPT:  ddcopy_progress.sh&lt;br /&gt;# PURPOSE: This script is used to watch progress of copying.&lt;br /&gt;#&lt;br /&gt;# Note:   I am using destination file as $$.temp .This is because,&lt;br /&gt;#         after testing you can easily delete temp files using&lt;br /&gt;#         rm -f *.temp&lt;br /&gt;&lt;br /&gt;#############################################&lt;br /&gt;#&lt;span style="color: rgb(204, 102, 204);"&gt;                    Arguments Checking  &lt;/span&gt;                                                 #&lt;br /&gt;#############################################&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]            # Look for exactly one parameter&lt;br /&gt;then&lt;br /&gt;    echo "..............Usage Error........."&lt;br /&gt;    echo  "Usage: $0 Sourcefil"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ ! -e $1 ]             # Check source file exist or not&lt;br /&gt;then&lt;br /&gt;    echo "File $1 not exist"&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;#############################################&lt;br /&gt;#  &lt;span style="color: rgb(204, 102, 204);"&gt;              DEFINE VARIABLES HERE &lt;/span&gt;                                               #&lt;br /&gt;#############################################&lt;br /&gt;&lt;br /&gt;Seek=0                      # Skip BLOCKS bs-sized blocks at start of output&lt;br /&gt;Skip=0                      # Skip BLOCKS bs-sized blocks at start of input&lt;br /&gt;Bsize=128000                # Block size&lt;br /&gt;size=`stat -c %s $1`&lt;br /&gt;blocks=$((size/Bsize))      # Total blocks of input file&lt;br /&gt;&lt;br /&gt;lastblock=$((size%Bsize))   # Last block,  which size  &lt; $Bsize&lt;br /&gt;&lt;br /&gt;# If last block size is &gt; 0 then add 1 block to existing blocks.&lt;br /&gt;&lt;br /&gt;if [ $lastblock -gt 0 ]&lt;br /&gt;then&lt;br /&gt;    let blocks++&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# I am dividing screen width 60 as 20 parts.Each part is filled with color&lt;br /&gt;# after each iteration.&lt;br /&gt;&lt;br /&gt;# Make blocks count dividable by 20.&lt;br /&gt;&lt;br /&gt;addblocks=$((blocks%20))&lt;br /&gt;if [ $addblocks -gt 0 ]&lt;br /&gt;then&lt;br /&gt;   adjustblocks=$((20-addblocks))&lt;br /&gt;   blocks=$((blocks+adjustblocks))&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;Count=$((blocks/20))&lt;br /&gt;&lt;br /&gt;# Count variable contain number of blocks to be copied for each iteration&lt;br /&gt;&lt;br /&gt;###############################################&lt;br /&gt;#  &lt;span style="color: rgb(204, 102, 204);"&gt;            MAIN PROGRAM STARTS HERE &lt;/span&gt;                                             #&lt;br /&gt;###############################################&lt;br /&gt;&lt;br /&gt;printf "\e[40m\e[43m%60s\n\e[A" " "&lt;br /&gt;&lt;br /&gt;for ((i=1;i&lt;=20;i++))&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;   dd if=$1 of=$$.temp bs=128kB seek=$Seek skip=$Skip count=$Count 2&gt;/dev/null&lt;br /&gt;   Skip=$((Skip+$Count))&lt;br /&gt;   Seek=$((Seek+$Count))&lt;br /&gt;   j=$((j+3))                       # 60/20 each part is 3 chars length&lt;br /&gt;   printf "\e[40m\e[7m%${j}s\n\e[A" " "&lt;br /&gt;#  echo -e "\033[7m   \c"&lt;br /&gt;#  sleep 1                          # Uncomment it for small files&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;printf "\e[0m\n"&lt;br /&gt;#echo -e "\033[0m"&lt;br /&gt;&lt;/span&gt;&lt;/pre&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;Screen Shot1:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Su7YdLfC0EI/AAAAAAAAAqw/T7zjnaWJex0/s1600-h/progress1.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 76px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Su7YdLfC0EI/AAAAAAAAAqw/T7zjnaWJex0/s400/progress1.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5399490999074607170" /&gt;&lt;/a&gt;&lt;br /&gt;Screen Shot2:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/Su7Ym9G1zLI/AAAAAAAAAq4/GoiDVCiKwZk/s1600-h/progress2.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 76px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/Su7Ym9G1zLI/AAAAAAAAAq4/GoiDVCiKwZk/s400/progress2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5399491167013686450" /&gt;&lt;/a&gt;&lt;br /&gt;Screen Shot3:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Su7Y0737x9I/AAAAAAAAArA/uCGQ5yjuy6c/s1600-h/progress3.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 76px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/Su7Y0737x9I/AAAAAAAAArA/uCGQ5yjuy6c/s400/progress3.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5399491407200896978" /&gt;&lt;/a&gt;&lt;br /&gt;Screen Shot4:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/Su7ZAo5eU8I/AAAAAAAAArI/0tXmQ2sZdKA/s1600-h/progress4.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 76px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/Su7ZAo5eU8I/AAAAAAAAArI/0tXmQ2sZdKA/s400/progress4.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5399491608265511874" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Observation:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;I tested above script with different block count.But I didn't get accurate result.&lt;br /&gt;Some times less count script given good result, some times more block count given&lt;br /&gt;good result.&lt;br /&gt;I copied 1.8 GB Movie file with this script.With time command it has give bellow &lt;br /&gt;result.&lt;br /&gt;&lt;br /&gt;real    1m12.646s&lt;br /&gt;user    0m0.043s&lt;br /&gt;sys     0m7.854s&lt;br /&gt;&lt;br /&gt;From my observation this script has taken less time compared with mouse copy and&lt;br /&gt;paste method. Copy and paste has taken 1m14.02s averagely.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-851155064432954563?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/851155064432954563/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/copy-progress-bar.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/851155064432954563'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/851155064432954563'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/copy-progress-bar.html' title='Copy Progress Bar 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/Su7YdLfC0EI/AAAAAAAAAqw/T7zjnaWJex0/s72-c/progress1.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-5556557881417980608</id><published>2009-11-01T11:56:00.000-08:00</published><updated>2010-05-14T08:07:38.947-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Create a File with Specified Number of Lines</title><content type='html'>&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;# SCRIPT:  bigfile.sh&lt;br /&gt;# PURPOSE: This script is used to create a text file that&lt;br /&gt;#          has a specified number of lines that is specified&lt;br /&gt;#          on the command line.&lt;br /&gt;#&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;usage()&lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;echo "............USAGE ERROR............"&lt;br /&gt;echo "USAGE: $0 &amp;lt;number_of_lines_to_create&amp;gt;"&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Argument Checking&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]             &lt;span style="color: rgb(204, 102, 204);"&gt;# Looking for exactly one parameter&lt;/span&gt;&lt;br /&gt;then&lt;br /&gt;     usage                 &lt;span style="color: rgb(204, 102, 204);"&gt;   # Usage error was made&lt;/span&gt;&lt;br /&gt;     exit 1                 &lt;span style="color: rgb(204, 102, 204);"&gt;   # Exit on a usage error&lt;/span&gt;&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Define files and variables here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;LINE_LENGTH=80              &lt;span style="color: rgb(204, 102, 204);"&gt;# Number of characters per line&lt;/span&gt;&lt;br /&gt;OUT_FILE=bigfile.$$         &lt;span style="color: rgb(204, 102, 204);"&gt; # New file to create&lt;/span&gt;&lt;br /&gt;&gt;$OUT_FILE                  &lt;span style="color: rgb(204, 102, 204);"&gt;     # Initialize to a zero-sized file&lt;/span&gt;&lt;br /&gt;TOTAL_LINES=$1              &lt;span style="color: rgb(204, 102, 204);"&gt; # Total number of lines to create&lt;/span&gt;&lt;br /&gt;LINE_COUNT=0                &lt;span style="color: rgb(204, 102, 204);"&gt; # Character counter&lt;/span&gt;&lt;br /&gt;CHAR=X                      &lt;span style="color: rgb(204, 102, 204);"&gt;       # Character to write to the file&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#   Beginning of Main&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;while ((LINE_COUNT &lt; TOTAL_LINES))          &lt;br /&gt;do&lt;br /&gt;     CHAR_COUNT=0        &lt;span style="color: rgb(204, 102, 204);"&gt;# Initialize the CHAR_COUNT to zero on every new line&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     while ((CHAR_COUNT &lt; LINE_LENGTH))     &lt;span style="color: rgb(204, 102, 204);"&gt;# Each line is fixed length&lt;/span&gt;&lt;br /&gt;     do&lt;br /&gt;          echo -e "$CHAR\c" &gt;&gt; $OUT_FILE    &lt;span style="color: rgb(204, 102, 204);"&gt;  # Echo a single character&lt;/span&gt;&lt;br /&gt;          let CHAR_COUNT++                  &lt;span style="color: rgb(204, 102, 204);"&gt;        # Increment the character counter&lt;/span&gt;&lt;br /&gt;     done&lt;br /&gt;&lt;br /&gt;     let LINE_COUNT++                       &lt;br /&gt;     echo&gt;&gt;$OUT_FILE                        &lt;span style="color: rgb(204, 102, 204);"&gt;            # Give a newline character&lt;/span&gt;&lt;br /&gt;done&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-5556557881417980608?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/5556557881417980608/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-create-file-with.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5556557881417980608'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5556557881417980608'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/11/shell-script-to-create-file-with.html' title='Shell Script to Create a File with Specified Number of Lines'/><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-2220671447705895436</id><published>2009-10-27T08:41:00.000-07:00</published><updated>2009-10-27T10:01:50.994-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script to Find Factorial of a  Number</title><content type='html'>&lt;pre style="font-family:"Times New Roman",Georgia,Serif;"&gt;&lt;span style="font-size:120%;"&gt;&lt;b&gt;&lt;h1&gt;Method 1:&lt;/h1&gt;&lt;/b&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# fact1&lt;br /&gt;# Finding factorial of a given number&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;echo "Enter a number"&lt;br /&gt;read num&lt;br /&gt;fact=1&lt;br /&gt;n=$num&lt;br /&gt;while [ $num -ge 1 ]&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;  fact=`echo $fact \* $num|bc`&lt;br /&gt;  &lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;  # You can use bellow commands also &lt;br /&gt;  # fact=$((fact*num))&lt;br /&gt;  # fact=`expr $fact \* $num`&lt;br /&gt;  # But maximum integer value that bash can handle is &lt;br /&gt;  # 9223372036854775807.&lt;br /&gt;  # An  operation  that takes a variable  outside these &lt;br /&gt;  # limits  will give an erroneous result. To solve this&lt;br /&gt;  # problem store numbers as strings and use bc for math.&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  let num--&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;echo "factorial of $n is $fact"&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;h1&gt;Method 2:&lt;/h1&gt;&lt;/b&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# fact2&lt;br /&gt;# Usage : sh fact2 Number&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;factorial ()  &lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;local number=$1&lt;br /&gt;if [ $number -eq 0 ]&lt;br /&gt;then&lt;br /&gt;    Factorial=1&lt;br /&gt;else&lt;br /&gt;    let "next = number - 1"&lt;br /&gt;    factorial $next&lt;br /&gt;    Factorial=`echo $number \* $Factorial | bc`&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#   let "Factorial = $number * $Factorial"&lt;/span&gt;&lt;br /&gt;fi&lt;br /&gt;return $Factorial 2&gt;/dev/null&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;# Bash function returns integer value only.&lt;br /&gt;# But maximum integer value that bash can handle is &lt;br /&gt;# 9223372036854775807.&lt;br /&gt;# An operation that takes a variable outside these limits &lt;br /&gt;# will give an erroneous result. That's why I redirected &lt;br /&gt;# stderror output to /dev/null&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Main program starts here.&lt;/span&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;    echo "Invalid Arguments"&lt;br /&gt;    echo "Usage: ./fact2.sh  Number "&lt;br /&gt;    exit 1&lt;br /&gt;fi&lt;br /&gt;factorial $1&lt;br /&gt;echo "Factorial of $1 is $Factorial"&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;[root@localhost shell]# ./fact1&lt;br /&gt;Enter a number&lt;br /&gt;8 &lt;br /&gt;factorial of 8 is 40320&lt;br /&gt;[root@localhost shell]# ./fact1&lt;br /&gt;Enter a number&lt;br /&gt;23&lt;br /&gt;factorial of 23 is 25852016738884976640000&lt;br /&gt;&lt;br /&gt;[root@localhost shell]# ./fact2 &lt;br /&gt;Invalid Arguments&lt;br /&gt;Usage: ./fact2.sh  Number &lt;br /&gt;[root@localhost shell]# ./fact2 7&lt;br /&gt;Factorial of 7 is 5040&lt;br /&gt;[root@localhost shell]# ./fact2 33&lt;br /&gt;Factorial of 33 is 8683317618811886495518194401280000000&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-2220671447705895436?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2220671447705895436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-find-factorial-of.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2220671447705895436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2220671447705895436'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-find-factorial-of.html' title='Shell Script to Find Factorial 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>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-4269091281773963645</id><published>2009-10-26T09:50:00.000-07:00</published><updated>2009-10-26T10:05:41.151-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>For Loop Example-2</title><content type='html'>&lt;pre style="font-family:"Times New Roman",Georgia,Serif;"&gt;&lt;span style="font-size:120%;"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# Usage: forloop2.sh     or  forloop2.sh number&lt;br /&gt;# Example using for loop&lt;br /&gt;# Prints following format&lt;br /&gt;#     *&lt;br /&gt;#    * *&lt;br /&gt;#   * * *&lt;br /&gt;#    * *&lt;br /&gt;#     *&lt;br /&gt;&lt;br /&gt;n=${1-10}    # If command line argument not supplied default is 10&lt;br /&gt;t=`expr $((2*$n - 1 ))`&lt;br /&gt;a=$n&lt;br /&gt;b=$(($a+1))&lt;br /&gt;for (( i=1 ; i&lt;=$n ;i++ ))&lt;br /&gt;do&lt;br /&gt;  echo -e -n "\033[47m"&lt;br /&gt;  a=$(($b-1))&lt;br /&gt;  b=$a&lt;br /&gt;  k=1&lt;br /&gt;for (( j=1 ; j&lt;=$t ; j++ ))&lt;br /&gt;  do&lt;br /&gt;      if [ $j -eq $a ]&lt;br /&gt;      then&lt;br /&gt;         echo -e "\033[43m * \033[47m\c"&lt;br /&gt;         if [ $k -lt $i ]&lt;br /&gt;         then&lt;br /&gt;            a=$((a+2))&lt;br /&gt;            let k++&lt;br /&gt;         fi&lt;br /&gt;      else&lt;br /&gt;         echo -n "   "&lt;br /&gt;     fi&lt;br /&gt;  done&lt;br /&gt;  echo  ""&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;n=$(($n-1))&lt;br /&gt;a=1&lt;br /&gt;b=$a&lt;br /&gt;for (( i=$n ; i&gt;=1 ;i-- ))&lt;br /&gt;do&lt;br /&gt;  echo -e -n "\033[47m"&lt;br /&gt;  a=$(($b+1))&lt;br /&gt;  b=$a&lt;br /&gt;  k=1&lt;br /&gt;  for (( j=1 ; j&lt;=$t ; j++ ))&lt;br /&gt;  do&lt;br /&gt;      if [ $j -eq $a ]&lt;br /&gt;      then&lt;br /&gt;         echo -e "\033[43m * \033[47m\c"&lt;br /&gt;         if [ $k -lt $i ]&lt;br /&gt;         then&lt;br /&gt;            a=$((a+2))&lt;br /&gt;            let k++&lt;br /&gt;         fi&lt;br /&gt;      else&lt;br /&gt;         echo -n "   "&lt;br /&gt;     fi&lt;br /&gt;  done&lt;br /&gt;  echo  ""&lt;br /&gt;done&lt;br /&gt;echo -e "\033[0m"&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SuXV1hDw3TI/AAAAAAAAAqo/e7ODr_t7H3c/s1600-h/forloop2.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 376px; height: 299px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SuXV1hDw3TI/AAAAAAAAAqo/e7ODr_t7H3c/s400/forloop2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5396954843857345842" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-4269091281773963645?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/4269091281773963645/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/for-loop-example-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4269091281773963645'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4269091281773963645'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/for-loop-example-2.html' title='For Loop Example-2'/><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://2.bp.blogspot.com/_ioCu0w5ZnYE/SuXV1hDw3TI/AAAAAAAAAqo/e7ODr_t7H3c/s72-c/forloop2.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6596279919133437795</id><published>2009-10-10T09:55:00.000-07:00</published><updated>2010-08-20T08:25:46.412-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>How to Execute a Shell Script</title><content type='html'>&lt;pre  style="font-family:"Times New Roman",Georgia,Serif;"&gt;&lt;span style="font-size:120%;"&gt;&lt;b&gt;&lt;span style="font-size:155%;"&gt;Defining the Shell Type&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To make a bash script crate a new file with a starting line like:&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash &lt;/span&gt;&lt;br /&gt;       It is important that the path to the bash is proper and &lt;br /&gt;first two  characters  must be “#!” . The shell from which you &lt;br /&gt;are starting the script will find this line and hand the whole&lt;br /&gt;script  over to bash. Without this  line the  script  would be &lt;br /&gt;interpreted  by the same  type of shell as the one, from which&lt;br /&gt;it was started.But since the syntax is different for all shells, &lt;br /&gt;it is necessary to define the shell with that line.&lt;br /&gt;Some typical interpreters for shebang lines:&lt;br /&gt;&lt;br /&gt;• #!/bin/bash — Execute using the Bourn-again shell&lt;br /&gt;• #!/bin/sh — Execute using the Bourn shell (if available) or a &lt;br /&gt;             Bourne compatible shell such as Bourne-again shell&lt;br /&gt;• #!/usr/bin/ksh --- Execute using korn shell&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Note:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;    In computing, a shebang (also called a hashbang, hashpling, &lt;br /&gt;pound  bang, or  crunchbang) refers to the  characters "#!" when &lt;br /&gt;they are the first two characters in a text file. In a Unix-like &lt;br /&gt;operating system, the program loader takes the presence of these &lt;br /&gt;two  characters as an indication that the  file is a script, and &lt;br /&gt;tries to execute  that script using the interpreter specified by &lt;br /&gt;the rest of  the first line in the file. For instance, shell &lt;br /&gt;scripts for the Bourne shell start with the first line:&lt;br /&gt;#!/bin/sh&lt;b&gt;&lt;span style="font-size:155%;"&gt;&lt;br /&gt;&lt;br /&gt;Executing a shell script&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;   Once a shell script is created, there are several ways to &lt;br /&gt;execute it. However, before executing a shell script you must&lt;br /&gt;assign proper permissions. Using chmod command you can change&lt;br /&gt;file permissions.For example giving execute permission to the &lt;br /&gt;file “temp”:&lt;br /&gt;chmod +x temp&lt;br /&gt;First create a sample shell script “temp”. &lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;$cat &gt; temp&lt;br /&gt;#!/bin/bash&lt;br /&gt;echo "My name is  `whoami`" &lt;br /&gt;echo "Users  are `users`" &lt;br /&gt;echo "OS name is `uname -s`" &lt;br /&gt;echo "current shell is $SHELL"&lt;br /&gt;ctrl+d&lt;/span&gt;&lt;b&gt;&lt;span style="font-size:155%;"&gt;&lt;br /&gt;&lt;br /&gt;Method 1 to execute a shell script:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;./scriptname&lt;br /&gt;&lt;br /&gt;   You must assign execute permission to the script.&lt;br /&gt;&lt;br /&gt;   With this method script is executed by interpreter  which is &lt;br /&gt;defined at sha-bang line.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt; Note:&lt;/span&gt;&lt;/b&gt;             Script path should be relative to the current &lt;br /&gt;directory then  only this method works. If you want to execute a &lt;br /&gt;script in the current directory you use ./scriptname. In fact if&lt;br /&gt;you want to execute  a  script in the  parent  directory of your &lt;br /&gt;current location you can use ../scriptname , as .  means current &lt;br /&gt;and .. means parent.&lt;br /&gt;&lt;br /&gt;Run temp script.&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;[root@localhost shell]# ./temp &lt;br /&gt;My name is  root &lt;br /&gt;Users  are root root &lt;br /&gt;OS name is Linux &lt;br /&gt;current shell is /bin/bash &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;now change sha-bang line with #!/bin/ksh and execute the script&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;[root@localhost shell]# ./temp &lt;br /&gt;bash: ./temp: /bin/ksh: bad interpreter: No such file or directory &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;now change sha-bang line with #!/bin/zsh and execute the script&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;[root@localhost shell]# ./temp&lt;br /&gt;My name is  root&lt;br /&gt;Users  are root root&lt;br /&gt;OS name is Linux&lt;br /&gt;current shell is /bin/bash&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So wrong path to interpreter  or bad interpreter gives error message.&lt;b&gt;&lt;span style="font-size:155%;"&gt;&lt;br /&gt;&lt;br /&gt;Method 2 to execute a shell script:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  you can execute the bash script by specifying the filename &lt;br /&gt;as an argument to the bash,sh,zsh commands.&lt;br /&gt;  This is a bad way  to call a script,  since it will override &lt;br /&gt;the #! at the beginning of the script.Depending on command you&lt;br /&gt;used, current shell spawns relative subshell.&lt;br /&gt;&lt;br /&gt;$sh temp&lt;br /&gt;     or&lt;br /&gt;$bash temp&lt;br /&gt;     or&lt;br /&gt;$zsh temp&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;My name is  root &lt;br /&gt;Users  are king root root sai venu &lt;br /&gt;OS name is Linux &lt;br /&gt;Current shell is /bin/bash &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Note:&lt;/span&gt;&lt;/b&gt;  It is good to use proper command. Suppose your script is&lt;br /&gt;korn shell script,then run it as:&lt;br /&gt;ksh temp&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Caution:&lt;/span&gt;&lt;/b&gt;   In this method shell runs script in sub shell. &lt;br /&gt;So you can't access some shell builtin variable values.&lt;br /&gt;For example run bellow shell script:&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;lines=$LINES&lt;br /&gt;cols=$COLUMNS&lt;br /&gt;echo $lines&lt;br /&gt;echo  $cols&lt;/span&gt;&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 sample&lt;br /&gt;It prints nothing&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Note:&lt;/span&gt;&lt;/b&gt;  To solve this problem export current shell variables &lt;br /&gt;using “export” command.Before executing script, export all&lt;br /&gt;variables you need.&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;$ export LINES COLUMNS&lt;br /&gt;$ sh sample&lt;br /&gt;24 &lt;br /&gt;80&lt;b&gt;&lt;span style="font-size:155%;"&gt;&lt;br /&gt;&lt;br /&gt;Method 3 to execute a shell script:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;     you can execute the bash script by specifying the filename &lt;br /&gt;as an argument to the . (dot) or source command. In this method&lt;br /&gt;script will be run in current shell.&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;$ . temp&lt;br /&gt;   or&lt;br /&gt;$ source temp&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;[root@localhost shell]#  . temp &lt;br /&gt;My name is  root &lt;br /&gt;Users  are king root root sai venu &lt;br /&gt;OS name is Linux &lt;br /&gt;current shell is /bin/bash &lt;br /&gt;&lt;br /&gt;[root@localhost shell]# source temp &lt;br /&gt;My name is  root &lt;br /&gt;Users  are king root root sai venu &lt;br /&gt;OS name is Linux &lt;br /&gt;current shell is /bin/bash &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Caution:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;  Try to unset variables at the end of the script. Otherwise &lt;br /&gt;you will get add results. Variable and functions used in the &lt;br /&gt;script will be alive after execution of the script.&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;create two files&lt;br /&gt;cat &gt; sample2&lt;br /&gt;n=100 &lt;br /&gt;m=200 &lt;br /&gt;echo "n=$n  m=$m" &lt;br /&gt;ctrl+d&lt;br /&gt;&lt;br /&gt;cat &gt; sample3&lt;br /&gt;n=$((n+m)) &lt;br /&gt;m=$((m+25)) &lt;br /&gt;echo "n=$n  m=$m" ctrl+d&lt;br /&gt;&lt;br /&gt;Execute scripts using sh command&lt;br /&gt;&lt;br /&gt;[root@localhost shell]# sh sample2 &lt;br /&gt;n=100  m=200 &lt;br /&gt;[root@localhost shell]# sh sample3 &lt;br /&gt;n=0  m=25 &lt;br /&gt;&lt;br /&gt;Now execute scripts using . Or source command&lt;br /&gt;&lt;br /&gt;[root@localhost shell]# . sample2 &lt;br /&gt;n=100  m=200 &lt;br /&gt;[root@localhost shell]# . sample3 &lt;br /&gt;n=300  m=225 &lt;br /&gt;&lt;br /&gt;So be careful. Try to unset variables at the end of the script.&lt;b&gt;&lt;span style="font-size:155%;"&gt;&lt;br /&gt;&lt;br /&gt;Method 4 to execute a shell script:    &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;   You can also execute scripts by just typing its name alone.&lt;br /&gt;However, for this method to work, the directory containing the&lt;br /&gt;script must be defined in your PATH variable and file must has &lt;br /&gt;executable permission.&lt;br /&gt;&lt;br /&gt;For example run “temp” script directly&lt;br /&gt;&lt;br /&gt;[root@localhost shell]# temp &lt;br /&gt;bash: temp: command not found &lt;br /&gt;&lt;br /&gt;   Now add your home directory to PATH variable(I am assuming &lt;br /&gt;script located at home directory).&lt;br /&gt;&lt;br /&gt;PATH=${PATH}:$HOME&lt;br /&gt;&lt;br /&gt;Now execute script with name, you will get result.&lt;br /&gt;&lt;br /&gt;[root@localhost ~]# temp &lt;br /&gt;My name is  root &lt;br /&gt;Users  are king root root sai venu &lt;br /&gt;OS name is Linux &lt;br /&gt;current shell is /bin/bash &lt;br /&gt;&lt;br /&gt; Suppose  you changed “temp” script name to “ls”, what will&lt;br /&gt;happen, which result you will get.&lt;br /&gt;&lt;br /&gt;[root@localhost ~]# mv temp ls &lt;br /&gt;[root@localhost ~]# ls &lt;br /&gt;Desktop  ls  sample  sample2  sample3 &lt;br /&gt;&lt;br /&gt;So you will get “ls” command output. Why ?&lt;br /&gt;&lt;br /&gt;    When you type name of any command at $ prompt, what shell &lt;br /&gt;do is it first look that command in it's internal part(called &lt;br /&gt;as internal command,which is part of shell itself, and always&lt;br /&gt;available to execute),if found as internal command shell will &lt;br /&gt;execute it, if not found then shell will look  PATH  setting, &lt;br /&gt;and try to find our requested commands executable file in all &lt;br /&gt;of the directories mentioned in PATH settings,if found it will &lt;br /&gt;execute it, other wise it will give message &lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;“bash:XXXX:command not found”.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So don't use command name as shell script.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Note:&lt;/span&gt;&lt;/b&gt;If you have two different files with same name in different &lt;br /&gt;directories, which file will execute?&lt;br /&gt;The directory which comes first in PATH setting will be executed. &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;First create a file “testscript” in home directory.&lt;br /&gt;&lt;br /&gt;$  cat &gt; testscript&lt;br /&gt;whoami&lt;br /&gt;ctrl+d&lt;br /&gt;&lt;br /&gt;Then create a directory “bin” in home directory &lt;br /&gt;$ mkdir bin&lt;br /&gt;Now create a file with same name “testscript”  in  bin dirctory.&lt;br /&gt;$ cd bin&lt;br /&gt;$ cat &gt; testscript&lt;br /&gt;date&lt;br /&gt;ctrl+d&lt;br /&gt;&lt;br /&gt;Then add “bin” directory to PATH variable.If your “bin” directory &lt;br /&gt;already in PATH setting leave it.&lt;br /&gt;&lt;br /&gt;PATH=${PATH}:$HOME/bin&lt;br /&gt;&lt;br /&gt;now check your path settings&lt;br /&gt;&lt;br /&gt;[root@localhost ~]# echo $PATH &lt;br /&gt; /usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:&lt;br /&gt;/usr/lib/ccache:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/&lt;br /&gt;bin:/usr/bin:/bin:/root/bin:/root&lt;br /&gt;&lt;br /&gt;/root and /root/bin added to your PATH.&lt;br /&gt;I added /root/bin  directory before /root directory.&lt;br /&gt;&lt;br /&gt;now execute “testscript” script&lt;br /&gt;&lt;br /&gt;[root@localhost ~]# testscript  &lt;br /&gt;Sat Oct 10 06:57:14 IST 2009 &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-6596279919133437795?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6596279919133437795/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/how-to-execute-shell-script_10.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6596279919133437795'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6596279919133437795'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/how-to-execute-shell-script_10.html' title='How to Execute 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>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7506040709075865756</id><published>2009-10-07T08:19:00.000-07:00</published><updated>2009-10-07T09:40:46.554-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Bash for loop example-1</title><content type='html'>&lt;pre  style="font-family:arial;"&gt;&lt;span style="font-size:100%;"&gt;#!/bin/bash&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt; # Usage: script.sh number&lt;br /&gt;# A for loop example produces following output&lt;br /&gt;#&lt;br /&gt;#             *&lt;br /&gt;#          *     *&lt;br /&gt;#       *     *     *&lt;br /&gt;#    *     *     *     *&lt;br /&gt;# *     *     *     *     *&lt;br /&gt;#&lt;/span&gt;&lt;br /&gt;&lt;h3&gt;Method1&lt;/h3&gt;c=1&lt;br /&gt;n=$1&lt;br /&gt;echo -e "\033[47m\c" &lt;span style="color: rgb(204, 102, 204);"&gt; #colourizing output&lt;/span&gt;&lt;br /&gt;for ((row=1;row&lt;=n;row++))&lt;br /&gt;do&lt;br /&gt;        for ((i=row;i&amp;lt;n;i++))&lt;br /&gt;        do&lt;br /&gt;                echo -n '   '&lt;br /&gt;        done&lt;br /&gt;        for ((k=1;k&amp;lt;=c;k++))&lt;br /&gt;        do&lt;br /&gt;           if [ $((k%2)) -eq 0 ]&lt;br /&gt;           then&lt;br /&gt;             echo -n "   "&lt;br /&gt;           else&lt;br /&gt;             echo -e "\033[43m * \033[47m\c"&lt;br /&gt;           fi&lt;br /&gt;        done&lt;br /&gt;        for ((i=row;i&amp;lt;n;i++))&lt;br /&gt;        do&lt;br /&gt;                echo -n '   '&lt;br /&gt;        done&lt;br /&gt; c=$((c+2))&lt;br /&gt;        echo&lt;br /&gt;done&lt;br /&gt;echo -e "\033[0m"  &lt;span style="color: rgb(204, 102, 204);"&gt; #Restoring colours&lt;/span&gt;&lt;br /&gt;unset c i k n row&lt;br /&gt;&lt;h3&gt;Method2&lt;/h3&gt;#!/bin/bash&lt;br /&gt;n=$1&lt;br /&gt;t=$((2*n - 1 ))&lt;br /&gt;a=$n&lt;br /&gt;b=$((a+1))&lt;br /&gt;echo -e -n "\033[47m"&lt;br /&gt;for (( i=1 ; i&amp;lt;=$n ;i++ ))&lt;br /&gt;do&lt;br /&gt;  a=$(($b-1))&lt;br /&gt;  b=$a&lt;br /&gt;  k=1&lt;br /&gt;  for (( j=1 ; j&amp;lt;=$t ; j++ ))&lt;br /&gt;  do&lt;br /&gt;      if [ $j -eq $a ]&lt;br /&gt;      then&lt;br /&gt;&lt;br /&gt;         echo -e "\033[43m * \033[47m\c"&lt;br /&gt;         if [ $k -lt $i ]&lt;br /&gt;         then&lt;br /&gt;&lt;br /&gt;            a=$((a+2))&lt;br /&gt;   let k++&lt;br /&gt;         fi&lt;br /&gt;      else&lt;br /&gt;         echo -n "   "&lt;br /&gt;     fi&lt;br /&gt;&lt;br /&gt;  done&lt;br /&gt;echo&lt;br /&gt;done&lt;br /&gt;echo -e "\033[0m"&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_ioCu0w5ZnYE/Ssy93bCc-KI/AAAAAAAAAqI/3P41fESL7wA/s1600-h/forloop.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 184px;" src="http://4.bp.blogspot.com/_ioCu0w5ZnYE/Ssy93bCc-KI/AAAAAAAAAqI/3P41fESL7wA/s400/forloop.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5389891613904140450" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7506040709075865756?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7506040709075865756/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/bash-for-loop-example-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7506040709075865756'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7506040709075865756'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/bash-for-loop-example-1.html' title='Bash for loop example-1'/><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://4.bp.blogspot.com/_ioCu0w5ZnYE/Ssy93bCc-KI/AAAAAAAAAqI/3P41fESL7wA/s72-c/forloop.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1982866784590507068</id><published>2009-10-07T07:58:00.000-07:00</published><updated>2009-10-07T08:15:58.559-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Bash for Loop Syntax</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span style=";font-family:arial;font-size:100%;"  &gt;A 'for loop' is a bash programming language statement which allows code to be repeatedly&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:arial;font-size:100%;"  &gt;executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process&lt;/span&gt;&lt;br /&gt;&lt;span style=";font-family:arial;font-size:100%;"  &gt;within a bash script.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style=";font-family:arial;font-size:100%;"  &gt;&lt;br /&gt;For example, you can run UNIX command or task 5 times or read and process list of files using&lt;br /&gt;a for loop. A for loop can be used at a shell prompt or within a shell script itself.&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 0, 0);font-size:130%;" &gt;&lt;br /&gt;for loop syntax&lt;br /&gt;&lt;/span&gt;Numeric ranges for syntax is as follows:&lt;br /&gt;&lt;br /&gt;for VARIABLE in 1 2 3 4 5 .. N&lt;br /&gt;do&lt;br /&gt;      command1&lt;br /&gt;      command2&lt;br /&gt;      commandN&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204); font-weight: bold;"&gt;Examples:&lt;/span&gt;&lt;br /&gt;#!/bin/bah&lt;br /&gt;for i in 1 2 3 4 5&lt;br /&gt;do&lt;br /&gt; echo "Welcome $i times"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Using “seq” command&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;#!/bin/bash&lt;br /&gt;for i in $(seq 1 2 20)&lt;br /&gt;do&lt;br /&gt; echo "Welcome $i times"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;To know more about “seq” command check manual using “man seq” command&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;Bash 3.0+ version supports following syntax also&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;for i in {1..5}&lt;br /&gt;do&lt;br /&gt; echo "Welcome $i times"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(255, 0, 0);font-size:130%;" &gt;Three expression syntax &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;for (( EXP1; EXP2; EXP3 ))&lt;br /&gt;do&lt;br /&gt;      command1&lt;br /&gt;      command2&lt;br /&gt;      command3&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(204, 51, 204);"&gt;Example:&lt;/span&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;for (( c=1; c&lt;=5; c++ ))&lt;br /&gt;do&lt;br /&gt;echo "Welcome $c times..." done &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1982866784590507068?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1982866784590507068/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/bash-for-loop-syntax.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1982866784590507068'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1982866784590507068'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/bash-for-loop-syntax.html' title='Bash for Loop Syntax'/><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-476295170983721883</id><published>2009-10-07T06:10:00.000-07:00</published><updated>2009-10-07T06:20:36.629-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script To Print Pyramid - Using while Loop</title><content type='html'>&lt;pre style="font-family: arial;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt; #!/bin/bash&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt; # Usage: scriptname argument&lt;br /&gt; # Here argument is height of pyramid&lt;br /&gt; # Output would be pyramid pattern of stars&lt;br /&gt; #       0                 *&lt;br /&gt; #       1                ***&lt;br /&gt; #       2               *****&lt;br /&gt; #       3              *******&lt;br /&gt; #       4             *********&lt;br /&gt; #       5            ***********&lt;br /&gt; #       6           *************&lt;br /&gt; #       .          ***************&lt;br /&gt; #       .         *****************&lt;br /&gt; #       .        *******************&lt;br /&gt; #       n-1     *********************&lt;br /&gt; #                ---\/--- | ---\/---&lt;br /&gt; #                  n - 1      n - 1&lt;br /&gt; #&lt;/span&gt;&lt;br /&gt;clear&lt;br /&gt;n=$1&lt;br /&gt;row=1;&lt;br /&gt;echo -e "\033[47m"&lt;br /&gt;        while [[ $row -le $n ]]&lt;br /&gt;        do&lt;br /&gt;        loop=1;&lt;br /&gt;        spaces=$((n-row))&lt;br /&gt;        stars=$((2*row - 1))&lt;br /&gt;&lt;br /&gt;           while [[ $loop -le $spaces ]]&lt;br /&gt;           do&lt;br /&gt;                echo -n ' '&lt;br /&gt;                let loop++&lt;br /&gt;           done&lt;br /&gt;        loop=1;&lt;br /&gt;           while [[ $loop -le $stars ]]&lt;br /&gt;           do&lt;br /&gt;                echo -e '\033[43m*\033[47m\c'&lt;br /&gt;                let loop++&lt;br /&gt;           done&lt;br /&gt;        loop=1;&lt;br /&gt;           while [[ $loop -le $spaces ]]&lt;br /&gt;           do&lt;br /&gt;                echo -n ' '&lt;br /&gt;                let loop++&lt;br /&gt;           done&lt;br /&gt;        echo&lt;br /&gt;        let row++&lt;br /&gt;        done&lt;br /&gt;echo -e "\033[0m"&lt;br /&gt;unset row loop spaces stars n&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt; [root@localhost blog]# sh while_pyramid 22&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s1600-h/1.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 330px; height: 400px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s400/1.png" alt="" id="BLOGGER_PHOTO_ID_5389838440569382530" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-476295170983721883?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/476295170983721883/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-print-pyramid-using.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/476295170983721883'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/476295170983721883'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-print-pyramid-using.html' title='Shell Script To Print Pyramid - Using while Loop'/><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://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s72-c/1.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-4622460125999000478</id><published>2009-10-07T05:35:00.000-07:00</published><updated>2009-10-07T06:08:31.605-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginners Scripts'/><title type='text'>Shell Script To Print Pyramid-Using for Loop</title><content type='html'>&lt;pre style="font-family: arial;"&gt;&lt;span style="font-size:100%;"&gt;#!/bin/bash&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Usage: scriptname argument&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Here argument is height of pyramid&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Output would be pyramid pattern of stars&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;&lt;br /&gt;#       0                 *&lt;br /&gt;#       1                ***&lt;br /&gt;#       2               *****&lt;br /&gt;#       3              *******&lt;br /&gt;#       4             *********&lt;br /&gt;#       5            ***********&lt;br /&gt;#       6           *************&lt;br /&gt;#       .          ***************&lt;br /&gt;#       .         *****************&lt;br /&gt;#       .        *******************&lt;br /&gt;#      n-1     *********************&lt;br /&gt;#                ---\/--- | ---\/---&lt;br /&gt;#                  n - 1      n - 1&lt;br /&gt;#&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;clear&lt;br /&gt;n=$1&lt;br /&gt;echo -e "\033[47m"  &lt;span style="color: rgb(204, 102, 204);"&gt;#used for colourizing output&lt;/span&gt;&lt;br /&gt;for ((row=1;row&lt;=n;row++)) &lt;br /&gt;do&lt;br /&gt;    spaces=$((n-row))&lt;br /&gt;    stars=$((2*$row - 1))&lt;br /&gt;    for ((i=1;i&lt;=spaces;i++))&lt;br /&gt;    do&lt;br /&gt;        echo -n ' '&lt;br /&gt;    done &lt;br /&gt;    for ((i=1;i&lt;=stars;i++))&lt;br /&gt;    do &lt;br /&gt;        echo -e '\033[43m*\033[47m\c'&lt;br /&gt;    done&lt;br /&gt;    for ((i=1;i&lt;=spaces;i++))&lt;br /&gt;    do  &lt;br /&gt;         echo -n ' ' &lt;br /&gt;    done&lt;br /&gt;    echo&lt;br /&gt;done&lt;br /&gt;echo -e "\033[0m"&lt;br /&gt;unset n i spaces stars row&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Unset variables is a good programming practice&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;[root@localhost blog]# sh for_pyramid 22&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s1600-h/1.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 330px; height: 400px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s400/1.png" alt="" id="BLOGGER_PHOTO_ID_5389838440569382530" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-4622460125999000478?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/4622460125999000478/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-print-pyramid-using-for.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4622460125999000478'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4622460125999000478'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/10/shell-script-to-print-pyramid-using-for.html' title='Shell Script To Print Pyramid-Using for Loop'/><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://2.bp.blogspot.com/_ioCu0w5ZnYE/SsyNgU7iJoI/AAAAAAAAAqA/yxLn3Jf7rAU/s72-c/1.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7733034577682134368</id><published>2009-09-22T22:22:00.000-07:00</published><updated>2009-09-22T22:49:06.973-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Sample System Monitor Shell Script - Using Options</title><content type='html'>&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# sys_monitor2.sh&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Usage: sys_monitor2.sh [-u|-d|-f (D/M)| -c number | -m number]&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Sample system monitor script using options&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Tested under Fedora 9&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Invalidoptions()&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  echo "Usage: `basename $0` [OPTIONS]"&lt;br /&gt;  echo "OPTIONS:"&lt;br /&gt;  echo -e "\t -d      for display today's date"&lt;br /&gt;  echo -e "\t -u      for Logged in users list"&lt;br /&gt;  echo -e "\t -f ARG  for Disk and Memory Statistics"&lt;br /&gt;  echo -e "\t        (ARG=D for disk statistics; ARG=M for memory statistics)"&lt;br /&gt;  echo -e "\t -c ARG  for Top CPU consuming process"&lt;br /&gt;  echo -e "\t         (ARG=10 means top 10 process)"&lt;br /&gt;  echo -e "\t -m ARG  for Top Memory consuming process"&lt;br /&gt;  echo -e "\t         (ARG=10 means top 10 process)"&lt;br /&gt;  echo -e "\t Note: Only one option at a time and -f,-c and -m require argument"&lt;br /&gt;  exit 1&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;Isnumber()&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  if [ $1 -eq $1 2&gt; /dev/null ]&lt;br /&gt;     then&lt;br /&gt;     :&lt;br /&gt;  else&lt;br /&gt;     echo -e "You supplied bad argument, \"$1\" is not a number"&lt;br /&gt;     Invalidoptions&lt;br /&gt;  fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if [ $# -lt 1 -o $# -gt 2 ]&lt;br /&gt;then&lt;br /&gt;  Invalidoptions&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ $# -eq 1 -a "$1" != "-d" -a "$1" != "-u" -a "$1" != "-f" -a "$1" != "-c" ]&lt;br /&gt;then&lt;br /&gt;  Invalidoptions&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;if [ $# -eq 2 ] &amp;amp;&amp;amp; [ "$1" != "-f" -a "$1" != "-c" -a "$1" != "-m" ]&lt;br /&gt;then&lt;br /&gt;  Invalidoptions&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;choice=&lt;br /&gt;top="head -$2"&lt;br /&gt;while getopts udf:c:m: choice&lt;br /&gt;do&lt;br /&gt;case $choice in&lt;br /&gt;  d) echo -e " Today's Date: \c"&lt;br /&gt;     date +"  %d-%h-%Y       Time:  %T";;&lt;br /&gt;  u) echo -e "\tCurrently Logged In Users"&lt;br /&gt;     who;;&lt;br /&gt;  f)&lt;br /&gt;    if [ "$OPTARG" = "D" ]&lt;br /&gt;    then&lt;br /&gt;        echo -e "\t\tDisk Statistics"&lt;br /&gt;        df -h | grep "%"&lt;br /&gt;    elif [ "$OPTARG" = "M" ]&lt;br /&gt;    then&lt;br /&gt;        echo -e "\t Memory Statistics "&lt;br /&gt;        free -m | awk 'BEGIN{printf "\t\tTotal\tUsed\tFree\n"; OFS="\t" }\&lt;br /&gt; /Mem/||/Swap/{printf "\t"; print $1,$2,$3,$4}'&lt;br /&gt;    else&lt;br /&gt;       Invalidoptions&lt;br /&gt;    fi;;&lt;br /&gt;  m) Isnumber $OPTARG&lt;br /&gt;     k3sort="sort -nr -k 3"&lt;br /&gt;     echo -e " PID   PPID  MEM  CPU COMMAND "&lt;br /&gt;     ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k3sort|$top;;&lt;br /&gt;  c) Isnumber $OPTARG&lt;br /&gt;     k4sort="sort -nr -k 4"&lt;br /&gt;     echo -e " PID   PPID  MEM  CPU COMMAND "&lt;br /&gt;     ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k4sort|$top;;&lt;br /&gt;&lt;br /&gt;esac&lt;br /&gt;done&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;font-family:verdana;" &gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style=";font-family:verdana;font-size:100%;"  &gt;[root@localhost blog]# sh sys_monitor2.sh -u&lt;br /&gt;     Currently Logged In Users&lt;br /&gt;root     tty7         2009-09-23 13:48 (:0)&lt;br /&gt;root     pts/2        2009-09-23 14:36 (:0.0)&lt;br /&gt;&lt;br /&gt;[root@localhost blog]# sh sys_monitor2.sh -d&lt;br /&gt;Todays Date:   23-Sep-2009       Time:  16:50:38&lt;br /&gt;&lt;br /&gt;[root@localhost blog]# sh sys_monitor2.sh -m 5&lt;br /&gt;PID   PPID  MEM  CPU COMMAND&lt;br /&gt;3122  3102  9.6  3.0 firefox&lt;br /&gt;2765  2540  1.9  0.0 nautilus&lt;br /&gt;3849     1  1.7  1.0 ktorrent&lt;br /&gt;2882     1  1.6  0.0 tomboy&lt;br /&gt;2810     1  1.6  0.0 /usr/bin/sealer&lt;br /&gt;&lt;br /&gt;[root@localhost blog]# sh sys_monitor2.sh -m&lt;br /&gt;Usage: sys_monitor2.sh [OPTIONS]&lt;br /&gt;OPTIONS:&lt;br /&gt;      -d      for display today's date&lt;br /&gt;      -u      for Logged in users list&lt;br /&gt;      -f ARG  for Disk and Memory Statistics&lt;br /&gt;             (ARG=D for disk statistics; ARG=M for memory statistics)&lt;br /&gt;      -c ARG  for Top CPU consuming process&lt;br /&gt;              (ARG=10 means top 10 process)&lt;br /&gt;      -m ARG  for Top Memory consuming process&lt;br /&gt;              (ARG=10 means top 10 process)&lt;br /&gt;      Note: Only one option at a time and -f,-c and -m require argument&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-7733034577682134368?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7733034577682134368/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/09/sample-system-monitor-shell-script_22.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7733034577682134368'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7733034577682134368'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/09/sample-system-monitor-shell-script_22.html' title='Sample System Monitor Shell Script - Using Options'/><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-4725032585417505317</id><published>2009-09-22T21:31:00.000-07:00</published><updated>2009-09-22T22:20:42.292-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Sample System Monitor Shell Script -Menu Based</title><content type='html'>&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# sys_monitor.sh&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Sample system monitor script using menu&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Tested under Fedora 9&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Create the following menu and clear the screen each time it appears&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;echo -e "\033[1m `uname -or` Monitor script\033[0m"&lt;br /&gt;items="       1.Date&lt;br /&gt;       2.Current Users&lt;br /&gt;       3.Disk Statistics&lt;br /&gt;       4.Memory Statistics&lt;br /&gt;       5.Top 10 Memory consuming process&lt;br /&gt;       6.Top 10 CPU consuming process&lt;br /&gt;       7.Exit"&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;exit_function()&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   clear&lt;br /&gt;   exit&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#function enter is used to go  back to menu and clear screen&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;enter()&lt;/span&gt;&lt;br /&gt;{&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;span style="color: rgb(204, 102, 204);"&gt;# When -icanon is set then one character has been received.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# min 0 means that read should read 0 characters.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# time 0 ensures that read is terminated the moment one character is hit.&lt;/span&gt;&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;span style="color: rgb(204, 102, 204);"&gt;#The while loop ensures that so long as at least one character is not received&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# "read" continue to get executed&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if [ "$ans" = "y" -o "$ans" = "Y" ]&lt;br /&gt;then&lt;br /&gt;   stty sane         &lt;span style="color: rgb(204, 102, 204);"&gt; &lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Restoring terminal settings&lt;/span&gt;&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;choice=&lt;br /&gt;h10="head -10"&lt;br /&gt;while true&lt;br /&gt;do&lt;br /&gt;   echo -e "\n\t    PROGRAM MENU \n"&lt;br /&gt;   echo -e "$items \n"&lt;br /&gt;   echo -n "Enter your choice :"&lt;br /&gt;read choice&lt;br /&gt;case $choice in&lt;br /&gt;   1) clear; echo -e "\n\n\t\t Today's Date \n"&lt;br /&gt;   date +"       %d-%h-%Y        Time %T"; enter;;&lt;br /&gt;   2) clear; echo -e "\n\n\t\t Currently Logged In Users\n"&lt;br /&gt;   who; enter;;&lt;br /&gt;   3) clear; echo -e "\n\n\t\t Disk Statistics\n"&lt;br /&gt;   df -h | grep "%"&lt;br /&gt;   enter;;&lt;br /&gt;   4) clear; echo -e "\n\n\t\t Memory Statistics\n "&lt;br /&gt;   free -m | awk 'BEGIN{printf "\t\tTotal\tUsed\tFree\n\n"; OFS="\t" }\&lt;br /&gt;/Mem/||/Swap/{printf "\t"; print $1,$2,$3,$4}'&lt;br /&gt;  enter;;&lt;br /&gt;   5) clear&lt;br /&gt;   k3sort="sort -nr -k 3"&lt;br /&gt;   echo -e "\033[1m PID   PPID  MEM  CPU COMMAND \033[0m "&lt;br /&gt;   ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k3sort|$h10&lt;br /&gt;   enter;;&lt;br /&gt;   6) clear&lt;br /&gt;   k4sort="sort -nr -k 4"&lt;br /&gt;   echo -e "\033[1m PID   PPID  MEM  CPU COMMAND \033[0m"&lt;br /&gt;   ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k4sort|$h10&lt;br /&gt;   enter;;&lt;br /&gt;   7)exit_function  ;;&lt;br /&gt;   *)echo -e "You entered wrong option \n Please enter 1,2,3,4,5,6 or 7\n"&lt;br /&gt;   echo " Press enter to continue"&lt;br /&gt;   read&lt;br /&gt;   clear&lt;br /&gt;esac&lt;br /&gt;done&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=";font-family:verdana;font-size:100%;"  &gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;pre&gt;&lt;span style=";font-family:verdana;font-size:100%;"  &gt;&lt;span style="font-family:verdana;"&gt;2.6.25-14.fc9.i686 GNU/Linux Monitor script&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;           PROGRAM MENU&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      1.Date&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      2.Current Users&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      3.Disk Statistics&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      4.Memory Statistics  &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      5.Top 10 memory cosuming process&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      6.Top 10 CPU consuming process&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;      7.Exit&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;Enter your choice :5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;PID   PPID  MEM  CPU COMMAND &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;3122  3102  9.2  2.6 firefox&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2765  2540  1.9  0.0 nautilus&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;3849     1  1.7  1.0 ktorrent&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2882     1  1.6  0.0 tomboy&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2810     1  1.6  0.0 /usr/bin/sealer&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;4041     1  1.4  0.1 gnome-terminal&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2394  2393  1.4  1.8 Xorg&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2759  2540  1.2  0.0 gnome-panel&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2876     1  0.9  0.0 clock-applet&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;2870     1  0.9  0.0 mixer_applet2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;Do you want to continue(y/n):&lt;/span&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-4725032585417505317?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/4725032585417505317/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/09/sample-system-monitor-shell-script.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4725032585417505317'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/4725032585417505317'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/09/sample-system-monitor-shell-script.html' title='Sample System Monitor Shell Script -Menu Based'/><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>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6091313998008963627</id><published>2009-09-19T10:33:00.000-07:00</published><updated>2009-09-19T11:37:04.379-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Bash Script to check after every one minute whether a user has logged in or not</title><content type='html'>&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# isuserloggedin.sh&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Usage: isuserloggedin.sh username&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Shell script which checks after every one minute whether a user has logged in&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# or not&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# You can also run script in background using &amp;amp; then foreground it to view result&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;   echo "You supplied wrong arguments"&lt;br /&gt;   echo "usage : `basename $0` username"&lt;br /&gt;exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;isuserexist()&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;grep -w "$1" /etc/passwd &gt; /dev/null&lt;br /&gt;&lt;br /&gt;if [ $? -eq 1 ]&lt;br /&gt;then&lt;br /&gt;   echo "$1 is not a valid user"&lt;br /&gt;exit 1&lt;br /&gt;fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;isuserexist $1&lt;br /&gt;time=0&lt;br /&gt;while true&lt;br /&gt;do&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#  you can replace following two statements with&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#   if `who|grep $1 &gt; /dev/null`&lt;/span&gt;&lt;br /&gt;who|grep $1 &gt; /dev/null&lt;br /&gt;if [ $? -eq 0 ]&lt;br /&gt;then&lt;br /&gt;  echo "User $1 is logged in "&lt;br /&gt;  if [ $time -gt 60 ]&lt;br /&gt;  then&lt;br /&gt;     hours=$((time/60))&lt;br /&gt;     minutes=$((time%60))&lt;br /&gt;     echo "He is logged in $hours hour(s) $minutes minute(s) late"&lt;br /&gt;  else&lt;br /&gt;     echo "He is logged in $time minute(s) late"&lt;br /&gt;  fi&lt;br /&gt;exit 0&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;let time++&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# You can use following formats also&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#   time=`expr $time + 1 `&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#  time=$((time+1))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;sleep 60&lt;br /&gt;fi&lt;br /&gt;done&lt;br /&gt;&lt;/span&gt;&lt;/pre&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;[root@localhost shell]# sh isuserloggedin.sh&lt;br /&gt;you have suplied wrong arguments&lt;br /&gt;usage : isuserloggedin.sh username&lt;br /&gt;[root@localhost shell]# sh isuserloggedin.sh root&lt;br /&gt;User root is logged in&lt;br /&gt;He is logged in 0 minute(s) late&lt;br /&gt;[root@localhost shell]# sh isuserloggedin.sh roott&lt;br /&gt;roott is not a valid user&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;Run script in background&lt;/span&gt;&lt;br /&gt;[root@localhost shell]# sh isuserloggedin.sh venu &amp;amp;&lt;br /&gt;[1] 15917&lt;br /&gt;[root@localhost shell]# User venu is logged in&lt;br /&gt;He is logged in 3 minute(s) late&lt;br /&gt;&lt;br /&gt;[1]+  Done                    sh isuserloggedin.sh venu&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;Run script in background then foreground it&lt;/span&gt;&lt;br /&gt;[root@localhost shell]# sh isuserloggedin.sh venu &amp;amp;&lt;br /&gt;[1] 16223&lt;br /&gt;[root@localhost shell]# fg&lt;br /&gt;sh isuserloggedin.sh venu&lt;br /&gt;User venu is logged in&lt;br /&gt;He is logged in 1 minute(s) late&lt;br /&gt;[root@localhost shell]#&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6091313998008963627?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6091313998008963627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/09/bash-script-to-check-after-every-one.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6091313998008963627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6091313998008963627'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/09/bash-script-to-check-after-every-one.html' title='Bash Script to check after every one minute whether a user has logged in 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>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-743829216393478047</id><published>2009-09-19T10:11:00.000-07:00</published><updated>2009-09-29T22:17:24.104-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Bash Script to Check whether a user has an account in your system</title><content type='html'>&lt;pre  style="font-family:verdana;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# validuser.sh&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Usage: validuser.sh username&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Script to check whether suplied user has an account in your system&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if [ $# -ne 1 ]&lt;br /&gt;then&lt;br /&gt;   echo "You supplied wrong arguments"&lt;br /&gt;   echo "Usage : `basename $0` user_name"&lt;br /&gt;exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;#grep -w "$1" /etc/passwd &gt; /dev/null&lt;br /&gt;grep -w "^$1" /etc/passwd &gt; /dev/null&lt;br /&gt;if [ $? -eq 1 ]&lt;br /&gt;then&lt;br /&gt;   echo "$1 is not a valid user"&lt;br /&gt;else&lt;br /&gt;   echo "$1 is a valid user"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# Using greps -q option you can simplify and faster your script&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# if `grep -qw "^$1" /etc/passwd`&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;# greps -q option prints nothing just returns exit status 0 or 1&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0); font-weight: bold;font-family:verdana;" &gt;Out Put:&lt;/span&gt;&lt;br /&gt;[root@localhost shell]# sh validuser.sh&lt;br /&gt;You supplied wrong arguments&lt;br /&gt;usage : validuser.sh user_name&lt;br /&gt;[root@localhost shell]# sh validuser.sh venu&lt;br /&gt;venu is a valid user&lt;br /&gt;[root@localhost shell]# sh validuser.sh venuk&lt;br /&gt;venuk is not a valid user&lt;br /&gt;[root@localhost shell]# sh validuser.sh root&lt;br /&gt;root is a valid user&lt;br /&gt;[root@localhost shell]# sh validuser.sh roott&lt;br /&gt;roott is not a valid user&lt;br /&gt;[root@localhost shell]#&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-743829216393478047?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/743829216393478047/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/09/devnull-if-eq-1-then-echo-e-n-1-is-not.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/743829216393478047'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/743829216393478047'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/09/devnull-if-eq-1-then-echo-e-n-1-is-not.html' title='Bash Script to Check whether a user has an account in your system'/><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>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1741234566809532204</id><published>2009-09-02T13:25:00.000-07:00</published><updated>2009-09-02T16:30:19.027-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>How to prompt user to enter input within the time limit in a shell script</title><content type='html'>There are various ways to read input within the time limit in shell script. I am posting 4 methods here.First three are direct methods. Final one implemented with some logic, just for practice only.&lt;br /&gt;&lt;h1&gt;Method 1:&lt;/h1&gt;&lt;br /&gt;    #!/bin/bash&lt;br /&gt;    # timedinput1.sh:      prompts times out at five seconds.&lt;br /&gt;    # Using &lt;span style="font-style:italic;"&gt;read&lt;/span&gt; command&lt;br /&gt;&lt;br /&gt;    timelimit=5&lt;br /&gt;    echo -e "  You have $timelimit seconds\n  Enter your name quickly: \c"&lt;br /&gt;    name=""&lt;br /&gt;    read -t $timelimit name&lt;br /&gt;    #read -t $timelimit name &lt;&amp;amp;1      &lt;br /&gt;    # for bash versions bellow 3.x&lt;br /&gt;    if [ ! -z "$name"  ]&lt;br /&gt;    then&lt;br /&gt;         echo -e "\n  Your name is $name"&lt;br /&gt;    else&lt;br /&gt;         echo -e "\n  TIME OUT\n You failed to enter your name"&lt;br /&gt;   fi &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;[root@localhost shell]# sh timedinput1.sh&lt;br /&gt;You have 5 seconds&lt;br /&gt;Enter your name quickly: king&lt;br /&gt;&lt;br /&gt;Your name is king&lt;br /&gt;[root@localhost shell]# sh timedinput1.sh&lt;br /&gt;You have 5 seconds&lt;br /&gt;Enter your name quickly:&lt;br /&gt;TIME OUT&lt;br /&gt;You failed to enter your name&lt;br /&gt;&lt;h1&gt;Method 2:&lt;/h1&gt;&lt;br /&gt; #!/bin/bash&lt;br /&gt; # timedinput2.sh&lt;br /&gt; # Using &lt;span style="font-style:italic;"&gt;stty&lt;/span&gt; command&lt;br /&gt; &lt;br /&gt;  timelimit=5     &lt;br /&gt;  # Time limit to enter input&lt;br /&gt;  echo -e "  You have only $timelimit seconds\n  Enter your name quickly: \c"&lt;br /&gt;  name=""&lt;br /&gt;  stty -icanon min 0  time ${timelimit}0&lt;br /&gt;&lt;br /&gt;  # "min N" with -icanon, set N characters minimum for a completed read&lt;br /&gt;  # "time N" with -icanon, set read timeout of N tenths of a second (i.e. 50 means 5 &lt;br /&gt;seconds )&lt;br /&gt;  &lt;br /&gt;  read name&lt;br /&gt;  if [ ! -z "$name"  ]&lt;br /&gt;  then&lt;br /&gt;          echo "  Your name is $name"&lt;br /&gt;  else&lt;br /&gt;          echo -e "\n  TIME OUT\n You failed to enter your name"&lt;br /&gt;  fi&lt;br /&gt;  stty sane  &lt;br /&gt;  #restore terminal settings&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;[root@localhost shell]# sh timedinput2.sh &lt;br /&gt;  You have only 5 seconds&lt;br /&gt;  Enter your name quickly: Sachin Ramesh Tendulkar&lt;br /&gt;  Your name is "Sachin Ramesh Tendulkar"&lt;br /&gt;[root@localhost shell]# sh timedinput2.sh &lt;br /&gt;  You have only 5 seconds&lt;br /&gt;  Enter your name quickly: &lt;br /&gt;  TIME OUT&lt;br /&gt; You failed to enter your name&lt;br /&gt;&lt;b&gt;Observation:&lt;/b&gt;&lt;br /&gt;  There is difference between method1 and method2.In method1 you should to enter input within 5 seconds.But in method 2 you have 5 seconds after a character has been hit.This is because &lt;span style="font-weight:bold;"&gt;time n&lt;/span&gt; means wait till &lt;span style="font-weight:bold;"&gt;n&lt;/span&gt; seconds after a character has been hit.So in method2 you can give any length of input.&lt;br /&gt;&lt;h1&gt;Method3:&lt;/h1&gt;&lt;br /&gt;     #!/bin/bash&lt;br /&gt;     # timedinput3.sh&lt;br /&gt;     # using &lt;span style="font-style:italic;"&gt;TMOUT&lt;/span&gt; environment variable&lt;br /&gt;&lt;br /&gt;      TMOUT=5&lt;br /&gt;      # TMOUT is an Internal Variable&lt;br /&gt;      # If the $TMOUT environment variable is set to a non zero value time, then the shell prompt will time out after $time seconds.This will cause a logout.&lt;br /&gt;    # If you run this script in current shell after 5 seconds you will be logout&lt;br /&gt;&lt;br /&gt;      echo -e "  You only have $TMOUT seconds\n  Enter your name quickly: \c"&lt;br /&gt;      name=""&lt;br /&gt;      read name&lt;br /&gt;      if [ ! -z "$name"  ]&lt;br /&gt;      then&lt;br /&gt;          echo "  Your name is $name"&lt;br /&gt;      else&lt;br /&gt;          echo -e "\n  TIME OUT\n  You failed to enter your name"&lt;br /&gt;      fi&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;[root@localhost shell]# sh timedinput3.sh &lt;br /&gt;  You only have 5 seconds&lt;br /&gt;  Enter your name quickly: Ricky ponting&lt;br /&gt;  Your name is "Ricky ponting"&lt;br /&gt;[root@localhost shell]# sh timedinput3.sh &lt;br /&gt;  You only have 5 seconds&lt;br /&gt;  Enter your name quickly: &lt;br /&gt;  TIME OUT&lt;br /&gt;  You failed to enter your name&lt;br /&gt;&lt;h1&gt;Method4:&lt;/h1&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# timedinput4.sh&lt;br /&gt;# Using &lt;span style="font-style:italic;"&gt;sleep&lt;/span&gt; command&lt;br /&gt;&lt;br /&gt;timelimit=5  &lt;br /&gt;#set another value if you require&lt;br /&gt;&lt;br /&gt;trap 'echo -e "\n  TIMEOUT"; exit 14' 14&lt;br /&gt;# Trapping signal 14&lt;br /&gt;&lt;br /&gt;echo -e "  You only have $timelimit seconds \n  What is your name:\c"&lt;br /&gt;&lt;br /&gt;sleep $timelimit &amp;&amp; kill -s 14 $$ &amp;&lt;br /&gt;&lt;br /&gt;# Waits 5 seconds, then sends sigalarm to script($$ environment variable gives pid of current script).&lt;br /&gt;read name&lt;br /&gt;echo "  Your name is \"$name\""&lt;br /&gt;kill $!&lt;br /&gt;#kills back ground job (i.e. sleep command)&lt;br /&gt;exit&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;[root@localhost shell]# sh timedinput4.sh &lt;br /&gt;  You only have 5 seconds &lt;br /&gt;  What is your name:Ganguly&lt;br /&gt;  Your name is "Ganguly"&lt;br /&gt;[root@localhost shell]# sh timedinput4.sh &lt;br /&gt;  You only have 5 seconds &lt;br /&gt;  What is your name:Kapil Dev&lt;br /&gt;  Your name is "Kapil Dev"&lt;br /&gt;timedinput4.sh: line 16:  3814 Terminated           sleep $timelimit &amp;&amp; kill -s 14 $$&lt;br /&gt;[root@localhost shell]# sh timedinput4.sh &lt;br /&gt;  You only have 5 seconds &lt;br /&gt;  What is your name:&lt;br /&gt;  TIMEOUT&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1741234566809532204?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1741234566809532204/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/09/how-to-read-user-input-within-time.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1741234566809532204'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1741234566809532204'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/09/how-to-read-user-input-within-time.html' title='How to prompt user to enter input within the time limit 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>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-5042608092515725721</id><published>2009-08-23T07:10:00.000-07:00</published><updated>2009-09-02T11:08:08.510-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>What is the difference between Hard Link and Soft Link in Linux</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Hard link: &lt;/span&gt;&lt;span&gt;Hard link refers to&lt;/span&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;"The specific location of physical data".&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Hard Link is a mirror copy of the original file.&lt;/li&gt;&lt;li&gt;Hard links share the same inode.&lt;/li&gt;&lt;li&gt;Any changes made to the original or Hard linked file will reflect the other.&lt;/li&gt;&lt;li&gt;Even if you delete any one of the files, nothing will happen to the other hard links.&lt;/li&gt;&lt;li&gt;But soft link which points to deleted hard link become a dangling soft link.&lt;/li&gt;&lt;li&gt;You can't link a directory even within the same file system.&lt;/li&gt;&lt;li&gt;Hard links can't cross file systems.&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;Soft link( also called symbolic link): &lt;/span&gt;&lt;span&gt;Soft link refers to "A symbolic path indicating the abstract location of another file".&lt;/span&gt; &lt;ul&gt;&lt;li&gt;Soft Link is a symbolic link to the original file.(more like windows shortcuts)&lt;/li&gt;&lt;li&gt;Soft Links will have a different Inode value.&lt;/li&gt;&lt;li&gt;Any changes made to the soft link will reflect the original file and its hard links.&lt;/li&gt;&lt;li&gt;A soft link points to the original file. If you delete the original file, the soft link fails. It would become dangling symbolic link.&lt;/li&gt;&lt;li&gt;If you delete the soft link, nothing will happen.&lt;/li&gt;&lt;li&gt;You can link a directory using soft link on same file system and also on other file system.&lt;/li&gt;&lt;li&gt;Soft links can cross file systems&lt;/li&gt;&lt;/ul&gt;     &lt;style type="text/css"&gt;  &lt;!--   @page { size: 8.5in 11in; margin: 0.79in }   P { margin-bottom: 0.08in }  --&gt;  &lt;/style&gt;  &lt;p style="margin-bottom: 0in; font-weight: bold;"&gt;Lets learn the difference with an example:&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;First create a file named with "mainfile.txt"&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFPn25LwpI/AAAAAAAAAog/ub4ygfVCY9E/s1600-h/2.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 410px; height: 85px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFPn25LwpI/AAAAAAAAAog/ub4ygfVCY9E/s400/2.png" alt="" id="BLOGGER_PHOTO_ID_5373163376598565522" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now create a hard link named with "hardlink.txt"&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFQF5gnx2I/AAAAAAAAAoo/OtFirAVq3uk/s1600-h/3.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 411px; height: 113px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFQF5gnx2I/AAAAAAAAAoo/OtFirAVq3uk/s400/3.png" alt="" id="BLOGGER_PHOTO_ID_5373163892696926050" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;From the above output you can find inode number of both files are equal.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now create a soft link named with "softlink.txt"&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFSC-MHz3I/AAAAAAAAAow/A-aOYc_6oNg/s1600-h/4.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 413px; height: 108px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFSC-MHz3I/AAAAAAAAAow/A-aOYc_6oNg/s400/4.png" alt="" id="BLOGGER_PHOTO_ID_5373166041436770162" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now you find that inode value is different for the soft link and main file.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;But equal for the hard link and main file.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Contents of all files before editing&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFVM_tMBkI/AAAAAAAAAo4/SNWiIMMNsSw/s1600-h/5.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 411px; height: 117px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFVM_tMBkI/AAAAAAAAAo4/SNWiIMMNsSw/s400/5.png" alt="" id="BLOGGER_PHOTO_ID_5373169512177468994" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now lets try to edit main file "mainfile.txt"&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFV5whvTvI/AAAAAAAAApA/33heJWoutrM/s1600-h/6.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 409px; height: 221px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFV5whvTvI/AAAAAAAAApA/33heJWoutrM/s400/6.png" alt="" id="BLOGGER_PHOTO_ID_5373170281197031154" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;From the above output it clarifies that, changes of main file reflects its soft and hard links.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);"&gt;Note: &lt;/span&gt;Permission changes of original file reflects only on hard links. Its soft links permission remains unchanged.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now lets remove main file "mainfile.txt"&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFXn368WCI/AAAAAAAAApI/tt0Fu1ckFE0/s1600-h/7.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 412px; height: 227px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFXn368WCI/AAAAAAAAApI/tt0Fu1ckFE0/s400/7.png" alt="" id="BLOGGER_PHOTO_ID_5373172172967401506" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;So removing the original file will affect the Soft link. The Soft link fails.Now it become a dangling symbolic link(or broken link).Hard link is unaffected.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now create main file "mainfile.txt" again and make its links as it is before it is&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;deleted.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;let now try to remove soft link "softlink.txt".&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFZY3UXUsI/AAAAAAAAApQ/IrTgNKkqruw/s1600-h/8.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 422px; height: 236px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFZY3UXUsI/AAAAAAAAApQ/IrTgNKkqruw/s400/8.png" alt="" id="BLOGGER_PHOTO_ID_5373174114130809538" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;It clarifies that deletion of soft link will not affect the main file and its hard links.&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now lets try to edit hard link "hardlink.tst".Before that create soft link "softlink.txt" again.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/SpFcFIe5QGI/AAAAAAAAApY/jLwBiaxYYPk/s1600-h/9.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 411px; height: 225px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/SpFcFIe5QGI/AAAAAAAAApY/jLwBiaxYYPk/s400/9.png" alt="" id="BLOGGER_PHOTO_ID_5373177073675878498" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;From the above output its clear that changing the contents of the hard link will reflects on main file and also reflect on soft link of main file.&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now lets try to edit the soft link "softlink.txt".&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFdiaV-aSI/AAAAAAAAApg/9ykUuvRBmH0/s1600-h/10.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 216px;" src="http://3.bp.blogspot.com/_ioCu0w5ZnYE/SpFdiaV-aSI/AAAAAAAAApg/9ykUuvRBmH0/s400/10.png" alt="" id="BLOGGER_PHOTO_ID_5373178676198140194" border="0" /&gt;&lt;/a&gt;&lt;p style="margin-bottom: 0in;"&gt;From the above output its clear that changing the contents of soft link will reflects on main file and also on all links of main file.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now lets try to remove main file then edit its soft link.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFgWF_y2sI/AAAAAAAAApo/9S2q2ZqrJ-g/s1600-h/11.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 249px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFgWF_y2sI/AAAAAAAAApo/9S2q2ZqrJ-g/s400/11.png" alt="" id="BLOGGER_PHOTO_ID_5373181763112852162" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Some strange result.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Soft link creates its main file.But it will not retain main file contents. It merely creates main file "mainfile.txt" with data what you inserted in "softlink.txt". &lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now hard link  "hardlink.txt" will not have any relation with main file "mainfile.txt".Both files now have unique inode value.&lt;/p&gt;&lt;p style="margin-bottom: 0in; font-weight: bold;"&gt;Now lets give a look on directories. How soft links and hard links behaves with directories:&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now create a directory named with temp and create some files in that directory.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Now try to create hard link and soft link for directory temp.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFji3dTFuI/AAAAAAAAApw/oWpneuSfIvw/s1600-h/12.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 249px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFji3dTFuI/AAAAAAAAApw/oWpneuSfIvw/s400/12.png" alt="" id="BLOGGER_PHOTO_ID_5373185281083250402" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Above example clarifies that it's not possible to create hard link on directory but it is possible to create soft link.&lt;br /&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in; font-weight: bold; color: rgb(0, 0, 0);"&gt;Note:&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;Like other files, a symbolic link has a separate directory entry with its own inode number. This means that &lt;span style="font-style: italic;"&gt;rm&lt;/span&gt; can remove a symbolic link even if its points to a directory(or even a device).&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;So lets try to remove symbolic link of a directory.In our above example "softtemp" is a symbolic link for directory temp.&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFzZCLyEKI/AAAAAAAAAp4/PPW3I01uX9A/s1600-h/12b.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 159px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFzZCLyEKI/AAAAAAAAAp4/PPW3I01uX9A/s400/12b.png" alt="" id="BLOGGER_PHOTO_ID_5373202704349925538" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;So it clears that we can remove directory symbolic link with &lt;span style="font-style: italic;"&gt;rm&lt;/span&gt; command just like a file. But don't add "/" at the end of link(If you use tab to complete file name at command prompt it automatically adds / at the end of link if it points to a directory).&lt;/p&gt;&lt;p style="margin-bottom: 0in; font-weight: bold;"&gt;Observations:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;You can link multiple files (i.e., create a link for each), but then the destination filename must be a directory.&lt;/li&gt;&lt;/ul&gt;&lt;p style="margin-bottom: 0in;"&gt;     Ex: ln chap?? project                      project is a directory&lt;/p&gt;&lt;ul&gt;&lt;li&gt;When you create hard link, The link count of file increases one. So based on link count you can find how many instances of a file exist in your system.&lt;/li&gt;&lt;li&gt;So a file is considered to be completely removed from the system when its link count drops to zero&lt;/li&gt;&lt;li&gt;Many UNIX/Linux commands are linked.&lt;/li&gt;&lt;/ul&gt;&lt;p style="margin-bottom: 0in;"&gt;       Ex:&lt;/p&gt;&lt;p style="margin-bottom: 0in;"&gt;       [root@localhost ~]# ls -li /usr/bin/gzip&lt;br /&gt;  499116 lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip -&gt;../../bin/gzip&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;If you want to create symbolic link of a file in other directory your source file path must be absolute.&lt;/li&gt;&lt;li&gt;You can identify symbolic links by the character "l" seen in the permission field.&lt;/li&gt;&lt;li&gt;Observe that the size of the symbolic link is equal to length of the path name it contains.&lt;/li&gt;&lt;li&gt;          &lt;style type="text/css"&gt;  &lt;!--   @page { size: 8.5in 11in; margin: 0.79in }   P { margin-bottom: 0.08in }  --&gt;  &lt;/style&gt;  &lt;p&gt;To view the permissions of the file or directory that the symbolic link references, use L option with ls -l (i.e. ls -lL )&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;               Ex: [root@localhost scripts]# ls -l /usr/bin/gzip&lt;br /&gt;                 lrwxrwxrwx 1 root root 14 2008-11-11 03:10 /usr/bin/gzip -&gt; ../../bin/gzip&lt;br /&gt;                [root@localhost scripts]# ls -lL /usr/bin/gzip&lt;br /&gt;                 -rwxr-xr-x 1 root root 64524 2008-02-22 02:55 /usr/bin/gzip&lt;br /&gt;&lt;br /&gt;    &lt;style type="text/css"&gt;  &lt;!--   @page { size: 8.5in 11in; margin: 0.79in }   P { margin-bottom: 0.08in }  --&gt;  &lt;/style&gt;  &lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-5042608092515725721?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/5042608092515725721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/08/what-is-difference-between-hard-link.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5042608092515725721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5042608092515725721'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/08/what-is-difference-between-hard-link.html' title='What is the difference between Hard Link and Soft Link 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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_ioCu0w5ZnYE/SpFPn25LwpI/AAAAAAAAAog/ub4ygfVCY9E/s72-c/2.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-6300526243122597584</id><published>2009-08-14T23:43:00.000-07:00</published><updated>2009-08-23T10:46:39.460-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>GCD of more than two numbers</title><content type='html'>#!/bin/bash&lt;br /&gt;#**********************************************************************************************&lt;br /&gt;# gcd.sh: greatest common divisor uses Eclidean algorithm&lt;br /&gt;# Usage : gcd.sh num1 num2 num3 .... (any number of arguments)&lt;br /&gt;# The algorithm used to calculate the GCD  two integers is known as the Euclidean algorithm.&lt;br /&gt;# Based on Euclidean algorithm this script is written(Recursive method).&lt;br /&gt;# For checking supplied arguments are integers or not check GCD of two numbers code&lt;br /&gt;#***********************************************************************************************&lt;br /&gt;&lt;br /&gt;# Argument check&lt;br /&gt;# Minimum 2 arguments you should to supply&lt;br /&gt;ARGS=2&lt;br /&gt;BADARGS=65&lt;br /&gt;&lt;br /&gt;if [ $# -lt "$ARGS" ]&lt;br /&gt;then&lt;br /&gt; echo&lt;br /&gt; echo  "Invalid Arguments"&lt;br /&gt; echo "Usage: $0 first-number second-number"&lt;br /&gt; echo&lt;br /&gt; exit $BADARGS&lt;br /&gt;fi&lt;br /&gt;# Preserve command line argument for future use&lt;br /&gt;cmdargs=$*&lt;br /&gt;&lt;br /&gt;function Euclidean()&lt;br /&gt;{&lt;br /&gt;if [ $2 -eq 0 ]&lt;br /&gt;then&lt;br /&gt;return $1&lt;br /&gt;else&lt;br /&gt;Euclidean $2 $(($1%$2))        # calling function recursively&lt;br /&gt;fi&lt;br /&gt;}&lt;br /&gt;Euclidean $1 $2&lt;br /&gt;return=$?&lt;br /&gt;# $? returns the exit status of script. This is one method to capture return value of a function&lt;br /&gt;&lt;br /&gt;shift&lt;br /&gt;# Shifts command line arguments one step.Now $1 holds second argument&lt;br /&gt;&lt;br /&gt;while true&lt;br /&gt;do&lt;br /&gt;shift&lt;br /&gt;# shift is used to pick up next command line argument to continue iteration&lt;br /&gt;# $# holds total number of arguments.At every shift operation its value decreases one&lt;br /&gt;&lt;br /&gt;if [ $# -eq 0 ]&lt;br /&gt;then&lt;br /&gt;break 2&lt;br /&gt;fi&lt;br /&gt;Euclidean $return $1&lt;br /&gt;return=$?&lt;br /&gt;done&lt;br /&gt;echo "GCD of $cmdargs is $return"&lt;br /&gt;exit 0&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6300526243122597584?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6300526243122597584/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/08/gcd-of-more-than-two-numbers.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6300526243122597584'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6300526243122597584'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/08/gcd-of-more-than-two-numbers.html' title='GCD of more than two numbers'/><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-7741835633843817183</id><published>2009-08-13T10:35:00.000-07:00</published><updated>2009-08-23T10:47:18.739-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>GCD of two nubers</title><content type='html'>I written three different methods to calculate GCD of two numbers&lt;br /&gt;&lt;h1&gt;Method 1: &lt;/h1&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#*******************************************************************************************&lt;br /&gt;# gcd.sh: greatest common divisor uses Eclidean algorithm&lt;br /&gt;# Usage : gcd.sh number1 number2&lt;br /&gt;# The algorithm used to calculate the GCD between two integers is known as the Euclidean&lt;br /&gt;# algorithm(Recursive method).&lt;br /&gt;#*******************************************************************************************&lt;br /&gt;&lt;br /&gt;#----------------------------------------------------------------------&lt;br /&gt;# Argument check&lt;br /&gt;ARGS=2&lt;br /&gt;BADARGS=65&lt;br /&gt;&lt;br /&gt;if [ $# -ne "$ARGS" ]&lt;br /&gt;then&lt;br /&gt; echo ;  echo  "Invalid Arguments"&lt;br /&gt; echo "Usage: $0 first-number second-number" ;  echo&lt;br /&gt; exit $BADARGS&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# Check supplied arguments are integers are not&lt;br /&gt;&lt;br /&gt;isnumber()&lt;br /&gt;{&lt;br /&gt;if [ $1 -eq $1 2&gt; /dev/null ]&lt;br /&gt;then&lt;br /&gt;:&lt;br /&gt;# : is a null command.This is the shell equivalent of a "NOP"(do nothing)&lt;br /&gt;else&lt;br /&gt;echo -e "\nYou supplied one bad argument \"$1\" is not a number"&lt;br /&gt;echo -e "Usage: $0 first-number second-number\n"&lt;br /&gt;exit $BADARGS&lt;br /&gt;fi&lt;br /&gt;}&lt;br /&gt;isnumber $1&lt;br /&gt;isnumber $2&lt;br /&gt;&lt;br /&gt;# ---------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;function Euclidean()&lt;br /&gt;{&lt;br /&gt;if [ $2 -eq 0 ]&lt;br /&gt;then&lt;br /&gt;&lt;br /&gt;return $1&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;&lt;br /&gt;Euclidean $2 $(($1%$2))    &lt;br /&gt;# calling function recursively&lt;br /&gt;fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Euclidean $1 $2&lt;br /&gt;&lt;br /&gt;echo "gcd of $1 and $2 is $?"&lt;br /&gt;&lt;br /&gt;# $? returns the exit status of script. This is one method to capture return value of a function&lt;br /&gt;&lt;br /&gt;exit 0&lt;br /&gt;&lt;h1&gt;Method 2:&lt;/h1&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# gcd2.sh&lt;br /&gt;# Usage: gcd2.sh number1 number2&lt;br /&gt;# For argument check see method 1&lt;br /&gt;&lt;br /&gt;gcd ()&lt;br /&gt;{&lt;br /&gt; dividend=$1 &lt;br /&gt; divisor=$2           &lt;br /&gt;#  Arbitrary assignment.&lt;br /&gt;#! It doesn't matter which of the two is larger.&lt;br /&gt;&lt;br /&gt;  remainder=1  &lt;br /&gt;&lt;br /&gt;#  If uninitialized variable used in loop,it results in an error message&lt;br /&gt;#  on the first pass through loop.&lt;br /&gt;&lt;br /&gt; if [ $divisor -eq 0 ]&lt;br /&gt; then&lt;br /&gt;    echo "GCD of $dividend and $divisor = $dividend"&lt;br /&gt;    exit 0&lt;br /&gt; fi&lt;br /&gt;&lt;br /&gt; until [ "$remainder" -eq 0 ]&lt;br /&gt; do&lt;br /&gt;   let "remainder = $dividend % $divisor"&lt;br /&gt;&lt;br /&gt;    # Now repeat with 2 smallest numbers&lt;br /&gt;&lt;br /&gt;   dividend=$divisor    .&lt;br /&gt;   divisor=$remainder&lt;br /&gt; done                   &lt;br /&gt;}                        &lt;br /&gt;# Last $dividend is the gcd.&lt;br /&gt;&lt;br /&gt;gcd $1 $2&lt;br /&gt;&lt;br /&gt;echo; echo "GCD of $1 and $2 = $dividend"; echo&lt;br /&gt;exit 0&lt;br /&gt;&lt;h1&gt;Method 3:&lt;/h1&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# gcd3.sh&lt;br /&gt;# Usage: gcd3.sh&lt;br /&gt;# This script doesn't use command line arguments.You should to enter two numbers when asking&lt;br /&gt;&lt;br /&gt;echo enter two numbers&lt;br /&gt;read n1  n2&lt;br /&gt;remainder=1&lt;br /&gt;# Preserve numbers for future use&lt;br /&gt;t1=$n1&lt;br /&gt;t2=$n2&lt;br /&gt;if [ $n2 -eq 0 ]&lt;br /&gt;then&lt;br /&gt;  echo "GCD of $n1 and $n2 = $n1"&lt;br /&gt;  exit 0&lt;br /&gt;fi&lt;br /&gt;while [ $remainder -ne 0 ]&lt;br /&gt;do&lt;br /&gt;remainder=`expr $n1 % $n2`&lt;br /&gt;# or use&lt;br /&gt;#remainder=$((n1%n2))&lt;br /&gt;n1=$n2&lt;br /&gt;n2=$remainder&lt;br /&gt;done&lt;br /&gt;echo "GCD of $t1 , $t2  is  $n1"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7741835633843817183?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7741835633843817183/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/08/gcd-of-two-nubers.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7741835633843817183'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7741835633843817183'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/08/gcd-of-two-nubers.html' title='GCD of two nubers'/><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-6182965569831581488</id><published>2009-08-10T08:57:00.000-07:00</published><updated>2009-08-23T10:47:47.437-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Terminal Lock Program</title><content type='html'>#************************************************************************************&lt;br /&gt;#Locking a terminal using a shell script.                                                &lt;br /&gt;#This script locks your terminal until you enter correct password        (open2world)&lt;br /&gt;#It also traps signals and interrupts &lt;br /&gt;#You can't terminate the script by Ctrl+c or Ctrl+\ or Ctrl+z and Ctrl+d                                                   &lt;br /&gt;#************************************************************************************&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;trap "" 1 2 3 20&lt;br /&gt;lines=`&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;tput&lt;/span&gt; lines`&lt;br /&gt;b=`&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;expr&lt;/span&gt; $lines / 2 - 4 `&lt;br /&gt;&lt;br /&gt;##   center function&lt;br /&gt;&lt;br /&gt;center()&lt;br /&gt;{&lt;br /&gt;columns=`&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;tput&lt;/span&gt; cols`&lt;br /&gt;until [ -z "$1" ]&lt;br /&gt;do&lt;br /&gt;for ((i=1;i&lt;$(((columns-${#1})/2));i++)) do          echo -n " " done echo -e "\033[1m \033[5m \033[42m $1 \033[0m  " shift done }  ##    End of center function  while true do clear &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;tput&lt;/span&gt; cup $b 0&lt;br /&gt;center "    TERMINAL LOCKED    "&lt;br /&gt;center "Press any key to unlock"&lt;br /&gt;read key&lt;br /&gt;echo -n "enter password : "&lt;br /&gt;read -s password&lt;br /&gt;while true&lt;br /&gt;do&lt;br /&gt;if [ $password = "open2world" ]&lt;br /&gt;then&lt;br /&gt;clear&lt;br /&gt;break 2&lt;br /&gt;# breaks second outer loop&lt;br /&gt;else&lt;br /&gt;echo -e "\n You are an illegal user "&lt;br /&gt;echo -e "\n Enter any key"&lt;br /&gt;read&lt;br /&gt;break&lt;br /&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_4"&gt;fi&lt;/span&gt;&lt;br /&gt;done&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;h1&gt; Analysis : &lt;/h1&gt;&lt;br /&gt; The trap command allows you to execute a command when a signal is received by your script. It works like this:&lt;br /&gt;&lt;br /&gt;trap &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;arg&lt;/span&gt; signals&lt;br /&gt;&lt;br /&gt;"signals" is a list of signals to interrupt and  "&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_6"&gt;arg&lt;/span&gt;" is a command to execute when one of the signals is received. If "&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_7"&gt;arg&lt;/span&gt;" is not supplied script doesn't do any thing after receiving signal.&lt;br /&gt;&lt;br /&gt;Note: Two signals "SIGKILL" "SIGSTOP" are unable to trap&lt;br /&gt;&lt;br /&gt;Ex: trap "echo signal received" 1 2 3 15&lt;br /&gt;(or use &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_8"&gt;SIGHUP&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_9"&gt;SIGINT&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_10"&gt;SIGQUIT&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_11"&gt;SIGTERM&lt;/span&gt; instead of 1 2 3 15 )&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;What is signal: &lt;/h2&gt;&lt;br /&gt;A &lt;i&gt;signal&lt;/i&gt; is a message which can be sent to a running process. Some times called&lt;br /&gt;software interrupts.&lt;br /&gt;&lt;br /&gt;Some of signals and its value:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;       Signal             Value     Action   Comment&lt;br /&gt;     -------------------------------------------------------------------------&lt;br /&gt;     SIGHUP                    1         Term    Hangup detected on controlling terminal&lt;br /&gt;                                                 or death of controlling process&lt;br /&gt;     SIGINT (Ctrl+c)           2         Term    Interrupt from keyboard&lt;br /&gt;     SIGQUIT (Ctrl+\)          3         Core    Quit from keyboard&lt;br /&gt;     SIGTERM                   15        Term    Termination signal&lt;br /&gt;     SIGTSTP (Ctrl+z)          20        Stop    Stop typed at tty&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note: There are different standards(POSIX,SUSv) to determine signals and its values.&lt;br /&gt;     I tested Above signals in Fedora 9, Other systems may support them or not depending on system standard(I think almost all standards supports above signals and values).&lt;br /&gt;&lt;h2&gt;More stuff: &lt;/h2&gt;&lt;br /&gt;        I used "tput cols" and "tput lines" commands to collect number of columns and lines.&lt;br /&gt;     You can also directly use bash variables $LINES ,$COLUMNS to get columns and lines.But&lt;br /&gt;     you should to run your script in current shell only. If you run your script in sub shell&lt;br /&gt;     current shell will not export $LINES and $COLUMNS variable values.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6182965569831581488?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6182965569831581488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2009/08/terminal-lock-program.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6182965569831581488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6182965569831581488'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2009/08/terminal-lock-program.html' title='Terminal Lock Program'/><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-6413423356912146397</id><published>2008-06-21T00:44:00.000-07:00</published><updated>2009-08-23T10:48:18.291-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Bash variables scope</title><content type='html'>&lt;p style="color: rgb(51, 204, 0); font-weight: bold;"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;Bash&lt;/span&gt; &lt;span style="color: rgb(51, 204, 0);"&gt;variables are defaults to global&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style="color: rgb(51, 255, 51);"&gt;&lt;span style="color: rgb(255, 255, 51);"&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;What&lt;/span&gt; &lt;span style="color: rgb(51, 204, 0);"&gt;makes a variable local?&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;span style="color: rgb(255, 255, 51);"&gt;&lt;ul style="color: rgb(204, 51, 204);"&gt;&lt;li&gt;&lt;span style="color: rgb(255, 204, 255);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;A variable declared as local is one that is visible only within the block of code in which it appears. It has local "scope." In a function, a local variable has meaning only within that function block.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Ex:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;local variable name &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;func ()&lt;br /&gt;{&lt;br /&gt;local loc_var=23&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(204, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Declared&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;as local variable&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Uses the 'local' builtin&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;echo "\"loc_var\" in function = $loc_var"&lt;br /&gt;global_var=999 &lt;/p&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Not declared as local.&lt;br /&gt;# Defaults to global.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "\"global_var\" in function = $global_var"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;func&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Now, to see if local variable "loc_var" exists outside function.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "\"loc_var\" outside function = $loc_var"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# $loc_var outside function =&lt;br /&gt;# No, $loc_var not visible globally&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "\"global_var\" outside function = $global_var"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# $global_var outside function = 999&lt;br /&gt;# $global_var is visible globally&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;exit 0&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="color: rgb(255, 204, 255);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Before a function is called, all variables declared within the function are invisible outside the body of the function, not just those explicitly declared as local&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;func ()&lt;br /&gt;{&lt;br /&gt;global_var=37&lt;/p&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;# Visible only within the function block&lt;br /&gt;# before the function has been called.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;} &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# END OF FUNCTION&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "global_var = $global_var"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# global_var =&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Function "func" has not yet been called,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# so $global_var is not visible here.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;func&lt;/span&gt;&lt;br /&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;echo "global_var = $global_var"&lt;br /&gt;&lt;span style="color: rgb(204, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# global_var = 37 Has been set by function call.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style="color: rgb(0, 0, 0);"&gt;&lt;li&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Using the declare builtin restricts the scope of a variable&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;foo ()&lt;br /&gt;{&lt;br /&gt;FOO="bar"&lt;br /&gt;} &lt;/p&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;bar ()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;foo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo $FOO&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;bar&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; # Prints bar&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 204, 255);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;However . . &lt;/span&gt;.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;foo (){&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;declare FOO="bar"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;bar ()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;foo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo $FOO&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;bar&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; # Prints nothing.&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6413423356912146397?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6413423356912146397/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/06/bash-variables-scope.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6413423356912146397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6413423356912146397'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/06/bash-variables-scope.html' title='Bash variables scope'/><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-2171354822645190837</id><published>2008-06-21T00:24:00.000-07:00</published><updated>2009-08-23T10:48:45.934-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Bash variables type</title><content type='html'>&lt;a style="color: rgb(204, 51, 204);" name="INTORSTRING"&gt;&lt;span style="color: rgb(255, 255, 51);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Unlike many other programming languages, Bash does not separate its variables by "type". Essentially, Bash variables are character strings, but, depending on context, Bash permits integer operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits&lt;/span&gt;.&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 255, 51);"&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;Integer or string?&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a=2334                &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Integer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;let "a += 1"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "a = $a "    &lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;  # a = 2335&lt;/span&gt;&lt;br /&gt;                 &lt;span style="color: rgb(0, 0, 0);"&gt; # Integer, still&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;b=${a/23/BB}         &lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Substitute "BB" for "23".&lt;br /&gt;                                   # This transforms $b into a string&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "b = $b"             &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# b = BB35&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;declare -i b            &lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 0);"&gt;# Declaring it an integer doesn't help&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "b = $b"         &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  # b = BB35&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;let "b += 1"    &lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(255, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# BB35 + 1 =&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;echo "b = $b"&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 0);"&gt;# b = 1&lt;/span&gt;&lt;br /&gt;c=BB34&lt;br /&gt;echo "c = $c"&lt;br /&gt;          &lt;span style="color: rgb(0, 0, 0);"&gt;# c = BB34&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;d=${c/BB/23}  &lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 0);"&gt;  # Substitute "23" for "BB".&lt;br /&gt;                                    # This makes $d an integer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "d = $d" &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;             #&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; d = 2334&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;let "d += 1"   &lt;/span&gt;&lt;br /&gt;         &lt;span style="color: rgb(0, 0, 0);"&gt;   # 2334 + 1 =&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "d = $d" &lt;/span&gt;&lt;br /&gt;         &lt;span style="color: rgb(0, 0, 0);"&gt; # d = 2335&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 51);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;# What about null variables?&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;e=""&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "e = $e"           &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# e =&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;let "e += 1"            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Arithmetic operations allowed on a null variable&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;?&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "e = $e"        &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;  # e = 1&lt;br /&gt;&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0);"&gt;#                                   Null variable transformed into an integer&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 255, 51);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;# What about undeclared variables?&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "f = $f"           &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# f =&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;let "f += 1"           &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Arithmetic operations allowed?&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo "f = $f"       &lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 0);"&gt; # f = 1&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 204, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Undeclared variable transformed into an integer.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;The burden is on the programmer to keep track of what type the script variables are.&lt;br /&gt;Bash will not do it for you.&lt;br /&gt;But “declare” or “typeset “built-ins permit restring the properties of variables.&lt;br /&gt;This is very weak form of the typing available in certain programming languages&lt;/span&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Ex:1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;declare –i number&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# The script will treat subsequent occurrences of “number” as an integer&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;number=3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo “Number = $number”    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;# Number = 3&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;number=three&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;echo “Number = $number”    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; #Number = 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;#Tries to evaluate the string “three” as an integer&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-2171354822645190837?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2171354822645190837/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/06/bash-variables-type.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2171354822645190837'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2171354822645190837'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/06/bash-variables-type.html' title='Bash variables type'/><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-2498136493713795083</id><published>2008-06-06T07:56:00.000-07:00</published><updated>2009-08-23T10:49:16.926-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Backup files to tarred and zip file</title><content type='html'>#!bin/bash&lt;br /&gt;&lt;br /&gt;# backs up all files in current directory modified within last 24 hours&lt;br /&gt;# in a tarred and zipped file&lt;br /&gt;# Replace 1 with how many day's you want to back up files&lt;br /&gt;&lt;br /&gt;BACKUPFILE=backup-`date +"%m-%d-%Y"`&lt;br /&gt;# Embeds date in backup filename&lt;br /&gt;&lt;br /&gt;archive=${1:-$BACKUPFILE}&lt;br /&gt;#If no filename specified default to backup-MM-DD-YYYY&lt;br /&gt;&lt;br /&gt;find . -mtime -1 -type f -print0  xargs -0 tar rvf "$archive.tar"&lt;br /&gt;&lt;br /&gt;#check bellow command&lt;br /&gt;# tar cvf - $(find . -mtime -1 -type f -print) &gt; $archive.tar&lt;br /&gt;# It works But  will fail to backup file names contain space&lt;br /&gt;# if there is no files containing spaces it is good&lt;br /&gt;&lt;br /&gt;#             ALSO USE bellow code but it is slow and not portable&lt;br /&gt;# find . -mtime -1 -type f -exec tar rvf "$archive.tar" {} \;&lt;br /&gt;# use rvf option instead of cvf otherwise only one file will be archived&lt;br /&gt;&lt;br /&gt;gzip $archive.tar &amp;amp;&amp;amp; echo "Directory $pwd backed up in \"$archive.tar.gz\" File"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-2498136493713795083?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/2498136493713795083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/06/backup-files-to-tarred-and-zip-file.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2498136493713795083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/2498136493713795083'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/06/backup-files-to-tarred-and-zip-file.html' title='Backup files to tarred and zip file'/><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-6728557372476275650</id><published>2008-06-04T06:11:00.000-07:00</published><updated>2009-08-23T10:49:54.544-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Traversing directory using depth first search</title><content type='html'>&lt;div align="left"&gt;#!/bin/bash&lt;br /&gt;# Traverse a directory using depth first traversal technique&lt;br /&gt;# Usage $0 directorypath&lt;br /&gt;# otherwise it takes current working directory as directory path&lt;br /&gt;&lt;br /&gt;depth()&lt;br /&gt;{&lt;br /&gt;#Do a small depth checking how deep into the tree we are&lt;br /&gt;          k=0&lt;br /&gt;          while [ $k -lt $1 ]&lt;br /&gt;          do&lt;br /&gt;             echo -n "  "&lt;br /&gt;             let k++&lt;br /&gt;             #or use k=`expr $k + 1`&lt;br /&gt;          done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;traverse()&lt;br /&gt;{&lt;br /&gt;# Traverse a directory&lt;br /&gt;&lt;br /&gt;ls "$1"while read i&lt;br /&gt;do&lt;br /&gt;    depth $2&lt;br /&gt;    if [ -d "$1/$i" ]&lt;br /&gt;    then&lt;br /&gt;           echo Directory: $1/$i &lt;/div&gt;&lt;div align="left"&gt;            traverse "$1/$i" `expr $2 + 1`&lt;br /&gt;           # Calling this as a subshell means that when the called&lt;br /&gt;           # function changes directory, it will not affect our&lt;br /&gt;           # current working directory&lt;br /&gt;           # If you call this in current shell it gives the error&lt;br /&gt;           # bash: traverse: No such file or directory after changing&lt;br /&gt;           # the current directory&lt;br /&gt;                else&lt;br /&gt;&lt;br /&gt;         echo File: $1/$i&lt;br /&gt;     fi&lt;br /&gt;done&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;# $1 is directory path&lt;br /&gt;&lt;br /&gt;if [ -z "$1" ]&lt;br /&gt;then&lt;br /&gt;# Here we are giving '0' is the current depth of direcory&lt;br /&gt;      traverse . 0&lt;br /&gt;else&lt;br /&gt;      traverse $1 0&lt;br /&gt;fi&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-6728557372476275650?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/6728557372476275650/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/06/traversing-directory-using-depth-first.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6728557372476275650'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/6728557372476275650'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/06/traversing-directory-using-depth-first.html' title='Traversing directory using depth first search'/><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-8849321552034060823</id><published>2008-04-25T22:02:00.000-07:00</published><updated>2011-01-03T13:35:25.093-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Shell Scripts'/><title type='text'>Displaying chess board on 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;h5&gt;Small Chess Board&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# SCRIPT  : smallchessboard.sh&lt;br /&gt;# PURPOSE : Prints small chess board on the screen.&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;for (( i=1 ; i&lt;=8 ; i++ ))&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;  for (( j=1 ; j&lt;=8 ; j++ ))&lt;br /&gt;  do&lt;br /&gt;&lt;br /&gt;    if [ `expr $(($i+$j)) % 2` -eq 0 ]&lt;br /&gt;    then&lt;br /&gt;        echo -e -n "\033[47m  "         # White background&lt;br /&gt;    else&lt;br /&gt;        echo -e -n "\033[40m  "         # Black background&lt;br /&gt;    fi&lt;br /&gt;&lt;br /&gt;  done&lt;br /&gt;  echo                                  # move to next line&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo -e "\033[0m"                       # Restores color settings.&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;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSI_rE5cfzI/AAAAAAAAAts/7mJQUifu4Do/s1600/smallboard.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 194px; height: 200px;" src="http://1.bp.blogspot.com/_ioCu0w5ZnYE/TSI_rE5cfzI/AAAAAAAAAts/7mJQUifu4Do/s200/smallboard.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5558074899410222898" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h5&gt;Big Chess Board&lt;/h5&gt;&lt;br /&gt;&lt;div class="codeblock"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# SCRIPT  : bigchessboard.sh&lt;br /&gt;# PURPOSE : Prints big chess board on the screen.&lt;br /&gt;&lt;br /&gt;clear&lt;br /&gt;a=4&lt;br /&gt;&lt;br /&gt;for (( i=1 ; i&lt;=8; i++ ))&lt;br /&gt;do&lt;br /&gt;&lt;br /&gt;   for (( j=1 ;j&lt;=2; j++ ))            # prints same line twice&lt;br /&gt;   do&lt;br /&gt;      tput cup $a 15                   # moves cursor to LINE COLUMN&lt;br /&gt;&lt;br /&gt;     for (( k=1 ; k&lt;=8; k++ ))&lt;br /&gt;     do&lt;br /&gt;&lt;br /&gt;        c=`expr $((i+k)) %  2`&lt;br /&gt;&lt;br /&gt;        if [ $c -eq 0 ]&lt;br /&gt;        then&lt;br /&gt;             echo -e -n "\033[40m    "   # Black background&lt;br /&gt;        else&lt;br /&gt;             echo -e -n "\033[47m    "   # White background&lt;br /&gt;        fi&lt;br /&gt;&lt;br /&gt;      done&lt;br /&gt;      let a=a+1&lt;br /&gt;&lt;br /&gt;    done&lt;br /&gt;&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo -e "\033[0m"                         # Restores color settings&lt;br /&gt;read key                                  # Waits for enter&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;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_ioCu0w5ZnYE/TSJAPzTWVCI/AAAAAAAAAt0/K9nw6sEDaLs/s1600/bigboard.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 360px;" src="http://2.bp.blogspot.com/_ioCu0w5ZnYE/TSJAPzTWVCI/AAAAAAAAAt0/K9nw6sEDaLs/s400/bigboard.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5558075530342192162" /&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-8849321552034060823?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/8849321552034060823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/04/displaying-chess-board-on-screen.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8849321552034060823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/8849321552034060823'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/04/displaying-chess-board-on-screen.html' title='Displaying chess board on 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/TSI_rE5cfzI/AAAAAAAAAts/7mJQUifu4Do/s72-c/smallboard.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-7268907890664115003</id><published>2008-04-01T22:51:00.000-07:00</published><updated>2009-07-25T08:38:42.845-07:00</updated><title type='text'>How can get nth line from a file</title><content type='html'>&lt;ol&gt;&lt;li&gt;&lt;span style="color: rgb(255, 255, 0);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;time head -5 emp.lst tail -1&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;It has taken time for execution is&lt;br /&gt;real 0m0.004s&lt;br /&gt;user 0m0.001s&lt;br /&gt;sys 0m0.001s&lt;br /&gt;&lt;span style="color: rgb(153, 255, 153);"&gt;or&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;awk 'NR==5' emp.lst&lt;/span&gt;&lt;br /&gt;It has taken time for execution is&lt;br /&gt;real 0m0.003s&lt;br /&gt;user 0m0.000s&lt;br /&gt;sys 0m0.002s&lt;br /&gt;&lt;span style="color: rgb(102, 255, 153);"&gt;or &lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;sed -n '5p' emp.lst&lt;/span&gt;&lt;br /&gt;It has taken time for execution is&lt;br /&gt;real 0m0.001s&lt;br /&gt;user 0m0.000s&lt;br /&gt;sys 0m0.001s&lt;br /&gt;&lt;span style="color: rgb(102, 255, 153);"&gt;or&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="color: rgb(255, 255, 0);"&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;using some cute trick we can get this with cut command&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;cut -d “&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 204, 0);"&gt;“ -f 5 emp.lst&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;# after -d press enter ,it means delimiter is newline&lt;br /&gt;It has taken time for execution is&lt;br /&gt;real 0m0.001s&lt;br /&gt;user 0m0.000s&lt;br /&gt;sys 0m0.001s&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 255, 153);"&gt;Analysis&lt;/span&gt;: comparing above commands 'head' command taken maximum time&lt;br /&gt;because it pipes the output to tail command. piping consumes some&lt;br /&gt;time.&lt;br /&gt;Next “awk” command has taken greater time ,because 'awk' not only&lt;br /&gt;a command, it is a programing language too.&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-7268907890664115003?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/7268907890664115003/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/04/how-can-get-nth-line-from-file.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7268907890664115003'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/7268907890664115003'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/04/how-can-get-nth-line-from-file.html' title='How can get nth line from a file'/><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-5015405909322447343</id><published>2008-04-01T01:50:00.000-07:00</published><updated>2009-08-23T10:51:11.921-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>When not to use shell scripts</title><content type='html'>· Resource-intensive tasks, especially where speed is a factor (sorting, hashing,&lt;br /&gt;etc.)Procedures involving heavy-duty math operations, especially floating&lt;br /&gt;point arithmetic, arbitraryprecision calculations, or complex numbers&lt;br /&gt;(use C++ or FORTRAN instead)&lt;br /&gt;&lt;br /&gt;· Cross-platform portability required (use C or Java instead) Complex applications, where structured programming is a necessity (need type-checking of variables, function prototypes, etc.)&lt;br /&gt;&lt;br /&gt;· Project consists of subcomponents with interlocking dependencies Extensive file operations required (Bash is limited to serial file access, and that only in a particularly clumsy and inefficient line-by-line fashion)&lt;br /&gt;&lt;br /&gt;· Need native support for multi-dimensional arrays&lt;br /&gt;· Need data structures, such as linked lists or trees&lt;br /&gt;· Need to generate or manipulate graphics or GUIs&lt;br /&gt;· Need direct access to system hardware&lt;br /&gt;· Need port or socket I/O&lt;br /&gt;· Need to use libraries or interface with legacy code&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-5015405909322447343?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/5015405909322447343/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/04/when-not-to-use-shell-scripts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5015405909322447343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/5015405909322447343'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/04/when-not-to-use-shell-scripts.html' title='When not to use 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>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8514572398266969701.post-1201679524995556417</id><published>2008-03-31T22:57:00.000-07:00</published><updated>2009-08-23T10:51:54.316-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Article'/><title type='text'>Why shell(bash) programming</title><content type='html'>&lt;span style="color: rgb(51, 204, 255);"&gt;&lt;em&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;No&lt;/span&gt; &lt;span style="color: rgb(204, 51, 204);"&gt;programming language is perfect. There is not even a single best language; there areOnly &lt;/span&gt;&lt;/em&gt;&lt;em style="color: rgb(204, 51, 204);"&gt;languages well suited or perhaps poorly suited for particular purposes. &lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;em&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;                                                                                                &lt;span style="color: rgb(204, 51, 204);"&gt;--&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&lt;span style="color: rgb(51, 204, 255);"&gt;  &lt;span style="color: rgb(204, 51, 204);"&gt;UNIX is simple. But It just needs a genius to understand its simplicity.&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 51, 204);"&gt;&lt;em&gt;                                                                                              --Dennis Ritchie&lt;/em&gt; &lt;/span&gt;&lt;br /&gt;                  We will be using Bash, an acronym for "Bourne--Again shell" developed by Brian Fox and    Chet Ramey. Bash has become a de facto standard for shell scripting on all flavors of UNIX. Bash is the standard GNU shell, intuitive and flexible. Probably most advisable for beginning users while being at the same time a powerful tool for the advanced and professional user. On Linux, bash is the standard shell for common users. This shell is a so-called superset of the Bourne shell, a set of add-ons and plug-ins. This means that the Bourne Again shell is compatible with the Bourne shell. commands that work in sh, also work in bash. However, the reverse is not always the case. Bash is the POSIX compliant.&lt;br /&gt;&lt;br /&gt;                     Shell is a command language interpreter that executes commands read from the standardInput device(key board) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc. Normally shells are interactive. An interactive shell generally reads from, and writes to, a user's terminal: input and output are connected to a terminal.&lt;br /&gt;&lt;br /&gt;                  The UNIX shell program interprets user commands, which are either directly entered by the user, or which can be read from a file called the shell script or shell program. Shell scripts are interpreted, not compiled.Writing shell script is not hard to learn. The syntax is simple and straightforward, similar to that of invoking and chaining together utilities at the command line, and there are only a few rules to learn. Most short scripts work right the first time, and debugging even the longer ones is straightforward. Shell script is just like batch file in MS-DOS but have more power than the MS-DOS batch file. Shell scripts useful to create our own commands that can save our lots of time and to automate some task of day today life. Using shell scripts we can automate system administrative tasks it saves lot of time&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8514572398266969701-1201679524995556417?l=www.bashguru.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.bashguru.com/feeds/1201679524995556417/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.bashguru.com/2008/03/why-shellbash-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1201679524995556417'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8514572398266969701/posts/default/1201679524995556417'/><link rel='alternate' type='text/html' href='http://www.bashguru.com/2008/03/why-shellbash-programming.html' title='Why shell(bash) programming'/><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></feed>
