KEMBAR78
UNIX OS Programs | PDF | Operating System | Shell (Computing)
0% found this document useful (0 votes)
36 views42 pages

UNIX OS Programs

Uploaded by

kulwanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views42 pages

UNIX OS Programs

Uploaded by

kulwanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

UNIX OPERATING SYSTEM

1. INTRODUCTION

UNIX is an operating system which was first developed in the 1960’s, and has been under
constant development ever since. By operating system, we mean the suite of programs which make the
computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.

UNIX systems also have a Graphical User Interface (GUI) similar to Microsoft Windows
which provides an easy to use environment. However, knowledge of UNIX is required for operations
which aren't covered by a graphical program, or for when there is no windows interface
available, for example, in a telnet session.

There are many different versions of UNIX, although they share common similarities. The most popular
varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS.

2. UNIX SYSTEM ORGANIZATION

1
The UNIX operating system is made up of four parts; the hardware, the kernel, the shell and the tools.

HARDWARE
The Hardware is the physical parts or components of a computer, such as the monitor,
keyboard, computer data storage, graphic card, sound card and motherboard. By contrast,
software is instructions that can be stored and run by hardware.

KERNEL
It is also known as the heart of Unix operating system, which interact with actual hardware and machine
level language. The Kernel has various functions. It manages files, manages memory, Manages
system resources, allocates time between users and processes, decides process priority and so on. It
is mostly a collection of programs written in C. There can be number of shells but has only one kernel. It
contains all the system files, devices drives etc.

SHELL
The shell acts as an interface between the user and the kernel. When a user logs in, the login program
checks the username and password, and then starts another program called the shell. The shell is a
Command Line Interpreter (CLI). It interprets the commands the user types in and arranges for them
to be carried out. The commands are themselves programs: when they terminate, the shell gives the user
another prompt (% on our systems).

END USERS
They are also known as third party application

3. FEATURES OF UNIX OPERATING SYSTEM

PORTABILITY
The system is written in high-level language making it easier to read, understand, change and, therefore
move to other machines. The code can be changed and complied on a new machine. Customers can then
choose from a wide variety of hardware vendors without being locked in with a particular vendor.

2
MACHINE-INDEPENDENCE
The System hides the machine architecture from the user, making it easier to write applications
that can run on micros, mins and mainframes.

MULTI-TASKING
Unix is a powerful multi-tasking operating system; it means when a active task in in process, there can
be a simultaneous background process working too. Unix handles these active and background threads
efficiently and manages the system resources in a fair-share manner.

MULTI-USER OPERATIONS
UNIX is a multi-user system designed to support a group of users simultaneously. The system
allows for the sharing of processing power and peripheral resources, white at the same time providing
excellent security features.

GRAPHICAL USER INTERFACE


A Graphical User Interface (GUI) is a human-computer interface (i.e., a way for humans to interact
with computers) that uses windows, icons and menus and which can be manipulated by a mouse (and
often to a limited extent by a keyboard as well).
GUIs stand in sharp contrast to Command Line Interfaces (CLIs), which use only text and are accessed
solely by a keyboard. The most familiar example of a CLI to many people is MS- DOS. Another
example is Linux when it is used in console mode (i.e., the entire screen shows text only).

HIERARCHICAL FILE SYSTEM


UNIX uses a hierarchal file structure to store information. This structure has the maximum flexibility in
grouping information in a way that reflects its natural state. It allows for easy maintenance and efficient
implementation.

SECURITY
Unix provides 3 levels of security - login password, file permission, file encryption and
decryption. The first level being providing password & login names to individual users. At next level
i.e., file level there are read, write & execute permission to each file. The last being file encryption that
helps to convert the entire file into an unreadable format.

3
COMMUNICATION
Unix provides a facility in which we can communicate with other users .The users can send and receive
mail, data, programs and other information.

Example: Who displays list of all users currently working on Unix. It displays login name, terminal
number, date and time.
$who
root console Jan 30 10:20 kk tty01 Jan 30 10:30

4. UNIX SHELL
UNIX has a simple user interface called the shell that has the power to provide the services that the user
wants. It protects the user from having to know the intricate hardware details.

TYPE OF SHELL
1. BOURNE SHELL (sh)
The Bourne shell, called "sh," is one of the original shells, developed for UNIX computers by Stephen
Bourne at AT&T's Bell Labs in 1977. Its long history of use means many software developers are
familiar with it. It offers features such as input and output redirection, shell scripting with string and
integer variables, and condition testing and looping.

2. BOURNE-AGAIN SHELL (bash)


The popularity of sh motivated programmers to develop a shell that was compatible with it, but with
several enhancements. Linux systems still offer the sh shell, but "bash" -- the "Bourne-again Shell"
based on sh -- has become the new default standard. One attractive feature of bash is its ability to
run sh shell scripts unchanged. Shell scripts are complex sets of commands that automate
programming and maintenance chores; being able to reuse these scripts saves programmers time.
Conveniences not present with the original Bourne shell include command completion and a command
history.

3. C SHELL (CSH) and TAB C SHELL (tcsh)


Developers have written large parts of the UNIX operating system in the C and C++ languages.
Using C syntax as a model, Bill Joy at Berkeley University developed the "C- shell," csh, in
1978. Ken Greer, working at Carnegie-Mellon University, took csh concepts a step forward with a new
4
shell, tcsh, which UNIX systems now offer. Tcsh fixed problems in csh and added command
completion, in which the shell makes educated "guesses" as you type, based on your system's
directory structure and files, enhanced history substitution to reuse commands, spelling correction
and word completion. Tcsh does not run bash scripts, as the two have substantial differences.

4. KORN SHELL (ksh)


David Korn developed the Korn shell, or ksh, about the time tcsh was introduced. Ksh is
compatible with sh and bash. Ksh is considered as the family member of Bourne shell as it uses the $
symbol of Bourne shell by adding floating-point arithmetic, job control, command aliasing and
command completion. It most widely used shell.

5. vi – VISUAL EDITOR
Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables
you to edit lines in context with other lines in the file.
An improved version of the vi editor which is called the VIM has also been made available now. Here,
VIM stands for Vi IMproved.
You can use the vi editor to edit an existing file or to create a new file from scratch. You can also use
this editor to just read a text file.

5
The above command will generate the following output-

You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a
line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some
other non-viewable character present.

OPERATION MODES
While working with the vi editor, we usually come across the following two modes –

COMMAND MODE − This mode enables you to perform administrative tasks such as save the files,
execute the commands, move the cursor, cut and paste the lines or words, as well as find and replace. In
this mode, whatever you type is interpreted as a command.

INSERT MODE − This mode enables you to insert text into the file. Everything that's typed in this
mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode for
which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the
command mode.

HINT: If you are not sure which mode you are in, press the Esc key twice; this will take you to the
command mode. You open a file using the vi editor. Start by typing some characters and then come to
the command mode to understand the difference.

6
Getting Out of vi
The command to quit out of vi is Esc:q. Once in the command mode, type colon, and ‘q’, followed by
return. If your file has been modified in any way, the editor will warn you of this, and not let you quit.
To ignore this message, the command to quit out of vi without saving is Esc:q!. This lets you exit
vi without saving any of the changes.

The command to save the contents of the editor is Esc:w. You can combine the above
command with the quit command, or use Esc:wq and return.

COMPILING AND EXECUTING


When you compile your program the compiler produces a file containing binary code which is directly
readable by the machine you are on. This file is called an executable file, because it can be executed by
the machine. But this executable file may not be readable on other machines. Any program you
want to run and compile on a machine, do not transfer the executable file from one machine to
another. It is best to recompile the code.

7
6. UNIX COMMANDS
Sl.
COMMAND EXAMPLE DESCRIPTION
No.
1 date date Displays the current date and time.
echo unix
2 echo echo ‘unix’ Displays the argument.
echo “unix”
Display the calendar. Capable of printing
3 cal cal calendar for any year in the specified range (1 –
9999) on the screen.
who Display all user information who have
logged into the system currently.
4 who who am i Display the current user information about
login name, terminal number, date and time at
which logged in.
bc Basic calculator, as we type bc at the
$2 + 3 prompt, user will be in calculator mode and $
prompt disappears, which performs basic
5 bc
arithmetic operations and displays the
result in integral form.
bc -l Display the floating value of the operation.
6 factor factor Displays the factors of a given number.
Evaluates an expression used to carry out
7 expr expr `9 – 3` arithmetic operations and write the result on
terminal.
ls Displays list of current files and directories of
specified user directory.
8 ls
ls -l Displays long list format of files and
directories.
Displays the version of the running bash
9 /bin/bash -version /bin/bash -version
(Bourne-again-Shell).

8
uname Displays the username of the system.
10 uname uname –a Displays the username, local host and
domain details.
11 logname logname Displays the login name of the user.
Displays the path of the present working
12 pwd pwd
directory.
Displays a snapshot/ information about the
13 ps ps
process running at any instant.
14 df df Displays the free disk space.
Displays the space utilized by each file in a
15 du du
directory.
To change mode/set permission for files and
16 chmod chmod
directories.
cd tempdir Change current directory to tempdir.
17 cd cd.. Move back from one directory.
cd\ Move back to root directory.
18 mkdir mkdir graphics Make/Create a directory called graphics.
Remove a specified directory (must be
19 rmdir rmdir graphics
empty)
Copy the content of source file into
20 cp cp source destination
destination file.
21 rm rm file Remove/ Delete a specified file.
22 mv mv old new Move/ Rename old file by new file.
cat manager Displays the contents of manager file.
cat > product Creates a file by name product.
23 cat
cat file1 file2 > file3 Concatenate the content of file1 file2 into one
file, ie. file3.
24 ctrl + d ctrl + d To save and exit from the file.
25 ctrl + z ctrl + z To logout of the current running system.
26 man man ls Manual (help) about the command.
27 tput tput clear Clears the terminal.

9
wc account Counts the number of lines, words and
characters in the file account.
wc -l account Counts the number of lines of the file
account, where –l is an option.
28 wc
wc -w account Counts the number of words of the file
account, where –w is an option.
wc -c account Counts the number of characters of the file
account, where –c is an option.
sort –d name/ sort name Sort the content of the file name in
dictionary order.
sort –r name Sort the content of the file name in reverse
order
sort –k2 name Sort the content second column of the file name
29 sort
in Alphabetical order (A-Z)/ Ascending
order (0-9).
sort –r –k3 name Sort the content third column of the file name
in reverse Alphabetical order (Z-A)/Descending
order (9-0).
30 pwd pwd Displays present working directory
31 head head file Displays the first 10 lines of the specified file.
32 tail tail file Displays the last 10 lines of the specified file.
33 fork( ) fork( ) Create a child process.
34 getpid( ) getpid( ) Fetch the child process ID.
35 getppid( ) getppid( ) Fetch the parent process ID
“Globally Search a Regular Expression
grep [option] and Print it”, searches the specified input
36 grep search NAME file
PATTERN filename fully for a matter with the supplied pattern and
displays it.

10
7. UNIX LABCYCLE PROGRAMS

1. a) Write a shell script to exchange the contents of two variables without using
third variables.

tput clear
echo "*************************************************"
echo "Exchange the content of two variables without third variable"
echo "*************************************************"
echo "Enter two numbers"
read a b
echo "**************************"
echo "Before swapping a = $a, b = $b"
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo "**************************"
echo " After swapping a = $a, b = $b"
echo "**************************"

11
OUTPUT
[user@localhost user]$ sh swap.sh
*************************************************
Exchange the content of two variables without third variable
*************************************************
Enter two numbers
5 13
*****************************
Before swapping a = 5, b = 13
*****************************
After swapping a = 13, b=5
*****************************

12
1. b) Write a shell script to exchange the contents of two variables with using third
variables.

tput clear
echo "**********************************************"
echo "Exchange the content of two variables with third variable"
echo "**********************************************"
echo "Enter two numbers"
read a b
echo "**************************"
echo " Before swapping a = $a, b = $b"
c=`expr $a `
a=`expr $b`
b=`expr $c`
echo "**************************"
echo " After swapping a = $a, b = $b"
echo "**************************"

13
OUTPUT
[user@localhost user]$ sh exchange.sh
**********************************************
Exchange the content of two variables with third variable
**********************************************
Enter two numbers
15 3
*****************************
Before swapping a = 15 b=3
*****************************
After swapping a=3 b = 15
*****************************

14
2. Write a shell script, which accepts three subject marks scored by a student and
declare the result.

tput clear
echo "**********************************************"
echo "Accepts three subject marks scored and declare the result "
echo "**********************************************"
echo "Enter marks of three subjects"
read m1 m2 m3
total=`expr $m1 + $m2 + $m3`
avg=`expr $total / 3`
echo "**************************"
echo "Total = $total"
echo "Average = $avg"
if [ $m1 -lt 35 -o $m2 -lt 35 -o $m3 -lt 35 ]
then
echo "Result: Fail"
elif [ $avg -ge 80 -a $avg -lt 100 ]
then
echo "Result: Distinction"
elif [ $avg -ge 60 -a $avg -lt 80 ]
then
echo "Result: First class"
elif [ $avg -ge 50 -a $avg -lt 60 ]
then
echo "Result: Second class"
else
echo "Result: Pass"
fi
echo "**************************"
exit 0

15
OUTPUT 1
[user@localhost user]$ sh markscored.sh
**********************************************
Accepts three subject marks scored and declare the result
**********************************************
Enter marks of three subjects
45 32 52
*****************************
Total = 129
Average = 43
Result: Fail
*****************************

OUTPUT 2
[user@localhost user]$ sh markscored.sh
**********************************************
Accepts three subject marks scored and declare the result
**********************************************
Enter marks of three subjects
*****************************
85 96 75
*****************************
Total = 256
Average = 85
Result: Distinction
*****************************

16
OUTPUT 3
[user@localhost user]$ sh markscored.sh
**********************************************
Accepts three subject marks scored and declare the result
**********************************************
Enter marks of three subjects
*****************************
65 75 62
*****************************
Total = 202
Average = 67
Result: First class
*****************************

OUTPUT 4
[user@localhost user]$ sh markscored.sh
**********************************************
Accepts three subject marks scored and declare the result
**********************************************
Enter marks of three subjects
52 59 60
*****************************
Total = 171
Average = 57
Result: Second class
*****************************

17
OUTPUT 5
[user@localhost user]$ sh markscored.sh
**********************************************
Accepts three subject marks scored and declare the result
**********************************************
Enter marks of three subjects
45 36 56
*****************************
Total = 137
Average = 45
Result: Pass
*****************************

18
3. Write a shell script to print integer numbers from 1 to 20.

tput clear
echo "***************************"
echo "Print integer number from 1 to 20"
echo "***************************"
i=1
while [ $i -le 20 ]
do
echo $i
i=`expr $i + 1`
done
echo "***************************"
exit 0

19
OUTPUT
[user@localhost user]$ sh integer.sh
****************************
Print integer number from 1 to 20
****************************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
****************************

20
4. Write a shell script to perform arithmetic operation on two numbers depending
on +, -, *, / and %.

tput clear
echo "*************************************"
echo "Perform arithmetic operation on two numbers"
echo "*************************************"
echo "Enter value for variable A and B"
read a b
while [ 1 ]
do
echo "***********************************************************"
echo " + Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit"
echo "***********************************************************"
echo "Choose any one"
read ch
case $ch in
+) echo "Addition"
echo "$a + $b = " `expr $a + $b`;;
-) echo "Subtraction"
echo "$a - $b = " `expr $a - $b`;;
\*) echo "Multiplication"
echo "$a * $b = " `expr $a \* $b`;;
/) echo "Division with quotient value"
echo "$a / $b = " `expr $a / $b`;;
%) echo "Division with remainder value"
echo "$a % $b = " `expr $a % $b`;;
0) echo "Thank you";;
exit 0;;
esac
echo "***********************************************************"
done
21
OUTPUT
[user@localhost user]$ sh arithmetic.sh
*************************************
Perform arithmetic operation on two numbers
*************************************
Enter value for variable A and B
12 6
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
+
Addition
12 + 6 = 18
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
-
Subtraction
12 – 6 = 6
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
*
Multiplication
12 * 6 = 72

22
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
/
Division with quotient value
12 / 6 = 2
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
%
Division with remainder value
12 % 6 = 0
***********************************************************
+ Addition - Subtraction * Multiplication / Division with quotient value
% Division with remainder value 0 exit
***********************************************************
Choose any one
0
Thank you
***********************************************************

23
5. Write an interactive shell script to display a menu and perform the following
task: i) Renaming a file ii) Deleting a file iii) Copying a file iv) Exit.

tput clear
echo "***************************************************"
echo "An interactive program to display & perform the following task"
while [ 1 ]
do
echo "*******************************************"
echo "1. Rename a file 2. Delete a file 3. Copy a file 4. Exit"
echo "*******************************************"
echo "Choose any one"
read op
case $op in
1) echo "Renaming a file"
echo "**************************"
echo "Enter the existing filename"
read old
echo "Enter the new filename"
read new
mv $old $new;;
2) echo "Deleting a file"
echo "**************************"
echo "Enter the existing filename"
read fn
rm $fn;;
3) echo "Copying a file"
echo "**************************"
echo "Enter the source filename"
read src
echo "Enter the destination filename"
read dst
cp $src $dst;;
24
4) echo "Thank you"
echo "**************************"
exit 0;;
esac
done

25
OUTPUT 1
[user@localhost user]$ cat > count
Welcome to UNIX systems
It is a graphical user interface (GUI)
ctrl+d

[user@localhost user]$ ls
swap.sh a.out menu.sh files.c markscard.sh count sort

[user@localhost user]$ sh menu.sh


***************************************************
An interactive program to display & perform the following task
***************************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
1
Renaming a file
*******************************************
Enter the existing filename
count
Enter the new filename
mfgc
*******************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
4
Thank you
*******************************************
[user@localhost user]$ ls
swap.sh a.out menu.sh files.c markscard.sh mfgc sort

26
OUTPUT 2
[user@localhost user]$ sh menu.sh
***************************************************
An interactive program to display & perform the following task
***************************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
2
Deleting a file
*******************************************
Enter the existing filename
sort
*******************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
4
Thank you
*******************************************

[user@localhost user]$ ls
swap.sh a.out menu.sh files.c markscard.sh mfgc

27
OUTPUT 3
[user@localhost user]$ sh menu.sh
***************************************************
An interactive program to display & perform the following task
***************************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
3
Copying a file
*******************************************
Enter the source filename
mfgc
Enter the destination filename
mahajana
***************************************************
1. Rename a file 2. Delete a file 3. Copy a file 4. Exit
*******************************************
Choose any one
4
Thank you
*******************************************

[user@localhost user]$ ls
swap.sh a.out menu.sh files.c markscard.sh mfgc mahajana

[user@localhost user]$ cat mahajana


Welcome to Unix Lab
It is a graphical user interface (GUI)

28
6. Write a shell script which counts the number of lines, words and character in a
file.

tput clear
echo "**********************************************"
echo "Counts the number of lines, words and characters in a file"
echo "**********************************************"
echo "Enter the filename"
read file
echo "**********************************************"
echo “The Number of lines in a given file”
wc -l $file
echo "**********************************************"
echo “The Number of words in a file”
wc -w $file
echo "**********************************************"
echo “The Number of characters in a file”
wc -c $file
echo "**********************************************"

29
OUTPUT
[user@localhost user]$ cat > count
Hai!!
How are you?
Fine!! Thank you.
Ctrl+d

[user@localhost user]$ sh wordcount.sh


**********************************************
Counts the number of lines, words and characters in a file
**********************************************
Enter the filename
count
**********************************************
The Number of lines in a given file
3 count
**********************************************
The Number of words in a file
7 count
**********************************************
The Number of characters in a file
34 count
**********************************************

30
7. Write a shell script to accept three command line arguments and display each one
of them.

tput clear
echo "***************************************"
echo "Accept and display the command line arguments"
echo "***************************************"
echo " The count of arguments = "$#
echo "Display all the Arguments = "$*
echo "The filename = "$0
echo "The first argument = "$1
echo "The second argument = "$2
echo "The third argument = "$3
echo "***************************************"

31
OUTPUT
[user@localhost user]$ sh argument.sh Hello! Good Morning...
***************************************
Accept and display the command line arguments
***************************************
The count of arguments = 3
Display all the Arguments = Hello! Good Morning...
The filename = argument.sh
The first argument = Hello!
The second argument = Good
The third argument = Morning...
***************************************

32
8. Write a c program to display the PID of parent and PID of child

#include<stdio.h>
main()
{
int child_id;
child_ id =fork();
printf("***************************************\n");
printf("Display Process_ID of Parent and Child Process\n");
printf("***************************************\n");
if(child_ id)
{
printf("Parent says Child_PID = %d\n",child_ id);
return 0;
}
if(child_ id <0)
{
fprintf(stderr,"fork failed\n");
return 1;
}
printf("Child_PID = %d and Parent_PID =%d\n", getpid(), getppid());
printf("********************************\n");
}

33
OUTPUT
[user@localhost user]$ cc process.c
[user@localhost user]$ ./a.out
***************************************
Display Process_ID of Parent and Child Process
***************************************
Parent says Child_PID = 2938
Child_PID = 2938 and Parent_PID =2937
*******************************************

34
9. Write a c program to copy the contents of one file into the other using command
line arguments.

#include<stdio.h>
main (int argc,char *argv[ ])
{
char ch;
FILE *fs,*fd;
printf("**********************************************************\n");
printf("Copy the content from one file to another using command line argument\n");
printf("**********************************************************\n");
if(argc!=3)
{
printf("Insufficient Arguments\n");
return 0;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("Cannot open source file\n");
return 0;
}
fd=fopen(argv[2],"w");
if(fd==NULL)
{
puts("Cannot open destination file\n");
fclose(fs);
return 0;
}
for(;;)
{
ch=fgetc(fs);
if(ch==EOF)
35
break;
else
fputc(ch,fd);
}
fclose(fs);
fclose(fd);
printf("Copy Successfully\n");
printf("**********************************************************\n");
return 0;
}

36
OUTPUT 1
[user@localhost user]$ cc copycontent.c
[user@localhost user]$ ./a.out
**********************************************************
Copy the content from one file to another using command line argument
**********************************************************
Insufficient Arguments
**********************************************************

OUTPUT 2
[user@localhost user]$ cat > details
Ajay Kumar
Mohammed Ali
Christopher
Sangeetha
Ctrl+d

[user@localhost user]$ cc copycontent.c


[user@localhost user]$ ./a.out details list
**********************************************************
Copy the content from one file to another using command line argument
**********************************************************
Copy Successfully
**********************************************************
[user@localhost user]$ cat list
Ajay Kumar
Mohammed Ali
Christopher
Sangeetha

37
10. Assume a file with following information
First Name Middle Name Age
Write a shell script to
i) Sort the first name in alphabetical order
ii) Sort the age in terms of ascending order
iii) Sort the age in terms of descending order
iv) Sort the middle name in alphabetical order

tput clear
echo "********************************************************"
echo "Accept and sort a file in alphabetical, ascending and descending order"
echo "********************************************************"
echo "Enter a filename"
read fn
echo "********************************************************"
echo "Sort first name in alphabetical order"
echo "********************************************************"
sort -d $fn
echo "********************************************************"
echo "Sort age in ascending order"
echo "********************************************************"
sort -k3 $fn
echo "********************************************************"
echo "Sort age in descending order"
echo "********************************************************"
sort -r -k3 $fn
echo "********************************************************"
echo "Sort middle name in alphabetical order"
echo "********************************************************"
sort -k2 $fn
echo "********************************************************"

38
OUTPUT
[user@localhost user]$ cat > name
Victor Manase 20
Ann Jesty 18
Irfan Shariff 21
Chandan Babu 17
Guru Prasad 19
Ctrl+d

[user@localhost user]$ sh sortfile.sh


********************************************************
Accept and sort a file in alphabetical, ascending and descending order
********************************************************
Enter a filename
name
********************************************************
Sort first name in alphabetical order
********************************************************
Ann Jesty 18
Chandan Babu 17
Guru Prasad 19
Irfan Shariff 21
Victor Manase 20
********************************************************
Sort age in ascending order
********************************************************
Chandan Babu 17
Ann Jesty 18
Guru Prasad 19
Victor Manase 20
Irfan Shariff 21
********************************************************

39
Sort age in descending order
********************************************************
Irfan Shariff 21
Victor Manase 20
Guru Prasad 19
Ann Jesty 18
Chandan Babu 17
********************************************************
Sort middle name in alphabetical order
********************************************************
Chandan Babu 17
Ann Jesty 18
Victor Manase 20
Guru Prasad 19
Irfan Shariff 21
********************************************************

40
11. Write a shell script to display
i) The version of the shell
ii) The user information
iii) Login date and time
iii) List of processes running on the system
iv) User home directory

tput clear
echo "******************************************************************"
echo "Display the Version, User Information, Date, Running Process & Home Directory"
echo "******************************************************************"
echo "1. The version of the shell"
/bin/bash -version
echo "*************************************************************"
echo "2. The user information"
who am i
echo "*************************************************************"
echo "3. Login date and time"
date
echo "*************************************************************"
echo "4. List of processes running on the system"
ps
echo "*************************************************************"
echo "5. User home directory"
pwd
echo "*************************************************************"

41
OUTPUT
[user@localhost user]$ sh display.sh
******************************************************************
Display the Version, User Information, Date, Running Process & Home Directory
******************************************************************
1. The version of the shell
GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
*************************************************************
2. The user information
user pts/1 Sep 16 11:49 (:0.0)
*************************************************************
3. Login date and time
Tue Sep 16 12:01:09 IST 2014
*************************************************************
4. List of processes running on the system
PID TTY TIME CMD
2675 pts/1 00:00:00 bash
2764 pts/1 00:00:00 sh
2768 pts/1 00:00:00 ps
*************************************************************
5. User home directory
/home/user
*************************************************************

42

You might also like