Experiment 1 and 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

13 To display system date and Time date

To display current month. date +%m

date To display the name of the date +%h


current month.

INTRODUCTION TO SHELL PROGRAMMING

A Shell provides you with an interface to the Unix system. It gathers input from you and
executes programs based on that input. When a program finishes executing, it displays that
program's output.
Shell is an environment in which we can run our commands, programs, and shell scripts.
There are different flavors of a shell, just as there are different flavors of operating systems.
Each flavor of shell has its own set of recognized commands and functions.

Shell Prompt
The prompt, $, which is called the command prompt, is issued by the shell. While the
prompt is displayed, you can type a command.
Shell reads your input after you press Enter. It determines the command you want executed
by looking at the first word of your input. A word is an unbroken set of characters. Spaces
and tabs separate words.
Following is a simple example of the date command, which displays the current
date and time −
$date
Thu Jun 25 08:30:19 MST 2009

You can customize your command prompt using the environment variable PS1 explained in
the Environment tutorial.

Shell Types

In Unix, there are two major types of shells −

● Bourne shell − If you are using a Bourne-type shell, the $ character is


the default prompt.

● C shell − If you are using a C-type shell, the % character is the


default prompt.

The Bourne Shell has the following subcategories −

● Bourne shell (sh)

● Korn shell (ksh)

● Bourne Again shell (bash)

● POSIX shell (sh)

The different C-type shells follow −

● C shell (csh)

● TENEX/TOPS C shell (tcsh)

The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at
the AT&T Bell Labs in New Jersey.
Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell".
Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the
shell of choice for writing scripts that can be used on different versions of Unix.
In this chapter, we are going to cover most of the Shell concepts that are based on the Borne
Shell.

Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of
execution. A good shell script will have comments, preceded by # sign, describing the steps.
There are conditional tests, such as value A is greater than value B, loops allowing us to go
through massive amounts of data, files to read and store data, and variables to read and store
data, and the script may include functions

In Unix-like operating systems, the chmod command is used to change the access
mode of a file. The name is an abbreviation of change mode.
Syntax :
chmod [reference][operator][mode] file...

The references are used to distinguish the users to whom the permissions apply
i.e. they are list of letters that specifies whom to give permissions. The references are
represented by one or more of the following letters:
Reference Clas Description
s
u own file's owner
er
g grou users who are members ofthe file's group
p
o othe users who are neither the file's owner nor members of the file's
rs group
a all All three of the above, same as ugo
The operator is used to specify how the modes of a file should be adjusted. The following operators
are accepted:
Operator Description

+ Adds the specified modes to the


specified classes

- Removes the specified modes from the


specified classes

= The modes specified are to be made the


exact modes for the specified classes
The modes indicate which permissions are to be granted or removed from the specified classes.
There are three basic modes which correspond to the basic permissions:
r Permission to read the file.

w Permission to write (or delete) the file. x


Permission to execute the file, or, in
the case of a directory, search it.

Types of permissions which we will be changing using chmod command :


In linux terminal, to see all the permissions to different files, type ls -l command which lists the
files in the working directory in long format
EXPERIMENT NO :2
SHELL PROGRAMMING

Exp no-2.1 EVEN OR ODD

AIM:
To write a program to find whether a number is even or odd .

ALGORITHM:

STEP 1: Read the input number.


STEP 2: Perform modular division on input number by 2.
STEP 3: If remainder is 0 print the number is even.
STEP 4: Else print number is odd.
STEP 5: Stop the program.

PROGRAM

echo "enter the number"


read num
if [ `expr $num % 2` -eq 0 ]
then
echo "number iseven"
else
echo "number is odd"
fi
OUTPUT:

enter the number: 5


the number is odd.
RESULT:
Thus the program has been executed successfully.

Exp no-2.2BIGGEST OF TWO NUMBERS

AIM :
To write a program to find biggest in two numbers.

ALGORITHM :

STEP 1: Read The Two Numbers.

STEP 2: If Value Of A Is Greater Than B Is Big.


STEP 3: Else Print B Is Big.
STEP 4: Stop The Program.

PROGRAM:

echo "enter the number"


read a b
if [ $a -gt $b ] then
echo "A is big"
else
echo "B is big"
fi

OUTPUT:
Enter The Two Number:
23 67

B is Big.

RESULT:

Thus the program has been executed successfully.


Exp no-2.3FACTORIAL OF NUMBER

AIM:
To find a factorial of a number using shell script.

ALGORITHM:

Step 1: read a number.


Step 2: Initialize fact as 1.
Step 3: Initialize I as 1.
Step 4: While I is lesser than or equal to no.
Step 5: Multiply the value of I and fact and assign to fact increment the value of I by 1.
Step 6: print the result.
Step 7: Stop the program.

PROGRAM:

echo "Enter a number:"


read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo “The factorial is $fact”

OUTPUT:
Enter a number :
5
The factorial is 120
RESULT:
Thus the program has been executed successfully.
Exp no-2.4LEAP YEAR

AIM :
Write a program to find the given year is leap year or not.

ALGORITHM:

PROGRAM:

echo "Enter the year"


read y
b=`expr $y % 4`
if [ $b -eq 0 ]
then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi

OUTPUT :
Enter the year: 2023

2023 is not a leap year

RESULT:

Thus the program has been executed successfully.

You might also like