basic unix commands

38
BASIC UNIX COMMANDS Ex. No: 1 Aim: To write the basic commands and syntax by using Unix. Command Description: Command: date Description: Date command is used to display the current date and time. Syntax: [Student@MRKIT68~]$ <command> Example: [Student@MRKIT68~]$ date <Press Enter Key> Tue Feb 02 10:35:15 IST 2010 [Student@MRKIT68~]$ Options of the Date command: Options of the date command can be used to format the date and time before displaying them. The options are specified within double quote and within the, they must begin with a + symbol. Example: [Student@MRKIT68~]$ date +”%a” <Press Enter Key>

Upload: sathyamurthy-duraikkannu

Post on 26-Oct-2014

76 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Unix Commands

BASIC UNIX COMMANDS

Ex. No: 1

Aim:

To write the basic commands and syntax by using Unix.

Command Description:

Command: date

Description: Date command is used to display the current date and time.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ date <Press Enter Key>

Tue Feb 02 10:35:15 IST 2010

[Student@MRKIT68~]$

Options of the Date command:

Options of the date command can be used to format the date and time before displaying them. The options are specified within double quote and within the, they must begin with a + symbol.

Example: [Student@MRKIT68~]$ date +”%a” <Press Enter Key>

S.No Option Description

1 %r Display time in AM/PM notation

2 %a Display weekdays as (Sun to Sat)

3 %h Display month (Jan to Dec)

4 %m Month of year (in digits)

5 %d Day of month (in digits)

6 %y Year (last two digits)

7 %T Time as HH:MM:SS

8 %H Hour (00 to 23)

9 %M Minute(00 to 59)

10 %S Second(00 to 59)

Page 2: Basic Unix Commands

Command: who

Description: The who command is used to display the names of all users who are currently logged in.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ who <Press Enter Key>

Student: 0 2010 – 02 – 02 17: 13

Student pts/1 2010 – 02 – 02 17: 13 (:0.0)

[Student@MRKIT68~]$

Command: who am i

Description: The who am i command is used to display the name of current users.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ who am i <Press Enter Key>

Student pts/1 2010 – 02 – 02 17: 13 (:0.0)

[Student@MRKIT68~]$

Command: cal

Description: The cal command is used to display the calendar with year and month at present.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ cal <Press Enter Key>

February 2010

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28

[Student@MRKIT68~]$

Options of the cal command:

S.No Options Description

1 Month Year ($cal 02 2010) To display calendar of specified month of specified year.

2 Year ($cal 2010) To display months of a mentioned year.

Page 3: Basic Unix Commands

Command: ls

Description: The ls command is used to list the contents of a directory.

Syntax: [Student@MRKIT68~]$ <command> <option>

Example: [Student@MRKIT68~]$ ls <Press Enter Key>

Desktop fruits tea sat.sh fa.c

Commands: bc

Description: The bc command is used for arithmetic and binary calculation.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ bc <Press Enter Key>

bc 1.06

Copyright 1991 – 1994, 1997, 1998, 2000 free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type ‘warranty’.

8+4

12

4*7

28

<Press ctrl + D>

[Student@MRKIT68~]$

Command: man

Description: To display the manual for various commands.

Syntax: [Student@MRKIT68~]$ man <command>

Example: [Student@MRKIT68~]$ man cal <Press Enter Key>

The above example have list out how to use the calendar command.

Page 4: Basic Unix Commands

Command: history

Description: The history command is used to display the history of commands what you have typed.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ history <Press Enter Key>

1 date

2 who

3 who am i

4 cal

5 history

[Student@MRKIT68~]$

Command: clear

Description: The clear command is used to clear the screen.

Syntax: [Student@MRKIT68~]$ <command>

Example: [Student@MRKIT68~]$ clear <Press Enter Key>

Result:

Thus the basic command in Unix has been executed and output was verified.

************

Page 5: Basic Unix Commands

WORKING WITH FILES AND DIRECTORIES

Ex. No: 2

Aim:

To write the commands and their syntax working with files and directories.

Command Description and their syntax:

Directory command:

CREATING A DIRECTORY:

Description: The mkdir (make directory) command is used to create the directory.

Syntax: [Student@MRKIT68~]$mkdir <directory name>

Example: [Student@MRKIT68~]$mkdir ganguly <Press Enter Key>

[Student@MRKIT68~]$cd ganguly <Press Enter Key>

MOVING A DIRECTORY:

Description: The mv (move) command is used to move the one directory to another directory.

Syntax: [Student@MRKIT68~]$ mv <source_ dir> <destination_ dir>

Example: [Student@MRKIT68~]$ mv ganguly ganguly1 <Press enter Key>

REMOVING A DIRECTORY:

Description: The rmdir (remove directory) command removes the specified directory.

Syntax: [Student@MRKIT68~]$rmdir <directory name>

Example: [Student@MRKIT68~]$rmdir ganguly <Press Enter Key>

CHANGING A DIRECTORY:

Description: The cd command is used to change the current working directory to another directory name.

Syntax: [Student@MRKIT68~]$cd <directory name>

Example: [Student@MRKIT68~]$cd ganguly <Press Enter Key>

Page 6: Basic Unix Commands

Identifying the current directory:

pwd command:

Description: The pwd (present working directory)command is used to display the full path name of the current directory.

Syntax: [Student@MRKIT68~]$pwd

Example: [Student@MRKIT68~]$pwd <Press Enter Key>

File command:

CREATING A FILE: cat command

Description: The cat command is used to create a file.

Syntax: [Student@MRKIT68~]$cat > [filename]

Example: [Student@MRKIT68~]$cat > Siva <Press enter Key>

One

Two

Three

<Press ctrl+d>

COPYING A FILE: cp command

Description: The cp (copy) command is used to copy the one file to another file.

Syntax: cp <source file> <destination file >

Example: cp siva siva1<Press Enter Key>

MOVING A FILE:

Description: The mv (move) command is used to move the one file to another file.

Syntax: [Student@MRKIT68~]$mv <source file> <destination file.>

Example: [Student@MRKIT68~]$mv ganguly ganguly1 <Press enter Key>

REMOVING A FILE:

Description: The rm (remove) command is used removes the specified file.

Syntax: [Student@MRKIT68~]$ rm <file name>

Example: [Student@MRKIT68~]$rm siva <Press enter Key>

Page 7: Basic Unix Commands

SORTING A COMMAND

Description: The sort command is used to sorts, merges, or compares all the lines from the given file.

Syntax: [Student@MRKIT68~]$ sort filename

Example: [Student@MRKIT68~]$ sort siva <Press enter Key>

Example1: [Student@MRKIT68~]$ sort siva siva1 <Press enter Key>

COMPARING A FILE:

Description: The cmp (compare) command is used to compare the two files.

Syntax: [Student@MRKIT68~]$ cmp <file1> <file2>

Example: [Student@MRKIT68~]$ cmp siva siva1

Word counting command:

Wc command:

Description: The wc (count) command is used to count the word and lines in the files.

Syntax: [Student@MRKIT68~]$ wc <filename>

Example: [Student@MRKIT68~]$ wc siva <Press enter Key>

3 5 25

[Student@MRKIT68~]$

File access permissions:

The file permissions determine which users on the system have access to a file and the type of access they are allowed.

The file owner:

The file owner is the user who created the file.

The group owner:

The group is the default group to which the owner belongs. By default this is primary group specified in the /etc/passwd file for the file owner.

Other user:

The other users who are not members of the group.

Page 8: Basic Unix Commands

Protecting files /file access permissions :

Permission associated with files can be changed only by the owner of the file .using the chmod,

access permission is associated with a file or directory can be changed access permission can be set using

the chmod command.

Syntax: [Student@MRKIT68~]$ chmod <access _permission> <filename /directory _ name>

Example: [Student@MRKIT68~]$ chmod 777 siva <Press enter Key>

Result:

Thus the files and directory commands has been executed and output was verified.

********************

Page 9: Basic Unix Commands

PIPES AND FILTERSEx. No: 3

Aim:

To write the commands and their syntax for pipes and filters in unix.

Command Description (PIPES):Description: A pipe is a way to connect the output of one program to the input of another program.

Syntax: [Student@MRKIT68~]$ <command> <filename> | <command>

Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key>

Grapes Mango Apple

Example: [Student@MRKIT68~]$ cat fruits | wc <Press Enter Key>

3 3 15

[Student@MRKIT68~]$

The tee command:

Description: while using pipes we have not seen any output from a command that gets piped into another command.

Where tee command copies the incoming data on input to output. In the mean time it saves a copy in the specified file.

Syntax: [Student@MRKIT68~]$ <command> | tee <file name> | command

Example: [Student@MRKIT68~]$ date | tee fruits | wc <Press Enter Key>

1 6 29

Command Description (FILTERS):

Description: When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output it is referred to as a filter. One of the most common uses of filters is to modify output.

Filter commands: tail, head, cut, uniq and paste

tail: This command would list the last lines in the file <file name>.

Syntax: [Student@MRKIT68~]$ <filter command> <file name> <options> <number>

Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key>

Apple Orange Banana Grapes

Example: [Student@MRKIT68~]$ tail fruits –n 2 <Press Enter Key> Banana Grapes

[Student@MRKIT68~]$

Page 10: Basic Unix Commands

head: This command would display the first lines of file <file name>.

Syntax: [Student@MRKIT68~]$ <filter command> < -number> <file name>

Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key>

Apple Orange Banana Grapes

Example: [Student@MRKIT68~]$ head fruits -2 fruits <Press Enter Key>

Apple Orange [Student@MRKIT68~]$

cut : This command would cut the content of file according to the options in the command

Syntax: [Student@MRKIT68~]$ <cut command> <options> <file name>

Demo file: [Student@MRKIT68~]$ cat fruits <Press Enter Key>

Apple Orange Banana Grapes

Example: [Student@MRKIT68~]$ cut c1 fruits <Press Enter Key>

A O B G [Student@MRKIT68~]$

uniq: This command prints those lines which are unique and remove the duplicate lines.

Syntax: [Student@MRKIT68~]$ <uniq command> <filename>

Demo file: [Student@MRKIT68~]$ cat hello <Press Enter Key>

Arun Suresh Sathya

Sathya Ramesh

Example: [Student@MRKIT68~]$ uniq hello

Arun Suresh Sathya Ramesh

[Student@MRKIT68~]$

Page 11: Basic Unix Commands

paste: This command is used to paste several files together

Syntax: [Student@MRKIT68~]$ <paste command> <file name1> <file name2>

Demo file: [Student@MRKIT68~]$ cat veg

Onion

Brinjal

Demo file: [Student@MRKIT68~]$ cat dept

CSE

CIVIL

Example: [Student@MRKIT68~]$ paste veg dept

Onion CSE

Brinjal CIVIL

[Student@MRKIT68~]$

Result:

Thus the commands used in pipe and filter has been executed and output was verified

*************

Page 12: Basic Unix Commands

VI EDITOR:

Vi - stands for visual

Vi is a visual editor used to enter and edit text files containing data, documents or program. It displays the contents of files on the screen, and allows the user to add, insert, delete or change parts of the text. it is a full screen editor that allows the user to view and edit entire document at the same time.

Features of vi

vi is very easy to learn and more powerful exciting features

vi is great speed which is missing in other editor

vi is case sensitive

Example:

M & m (the upper case ‘M’& lower case ‘m’ are completely different.)

Three modes of vi:

1. Command mode.2. Insert mode.3. Escape mode.

Starting with vi:

There are different ways to start vi editor given below:

[Student@MRKIT68~]$ vi <Press Enter Key>

[Student@MRKIT68~]$ vi <file name> <Press Enter Key>

Example: [Student@MRKIT68~]$ vi Siva <Press Enter Key>

(Vi editor screen will be appeared as shown below)

~~

~

~

~

~

~

~

“Siva” [new file]

Page 13: Basic Unix Commands

Saving the contents of the file (siva)

When you edit a file vi ,the original file is not disturbed, but only a copy placed in the buffer .

The following commands are used to save a file

Command Purpose

:w Save file and remains in edit mode

:x Save file and quit from the edit mode

:wq Save file and quits edit mode

:w file name Save file under new file

:q Quit without changes from edit mode

:sh Escape the Unix shell

COMMANDS USING IN vi – EDITOR

Page 14: Basic Unix Commands

Ex. No: 4

Aim:

To write the command and their syntax used in vi editor.

Command Description:

Syntax: [Student@MRKIT68~]$ <vi file name>

Examples: [Student@MRKIT68~]$ vi new <Press Enter Key>

Core Worm seed Jewel ~ ~ ~ :wq [Student@MRKIT68~]$

sed: This command is used to editing the contents of a file.

Description: The cat command display the contents of the “new” file and send that display through a

pipe to the sed command. The sed command changed the first occurrence of the ‘e’on each line to

‘ZZZ’.

Syntax: [Student@MRKIT68~]$ <sed > <expression> <file>

Demo file: [Student@MRKIT68~]$ cat new <Press Enter Key>

Core

Worm seed

Jewel

Example: [Student@MRKIT68~]$ cat new | sed –e ‘s/e/ZZZ/’ <Press Enter Key>

CorZZZ

Worm sZZZed

JZZZwel

[Student@MRKIT68~]$

grep: This command is used to find the matching pattern in the contents of file

Page 15: Basic Unix Commands

Syntax: <grep> <pattern to find> <file name>

Demo file:[Student@MRKIT68~]$ cat birds <Press Enter Key>

Pigeon

Bulbul

Parrot

Duck

Example:[Student@MRKIT68~]$ grep Bulbul birds <Press Enter Key>

Bulbul

[Student@MRKIT68~]$

awk: This command is used to manipulating the contents of a file.

Description: The awk command select each record from file containing the word “good” and

displayed the third field.

Syntax: <awk> <pattern action> <{file name}>

Demo file:[Student@MRKIT68~]$ cat fruits

Cherry order 4

Mango good 10

Strawberry ok 4

Apple good 12

Banana good 5

Example:[Student@MRKIT68~]$ awk ‘/good/ {print $3}’ fruits <Press Enter Key>

10

12

5

[Student@MRKIT68~]$

********************

SHELL PROGRAMS

Page 16: Basic Unix Commands

FINDING SUM OF TWO NUMBERS

Ex. No: 5a

Aim:

To write a shell program to find the sum of two numbers.

Algorithm:

Step 1: Start the program.

Step 2: Get input values a b.

Step 3: Use the expression, a+b

Step 4: Display the result.

Step 5: Stop the program.

Shell Program:

clear

echo “Enter the values of A and B:”

read a b

echo “The Sum is:”

expr $a + $b

Output:

[Student@MRKIT68~]$sh sum.sh

Enter the values of A and B: 45 23

The Sum is: 68

[Student@MRKIT68~]$

Result:

Thus the shell program to calculate the sum of two numbers has been executed and output was verified.

MULTIPLICATION TABLE

Page 17: Basic Unix Commands

Ex. No: 5b

Aim:

To write a shell script to display the multiplication table of a given number.

Algorithm:

Step1: Start the program.

Step2: Input number ‘n’.

Step3: If i<10 then m=n*i.

Step4: Print the value of i, n and m in multiplication table format.

Step5: Increment the value of i.

Step6: Stop the program.

Shell program:

echo Enter the number

read n

for i in 1 2 3 4 5 6 7 8 9 10

do

m=$(( $n * $i ))

echo “ $i * $n ”=$m

done

Output:

[Student@MRKIT68~]$sh mult.sh

Enter the number101*10=102*10=203*10=304*10=405*10=506*10=607*10=708*10=809*10=9010*10=100 [Student@MRKIT68~]$

Result:

Thus the shell program for displaying the multiplication table has been executed and output was verified.

SWAPPING TWO NUMBERS

Page 18: Basic Unix Commands

Ex. No:5c

Aim:

To write a shell script to interchange two numbers.

Algorithm:

Step 1: Start the program.

Step 2: Input number a and b.

Step 3: Do the following assignments

C=a a=b b=c.Step 4: Print a and b values.

Step 5: Stop the program.

Shell program:

echo Enter a

read a

echo Enter b

read b

c=$a

a=$b

b=$c

echo After Swapping

echo Number a is $a

echo Number b is $b

Output:

[Student@MRKIT68~]$sh swap.sh

Swapping using temporary variableEnter a25Enter b50After swappingNumber a is 50Number b is 25[Student@MRKIT68~]$

Result:

Thus the Shell program for swapping two numbers has been executed and output was verified.COMPARING TWO NUMBERS

Page 19: Basic Unix Commands

Ex. No: 5d

Aim:

To write a shell program to compare two numbers.

Algorithm:

Step 1: Start the program.

Step 2: Input two numbers a and b.

Step 3: Check a and b are equal.

Step 4: If a and b are equal then print numbers are equal. Otherwise numbers are not equal.

Step 5: Stop the program.

Shell program:

echo “Enter two numbers”

read a b

if test $a –eq $b

then

echo “Numbers are equal”

else

echo “numbers are not equal”

fi

Output:

[Student@MRKIT68~]$sh comp.sh

Enter two numbers

34 54

Numbers are not equal.

[Student@MRKIT68~]$

Result:

Thus the shell program to compare the two numbers has been executed and output was verified.

LARGEST OF THREE NUMBERS

Page 20: Basic Unix Commands

Ex. No: 6

Aim:

To create a shell program to find the largest of three numbers.

Algorithm:

Step 1: Start the program.

Step 2: Get input values a, b,c.

Step 3: Use the if condition.

Step 4: if [a > b] , & [a > c]

Print ‘a’ is greater (or) Print ‘c’ is greater.

Step 5: if[b > c]

Print ‘b’ is greater (or) Print ‘c’ is greater.

Step 6: Close if loop as fi

Step 7: Display the output and stop the program.

Shell Program:

echo “Enter any three numbers”read aread bread cif [ $a –gt $b –a $a –gt $c ]thenecho “The largest number is” $aelif [ $b –gt $c]thenecho “The largest number is” $belseecho “The largest number is” $cfi

Output:

[Student@MRKIT68~]$ sh big.sh

Enter any three numbers

321333The largest number is 33

[Student@MRKIT68~]$

Result:

Thus the shell program to find the largest of three numbers has been executed and output was verified.

TO CHECK EVEN OR ODD NUMBER

Page 21: Basic Unix Commands

Ex. No: 7a

Aim:

To write a shell program to check the given number is even or odd

Algorithm:

Step 1: Start the program.

Step 2: Enter the number.

Step 3: Find ‘c’ by using expression.

Step 4: If ‘c’ is equal to zero then number is even .

Step 5: else number is odd.

Step 6: Display the result.

Step 7: Stop the program.

Shell program:

echo “Enter a number”

read n

c=$(( $n % 2 ))

if [ $c –eq 0 ]

then

echo “This is an even number.”

else

echo “This is an odd number.”

fi

Output:

[Student@MRKIT68~]$ sh eo.sh

Enter a number.6This is an even number.[Student@MRKIT68~]$

Result:

Thus the shell program to check the number is even or odd has been executed and output was verified.

CHECKING POSITIVE OR NEGATIVE NUMBER

Page 22: Basic Unix Commands

Ex. No: 7b

Aim:

To write a shell program to check the given number is positive or negative number.

Algorithm:

Step 1: Start the program.

Step 2: Input number n.

Step 3: Is n greater than zero. If yes print positive number and go to step 5.

Step 4: If no print negative number.

Step 5: Stop the program.

Shell Program:

echo “Enter a number”

read n

if test $n –gt 0

then

echo “Positive number”

elif test $n –lt 0

then

echo “Negative number”

else

echo “zero”

fi

Output:

[Student@MRKIT68~]$ sh pn.sh

Enter a number

10

Positive number.

[Student@MRKIT68~]$

Result:

Thus the shell program to check positive or negative number has been executed and output was verified.

MENU DRIVEN SHELL PROGRAM

Page 23: Basic Unix Commands

Ex. No: 8

Aim: To create a shell program for menu driven

Algorithm:Step 1: Start the program.Step 2: Using case structure enter the menu one by one.Step 3: If the choice 1 is selected, today’s date will be displayed.Step 4: If the choice 2 is selected, present working directory will be displayed.Step 5: If the choice 3 is selected, list the users logged will be displayed.Step 6: If the choice 4 is selected, current month is displayed.Step 7: If the choice 5 is selected, we can exit from the program.Stop 8: Stop the program.

Shell program:while truedoecho “Menu”echo “1. Today’s date”echo “2. Present working directory”echo “3. List of users”echo “4. Display current month”echo “5. Exit”echo Enter your choice:read choicecase $choice in1) date;;2) pwd;;3) ls;;4) cal;;5) exit;;6) echo “Invalid choice”esacdone

Output:

[Student@MRKIT68~]$ sh menu.shMenu1. Today’s date2. Present working directory3. List of users4. Display current month

5. ExitEnter your choice:2/home/studentMenu1. Today’s date2. Present working directory3. List of users4. Display current month

5. Exit[Student@MRKIT68~]$

Result:

Thus the shell program of menu driven program has been executed and output was verified.LEAP YEAR OR NOT

Page 24: Basic Unix Commands

Ex. No: 9

Aim:

To write a shell program to check whether the given year is leap year or not.

Algorithm:

Step 1: Start the program.

Step 2: Enter the year.

Step 3: Check the condition, then print leap year or not.

Step 4: Display the result

Step 5: Stop the program.

Shell program:

echo “Enter the year”

read year

a =$(( $year % 4 ))

if [ $a –eq 0 ]

then

echo $year is a leap year

else

echo $year is not a leap year

fi

Output:

[Student@MRKIT68~]$sh leap.sh

Enter the year

2012

2012 is a leap year.

[Student@MRKIT68~]$

Result:

Thus the shell program to check whether the year is leap year or not has been executed and output was verified.

CHECKING PRIME NUMBER

Page 25: Basic Unix Commands

Ex. No: 10

Aim:To write a shell program to check whether the given number is prime number or not.

Algorithm:

Step 1: Start the program.

Step 2: Input number n.

Step 3: Assign i = 2, j = 0.

Step 4: Is i < n then r = n % i. Otherwise go to step 8.

Step 5: Is r = 0 then increment I and j value by 1. Otherwise go to step 6.

Step 6: Increment i value one by one.

Step 7: Repeat step 4 to 6.

Step 8: Is j = 0 then print number is prime and go to step 10.

Step 9: Is j! = 0 then print number is not a prime number.

Step 10: Stop the program.

Shell Program:

echo Enter a numberread ni=2j=0while test $i –lt $ndor=$(( $n % $i ))if test $r –eq 0thenj=$(( $j + 1 ))fii=$(( $i +1 ))doneif test $j –eq 0thenecho Number is primeelseecho Number is not primefi

Output:

[Student@MRKIT68~]$sh prime.sh

Enter a number10Number is not Prime.[Student@MRKIT68~]$

Result :

Thus the shell program to check the given number is prime number or not.

FIBONACCI SERIES

Page 26: Basic Unix Commands

Ex. No:11

Aim: To write a shell script to generate the Fibonacci series for the given limit.Algorithm:

Step 1: Start the program.Step 2: Input number n.Step 3: Assign a=01,b=1 and print a,b values.Step 4: Assign i=2.Step 5: If i<n then go to step 6 otherwise go to step 9.Step 6: Calculate c = a + b, i= i +1,assign a = b, b = c.Step 7: Print the value of c.Step 8: Repeat steps 5 to 7.Step 9: Stop the program.

Shell program:echo “Program to generate Fibonacci series”echo ”Enter the range to be displayed”read na=0b=1echo fibonacci seriesecho $aecho $b i=2while test $i -lt $ndoc=$(( $a + $b ))echo $ci=$(( $i + 1 ))echo $ci=’expr $i + 1’a=$bb=$cdone

Output:[Student@MRKIT68~]$sh fibo.shProgram to generate Fibonacci seriesEnter the range to be displayed10Fibonacci series01123[Student@MRKIT68~]$

Result:

Thus the shell program to print the Fibonacci series for the given range has been executed and output was verified.

CALCULATING FACTORIAL OF A GIVEN NUMBER

Page 27: Basic Unix Commands

Ex. No: 12

Aim:

To write a shell script to find the factorial of a given numbers.

Algorithm:

Step 1: Start the program.Step 2: Input number n.Step 3: Initialize the value of i and factorial f to 1.Step 4: Is the value of i is less than the number find the value of factorial by multiplying i and f. Step 5: Increment the value of i by 1.Step 6: Print the result.Step 7: Stop the program.

Shell program:

Echo “Enter a number”

read n

i=1

f=1

if test $n -eq 0

then

echo “factorial is 1”

else

while test $i –le $n

do

f=$(( $f * $i ))

i=$(( $i + 1 ))

done

echo Factorial is $f

fi

Output

[Student@MRKIT68~]$sh fact.shEnter a number 5Factorial is 120[Student@MRKIT68~]$

Result:

Thus the Shell program to find the factorial of the given number has been executed and output was verified.

****************

FORK FUNCTION

Page 28: Basic Unix Commands

Ex. No: 13

Aim:

To write the fork function using c program.

Algorithm:

Step 1: Declare the input.

Step 2: Use if loop.

Step 3: Declare condition.

Step 4: Execute the output.

Program:

#include <stdio.h>

main()

{

int I;

int pid;

pid=fork();

if(pid==0)

{

for(i=0;i<=5;i++)

{

printf(“\n I am in child %d”,i)

sleep(2);

}

}

else

{

for(i=0;i<10;i++)

{

printf(“\n I am in parent %d”,i);

sleep(1);

}

}

}

Page 29: Basic Unix Commands

Output:

[Student@MRKIT68~]$ cc fork.c

[Student@MRKIT68~]$ ./a.out

I am in parent0

I am in child0

I am in parent1

I am in child1

I am in parent2

I am in parent3

I am in child2

I am in parent4

I am in parent5

I am in child3

I am in parent6

I am in parent7

I am in child4

I am in parent8

I am in parent9

I am in child5

[Student@MRKIT68~]$

Result:

Thus the C program in Unix has been executed and output was verified.

Page 30: Basic Unix Commands

SIGNAL IN UNIX

Ex. No: 14

Aim:

To write the signal in Unix using C program.

Algorithm:

Step 1: Declare the integer.

Step 2: Use for(i=1;i<=10;i++)

Step 3: Close the loop.

Step 4: Execute the output.

Program:

#include<stdio.h>#include<signal.h>main(){void change();int i;signal(SIGINT,change);for(i=1;i<=10;i++){printf(“\n unix signal program”);sleep(1);}}void change(){printf(“\n Test the signal”);}}

Output:

[Student@MRKIT68~]$ cc signal.c[Student@MRKIT68~]$ ./a.outunix signal programunix signal programunix signal programTest the Signalunix signal programunix signal programunix signal programunix signal programTest the Signalunix signal programunix signal programunix signal program[Student@MRKIT68~]$

Result:

Thus the ‘C’ program in Unix has been executed and output was verified.

Page 31: Basic Unix Commands

FILE HANDLING IN UNIX USING C

Ex. No: 15

Aim:

To write the C program for File Handling in Unix using C

Algorithm:

Step 1: Declare the integer fd.

Step 2: Use if command.

Step 3: Declare the conditions.

Step 4: Close if command.

Program:#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

main()

{

int fd;

char fname[20],buf1[100],buf2[]=’hai how are you’;

printf(“\n enter input file name:”)

scanf(“%s”,fname);

if((fd=creat(fname,S_IRWXU))<0)

{

printf(“\n error in file creation);

exit(0);

}

else

{

write(fd,buf2,20);

printf(“\n content is written”);

close(fd);

fd=open(fname,O_RDONLY);

read(fd,buf1,20);

printf(“\n the file content is :\n %s”,buf1);

printf(“\n”);

close(fd);

}

}

Page 32: Basic Unix Commands

Output:

[Student@MRKIT68~]$ cc file1.c

[Student@MRKIT68~]$ ./a.out

enter input file name:first.out

content is written

the file content is :

Hai how are you

[Student@MRKIT68~]$

[Student@MRKIT68~]$ vi first.out

Hai how are you^@^A^@^@^@

Result:

Thus the ‘c’ program in Unix has been executed and output was verified.

*******************