Menu Driven Shell Script

in Categories Decision Making last updated April 5, 2008

Explains how to write a menu driven Shell script, which has following options:
1. Contents of /etc/passwd
2. List of users currently logged
3. Present handling directory (working directory)
4. Exit

Take action as per selected option

Shell Script to read any year and find whether it is a leap year or not

in Categories Time and Date last updated April 5, 2008

A leap year comes once every four years. It is the year when an extra day is added to the Gregorian calendar used by most of the world.

An ordinary year has 365 days. A leap year has 366 days. The extra day is added to the month of February. In an ordinary year, February has 28 days. In a leap year, it has 29 days. This extra day is called a leap day.

How do I find out leap year?

A year is a leap year if it can be evenly divided by four. For example, 1996 was a leap year. But a year is not a leap year if can be evenly divided by 100 and not by 400. This is why 1700, 1800, 1900 were not leap years, but 2000 was.

Shell script to determine if entered year is leap or not…

Shell script to read a character (upper or lower), digit, special symbol and display message according to the character entered

in Categories Decision Making last updated April 5, 2008

This script uses sed command to find out all digits or upper characters. -z string (conditional expression) will return true if the length of string is zero.

Script logic

Read one character
[1] Use sed command to determine input character is digit or not (sed -e ‘s/[A-Z]//g’))

[2] Attempt to match regex [0-9] i.e. all digit against the pattern space

[3] If successful, replace that portion matched with replacement // i.e. do nothing.
If digits are in input, it will replace all those digit, and if input is only digit, nothing is left i.e. string is zero and that is what tasted with -z conditional expression.

Shell Script to find the validity of a given date

in Categories Time and Date last updated April 5, 2008

Date is entered in mm/dd/yyyy format using a keyboard. Various conditions are applied to make sure date is valid. Read the program comments to get idea about program logic.

Shell Script to find out whether file has read, write and execute permission

in Categories File-management last updated April 5, 2008

There are three specific permissions on Unix-like (including Linux / Mac OS x) systems that apply to each user or class:
=> The read permission (r), which grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory
=> The write permission (w), which grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
=> The execute permission (x), which grants the ability to execute a file. This permission must be set for executable binaries in order to allow the operating system to run them. When set for a directory, this permission grants the ability to traverse its tree in order to access files or subdirectories, but not see files inside the directory (unless read is set).

SHELL CONDITIONAL EXPRESSIONS

You can use conditional expressions to find out file permissions. These are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

Conditional ExpressionMeaning
-a file True if file exists.
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-h file True if file exists and is a symbolic link.
-k file True if file exists and its “sticky” bit is set.
-p file True if file exists and is a named pipe (FIFO).
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-t fd True if file descriptor fd is open and refers to a terminal.
-u file True if file exists and its set-user-id bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user id.
-G file True if file exists and is owned by the effective group id.
-L file True if file exists and is a symbolic link.
-S file True if file exists and is a socket.
-N file True if file exists and has been modified since it was last read.

Sample Shell Script

Shell script to read a number and find whether the number is odd or even

in Categories Decision Making last updated April 5, 2008

Shell Script to read source file and copy it to target file

in Categories Decision Making last updated April 5, 2008

Shell script to read 5 digit number and calculate the sum of digit

in Categories Shell Math last updated April 4, 2008

Shell Script Calulate Area and Circumference of Circle

in Categories Shell Math last updated April 4, 2008

A circle is one of the simple shapes of Euclidean geometry. It is the locus of all points in a plane at a constant distance, called the radius, from a fixed point, called the center. Through any three points not on the same line, there passes one and only one circle.

A diameter is a chord passing through the center. The length of a diameter is twice the radius. A diameter is the largest chord in a circle.

Circles are simple closed curves which divide the plane into an interior and an exterior. The circumference of a circle is the perimeter of the circle, and the interior of the circle is called a disk.

Sample Shell Script To Calculate Area and Circumference of Circle