Вы находитесь на странице: 1из 77

From:

Mr. Sohit Agarwal


Lecturer,
Dept. of CS/IT, JNIT
2

CONTENTS
I

(i) Introduction of UNIX 4


(ii) Types of Shell 5

Commands

1. date 8
2. who
10
3. cal
12
4. ls
14
5. ed
16
6. cat
18
7. touch
20
8. wc
22
9. more
24
10. echo
26
11. pr
28
12. bc
30
13. mkdir
32
14. rmdir
34
15. head
36
16. tail
38
17. ps
40
18. kill
42
3

19. cp
44
20. vi
46
21. pwd
48
22. sh
50
23. grep
52
24. cmp
54
25. diff
56
4

CONTENTS
Programs

1. Program to print Hello World


58
2. Program to obtain all files in current directory
58
3. Program to accept a number from user and print the same
60
4. Program to check who all are logged on
60
5. Program for addition of two numbers
62
6. Program for multiplication of two numbers
62
7. Program for adding the digits of a number
64
8. Program for reversing a number
64
9. Program to calculate Simple Interest
66
10. Program to check if given number is even or odd
66
11. Program to demonstrate syntax of while
68
12. Program to demonstrate syntax of switch
68
13. Program to calculate area and circumference of a circle
70
14. Program to check if the year is a leap year or not
70
15. Program to find the factorial of a number
72
16. Program to print the Fibonacci sequence
72
17. Program to find the shortest of three numbers
74
18. Program to find the largest of three numbers
76
19. Program to find a particular pattern
76
5

INTRODUCTION OF UNIX

What is UNIX?

UNIX is an operating system which was first developed in the 1960s, 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.

Types of UNIX

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 X. We use Solaris on our servers and workstations, and Fedora
Linux on the servers and desktop PCs.

The UNIX operating system

The UNIX operating system is made up of three parts; the kernel, the shell
and the programs.

The kernel of UNIX is the hub of the operating system. it allocates time and
memory to programs and handles the filestore and communications in
response to system calls.

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.

Everything in UNIX is either a file or a process. A process is an executing


program identified by a unique PID (process identifier). A file is a collection
of data. They are created by users using text editors, running compilers etc.
Files can be a document (report, essay etc.), the text of a program written in
some high-level programming language, etc. UNIX is really a reliable
operating system with very few number of virus attacks because it does not
support .exe, .bat extensions.
6

Types of Shell

There are 13 types of shell known. They are listed below :

1. sh
The Bourne shell, or sh, was the default Unix shell of Unix Version 7,
and replaced theThompson shell, whose executable file had the
same name, sh. It remains a popular default shell for Unix accounts.
2. ash
The Almquist shell (also known as A Shell or ash) was originally
Kenneth Almquist’s clone of the SVR4-variant of the Bourne shell; it
is a fast, small, POSIX-compatible Unix shell designed to replace the
Bourne shell in later BSD distributions.
3. dash
Debian Almquist shell (dash) is a POSIX-compliant Unix shell, much
smaller than bash. It requires less disk space but is also less feature
rich. dash is a direct descendant of the NetBSD version of the
Almquist Shell.
4. bash
Bash is a Unix shell written for the GNU Project. The name of the
actual executable is bash. Its name is an acronym for Bourne-again
shell, a pun on the name of the Bourne shell. Bash is the default
shell on most Linux systems as well as on Mac OS X and it can be
run on most Unix-like operating systems.
5. fish
fish is a Unix shell. Its name is an acronym for friendly interactive
shell. fish focuses on interactive use, discoverability, and user
friendliness.
6. ksh
7

The Korn shell (ksh) is a Unix shell is backwards compatible with the
Bourne shell and includes many features of the C shell as well. The
main advantage of ksh over the traditional Unix shell is in its use as
a programming language.

7. csh
The C shell (csh) is a Unix shell is predecessor of the Bourne shell. Its
syntax is modeled after the C programming language.

8. tcsh
tcsh is a Unix shell based on and compatible with the C shell (csh). It
is essentially the C shell with (programmable) filename completion,
command-line editing, and a few other features.
9. es
The es shell is a command line interpreter that uses a scripting
language similar to the rc shell. It is intended to provide a fully
functional programming language as a Unix shell.
10. rc
rc is the command line interpreter for Version 10 Unix, Plan 9, and
Inferno operating systems. It resembles the Bourne shell, but its
syntax is somewhat simpler.
11. scsh
Scsh is a POSIX API layered on top of the Scheme programming
language (currently only a Scheme 48 implementation exists, but
others are planned) in a manner to make the most of scheme’s
capability for scripting. It is limited to 32-bit platforms.
12. sash
Stand-alone shell (sash) is a Unix shell designed for use in
recovering from certain types of system failures.
13. zsh
The Z shell (zsh) is a Unix shell that can be used as an interactive
login shell and as a powerful command interpreter for shell scripting.
Zsh can be thought of as an extended bourne shell with a large
number of improvements, including some of the most useful
features of bash, ksh, and tcsh.

-----------------
8

OUTPUT: date COMMAND


9

Command 1
‘date’ COMMAND
NAME : date - print or set the system date and time

SYNOPSIS : date [OPTION]... [+FORMAT]


date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION

Display the current time in the given FORMAT, or set the system date.

-d, --date=STRING : display time described by STRING, not ânowâ

-f, --file=DATEFILE : like --date once for each line of DATEFILE

-r, --reference=FILE : display the last modification time of FILE

-R, --rfc-2822 : output date and time in RFC 2822 format

-s, --set=STRING : set time described by STRING

-u, --utc, --universal : print or set Coordinated Universal Time

FORMAT controls the output. The only valid option for the second form specifies Coordinated
Universal Time. Interpreted sequences are:

%a localeâs abbreviated weekday name (e.g., Sun)

%A localeâs full weekday name (e.g., Sunday)

%b localeâs abbreviated month name (e.g., Jan)

%B localeâs full month name (e.g., January)

%c localeâs date and time (e.g., Thu Mar 3 23:05:25 2005)

%C century; like %Y, except omit last two digits (e.g., 21)

%d day of month (e.g, 01)

%D date; same as %m/%d/%y

%e day of month, space padded; same as %_d

OUTPUT: who COMMAND


10

OUTPUT: ‘who am I’ COMMAND


11

Command 2
‘who’ COMMAND
NAME : who - show who is logged on

SYNOPSIS : who [OPTION]... [ FILE | ARG1 ARG2 ]

DESCRIPTION

Tells who all are logged on.

-a, --all : same as -b -d --login -p -r -t -T -u

-b, --boot : time of last system boot

-d, --dead: print dead processes

-H, --heading : print line of column headings

-l, --login : print system login processes

--lookup : attempt to canonicalize hostnames via DNS

-m : only hostname and user associated with stdin

-p, --process : print active processes spawned by init

-q, --count : all login names and number of users logged on

-r, --runlevel : print current runlevel

-s, --short: print only name, line, and time (default)

-t, --time : print last system clock change

-T, -w, --mesg : add userâs message status as +, - or ?


12

OUTPUT: ‘cal’ COMMAND

Command 3
13

‘cal’ COMMAND
NAME : cal - displays a calendar

SYNOPSIS : cal [-smjy13] [[month] year]

DESCRIPTION

Cal displays a simple calendar. If arguments are not specified, the current month is displayed. The
options are as follows:

-1 : Display single month output. (This is the default.)

-3 : Display prev/current/next month output.

-s : Display Sunday as the first day of the week. (This is the default.)

-m : Display Monday as the first day of the week.

-j : Display Julian dates (days one-based, numbered from January 1).

-y : Display a calendar for the current year.

OUTPUT: ‘ls’ COMMAND


14

OUTPUT: ‘ls -r’ COMMAND

OUTPUT: ‘ls –u’ COMMAND

OUTPUT: ‘ls –l’ COMMAND

OUTPUT: ‘ls –lut’ COMMAND


15

Command 4
‘ls’ COMMAND
NAME : ls - list directory contents

SYNOPSIS : ls [OPTION]... [FILE]...

DESCRIPTION

List information about the FILEs (the current directory by default).

-a, --all : do not ignore entries starting with .

-A, --almost-all : do not list implied . and ..

--author : with -l, print the author of each file

--block-size=SIZE : use SIZE-byte blocks

-B, --ignore-backups : do not list implied entries ending with ~

-C : list entries by columns

--color[=WHEN] : control whether color is used to distinguish file types.

-d, --directory : list directory entries instead of contents.

-F, --classify : append indicator (one of */=>@|) to entries

--file-type : likewise, except do not append â*â

-g : like -l, but do not list owner

-l : uses a long listing format.

-lt : causes the file to be listed in time order.

-lut : shows the least used files in time sequence

-u : gives information about when that file was actually used.


16

OUTPUT: ed COMMAND

Command 5
17

‘ed’ COMMAND
NAME : ed, red - text editor

SYNOPSIS : ed [-] [-Gs] [-p string] [file]


red [-] [-Gs] [-p string] [file]

DESCRIPTION

‘ed’ is a line-oriented text editor. It is used to create, display, modify and otherwise manipulate text
files.
'red’ is a restricted ed. it can only edit files in the current directory and cannot execute shell
commands.
If invoked with a file argument, then a copy of file is read into the editorâs buffer. Changes are
made to this copy and not directly to file itself. Upon quitting ed, any changes not explicitly saved
with a âwâ command are lost.

Editing is done in two distinct modes: command and input.


When first invoked, ed is in command mode. In this mode commands are read from the standard
input and executed to manipulate the contents of the editor buffer. A typical command might look
like:
,s/old/new/g
which replaces all occurrences of the string old with new.
When an input command, such as âaâ (append), âiâ (insert) or âcâ (change), is given, ed enters input
mode. This is the primary means of adding text to a file. In this mode, no commands are available;
instead, the standard input is written directly to the editor buffer. Lines consist of text up to and
including a newline character. Input mode is terminated by entering a single period (.) on a line.

OPTIONS
-G : Forces backwards compatibility.

-s : Suppresses diagnostics. This should be used if edâs standard input is from a script.

-p string : Specifies a command prompt. This may be toggled on and off with the âPâ
command.
18

OUTPUT: cat COMMAND


19

Command 6
‘cat’ COMMAND
NAME : cat - concatenate files and print on the standard output

SYNOPSIS : cat [OPTION] [FILE]...

DESCRIPTION

Concatenate FILE(s), or standard input, to standard output.

-A, --show-all : equivalent to -vET

-e : equivalent to -vE

-E, --show-ends : display $ at end of each line

-n, --number : number all output lines

-s, --squeeze-blank : never more than one single blank line

-t : equivalent to -vT

-T, --show-tabs : display TAB characters as ^I

-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
20

OUTPUT: touch COMMAND

Command 7
21

‘touch’ COMMAND

NAME : touch - change file timestamps

SYNOPSIS : touch [OPTION]... FILE...

DESCRIPTION

Update the access and modification times of each FILE to the current time. Mandatory arguments to
long options are mandatory for short options too.

-a : change only the access time

-c, --no-create : do not create any files

-d, --date=STRING : parse STRING and use it instead of current time

-m : change only the modification time

-r, --reference=FILE : use this fileâs times instead of current time

-t STAMP : use [[CC]YY]MMDDhhmm[.ss] instead of current time

If a FILE is -, touch standard output.


22

OUTPUT: wc COMMAND

Command 8
23

‘wc’ COMMAND

NAME : wc - print the number of newlines, words, and bytes in files

SYNOPSIS : wc [OPTION]... [FILE]…

DESCRIPTION

Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is
specified.

-c, --bytes : print the byte counts

-m, --chars : print the character counts

-l, --lines : print the newline counts

-L, --max-line-length : print the length of the longest line

-w, --words : print the word counts


24

OUTPUT: more COMMAND

Command 9
25

‘more’ COMMAND

NAME : more - file perusal filter for crt viewing

SYNOPSIS : more [-dlfpcsu] [-num] [+/ pattern] [+ linenum] [file ...]

DESCRIPTION

More is a filter for paging through text one screenful at a time.

OPTIONS

-num : This option specifies an integer which is the screen size (in lines).

-d : more will prompt the user with the.

-l : more usually treats ^L (form feed) as a special character, and will pause after
any line that contains a form feed. The -l option will prevent this
behavior.

-f : Causes more to count logical, rather than screen lines

-p : Do not scroll. Instead, clear the whole screen and then display the text.

-c : Do not scroll. Instead, paint each screen from the top, clearing the remainder
of each line as it is displayed.

-s : Squeeze multiple blank lines into one.

-u : Suppress underlining.
26

OUTPUT: echo COMMAND

Command 10
27

‘echo’ COMMAND

NAME : echo - display a line of text

SYNOPSIS : echo [OPTION]... [STRING]...

DESCRIPTION

Echo the STRING(s) to standard output.

-n : do not output the trailing newline

-e : enable interpretation of backslash escapes

-E : disable interpretation of backslash escapes (default)

If -e is in effect, the following sequences are recognized:

\0NNN : the character whose ASCII code is NNN (octal)

\\ : backslash

\a : alert (BEL)

\b : backspace

\c : suppress trailing newline

\f : form feed

\n : new line

\r : carriage return

\t : horizontal tab

\v : vertical tab

OUTPUT: pr COMMAND
28

Command 11
29

‘pr’ COMMAND

NAME : pr - convert text files for printing

SYNOPSIS : pr [OPTION]... [FILE]...

DESCRIPTION

Paginate or columnate FILE(s) for printing.


Mandatory arguments to long options are mandatory for short options too.

-a, --across : print columns across rather than down, used together with -COLUMN

-c, --show-control-chars : use hat notation (^G) and octal backslash notation

-d, --double-space : double space the output

-D, --date-format=FORMAT: use FORMAT for the header date

-F, -f, --form-feed : use form feeds instead of newlines to separate pages.

-h HEADER : use a centered HEADER instead of filename in page header.

-m, --merge : print all files in parallel, one in each column, truncate lines, but
join lines of full length with -J

pr filename1 filename2 : Calumnates both the files for printing.

Pr -3 filename : Makes 3 columns for the text.


30

OUTPUT: bc COMMAND

Command 12
‘bc’ COMMAND
31

NAME : bc - An arbitrary precision calculator language

SYNOPSIS : bc [ -hlwsqv ] [long-options] [ file ... ]

DESCRIPTION

bc is a language that supports arbitrary precision numbers with interactive execution of statements.
There are some similarities in the syntax to the C programming language. A standard math library is
available by command line option. If requested, the math library is defined before processing any
files.
bc starts by processing code from all the files listed on the command line in the order listed. After
all files have been processed, bc reads from the standard input. All code is executed as it is read.

OPTIONS:
-h, --help : Print the usage and exit.

-i, --interactive : Force interactive mode.

-l, --mathlib : Define the standard math library.

-w, --warn : Give warnings for extensions to POSIX bc.

-s, --standard : Process exactly the POSIX bc language.

-q, --quiet : Do not print the normal GNU bc welcome.

-v, --version : Print the version number and copyright and quit.

OUTPUT: mkdir COMMAND


32

Command 13
‘mkdir’ COMMAND
33

NAME : mkdir - make directories

SYNOPSIS : mkdir [OPTION] DIRECTORY...

DESCRIPTION

Create the DIRECTORY(ies), if they do not already exist.


Mandatory arguments to long options are mandatory for short options too.

-Z, --context=CONTEXT (SELinux) : set security context to CONTEXT

-m, --mode=MODE : set permission mode, not rwxrwxrwx- umask

-p, --parents : no error if existing, make parent directories as needed

-v, --verbose : print a message for each created directory

OUTPUT: rmdir COMMAND


34

Command 14
‘rmdir’ COMMAND
35

NAME : rmdir - remove empty directories

SYNOPSIS : rmdir [OPTION]... DIRECTORY...

DESCRIPTION

Remove the DIRECTORY(ies), if they are empty.

--ignore-fail-on-non-empty : ignore each failure that is solely as the directory is non-empty

-p, --parents : Remove DIRECTORY and its ancestors.

-v, --verbose : output a diagnostic for every directory processed


36

OUTPUT: head COMMAND

Command 15
‘head’ COMMAND
37

NAME : head - output the first part of files

SYNOPSIS : head [OPTION]... [FILE]...

DESCRIPTION

Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with
a header giving the file name. With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.

-c, --bytes=[-]N : print the first N bytes of each file; with the leading â-â, print all but the
last N bytes of each file.

-n, --lines=[-]N : print the first N lines instead of the first 10; with the leading â-â, print
all but the last N lines of each file.

-q, --quiet, --silent : never print headers giving file names

-v, --verbose : always print headers giving file names

OUTPUT: tail COMMAND


38

Command 16
‘tail’ COMMAND
39

NAME : tail - output the last part of files

SYNOPSIS : tail [OPTION]... [FILE]...

DESCRIPTION

Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with
a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.

--retry : keep trying to open a file even if it is inaccessible when tail starts or if
it becomes inaccessible later; useful when follow- ing by name.

-c, --bytes=N : output the last N bytes

-F : same as --follow=name --retry

-n, --lines=N : output the last N lines, instead of the last 10

--pid=PID : with -f, terminate after process ID, PID dies

-q, --quiet, --silent : never output headers giving file names

-s, --sleep-interval=S: with -f, sleep for approximately S seconds(default 1) between


iterations.

-v, --verbose : always output headers giving file names


40

OUTPUT: ps COMMAND

PID : Process Id
TTY : Terminal Type

Command 17
‘ps’ COMMAND
41

NAME : ps - report a snapshot of the current processes.

SYNOPSIS : ps [options]

DESCRIPTION

ps displays information about a selection of the active processes. If you want a repetitive update of
the selection and the displayed information, use top(1) instead.

Options of different types may be freely mixed, but conflicts can appear. There are some
synonymous options, which are functionally identical, due to the many standards and ps
implementations that this ps is compatible with.

EXAMPLES
To see every process on the system using standard syntax:
ps -e
ps -ef
ps -eF
ps -ely

To see every process on the system using BSD syntax:


ps ax
ps axu

To print a process tree:


ps -ejH
ps axjf

To get info about threads:


ps -eLf
ps axms

To get security info:


ps -eo euser,ruser,suser,fuser,f,comm,label
ps axZ
ps –eM
42

OUTPUT: kill COMMAND

EFFECT : Since we are supplied the process id of the bash process, as soon as we press enter the
bash process is killed and we are logged out.

Command 18
43

‘kill’ COMMAND

NAME : kill - terminate a process

SYNOPSIS : kill [ -s signal | -p ] [ -a ] [ -- ] pid ...


kill -l [ signal ]

DESCRIPTION

The command kill sends the specified signal to the specified process or process group. If no signal
is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this
signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot
be caught.

OPTIONS
pid... Specify the list of processes that kill should signal. Each pid can be one of five things:

n : where n is larger than 0. The process with pid n will be signaled.

0 : All processes in the current process group are signaled.

-1 : All processes with pid larger than 1 will be signaled.

-n : where n is larger than 1. All processes in process group n are signaled.

-s signal : Specify the signal to send. The signal may be given as a signal name or no.

-l : Print a list of signal names. These are found in /usr/include/linux/signal.h

-a : Do not restrict the commandname-to-pid conversion to processes with the


same uid as the present process.

-p : Specify that kill should only print the process id (pid) of the named
processes, and not send any signals.
44

OUTPUT: cp COMMAND

Command 19
‘cp’ COMMAND
45

NAME : cp - copy files and directories

SYNOPSIS : cp [OPTION]... [-T] SOURCE DEST


cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.


Mandatory arguments to long options are mandatory for short options too.

-a, --archive : same as -dpR

--backup[=CONTROL] : make a backup of each existing destination file

-b : like --backup but does not accept an argument

--copy-contents : copy contents of special files when recursive

-d : same as --no-dereference --preserve=link

-f, --force : if existing destination file can’t be opened,remove it & try again

-i, --interactive : prompt before overwrite

-H : follow command-line symbolic links

-l, --link : link files instead of copying

-L, --dereference : always follow symbolic links

-P, --no-dereference : never follow symbolic links

-p : same as --preserve=mode, ownership, timestamps

--parents : use full source file name under DIRECTORY

-R, -r, --recursive : copy directories recursively


46

OUTPUT: vi COMMAND

Command 20
‘vi’ COMMAND
47

NAME : vim - Vi IMproved, a programmers text editor

SYNOPSIS : vim [options] [file ..]


vim [options] -
vim [options] -t tag
vim [options] -q [errorfile]
ex
view
gvim gview evim eview
rvim rview rgvim rgview

DESCRIPTION

Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text.
It is especially useful for editing programs. There are a lot of enhancements above Vi: multi level
undo, multi windows and buffers, syntax highlighting, command line editing, filename
completion, on-line help, visual selection, etc.

While running Vim a lot of help can be obtained from the on-line help system, with the ":help"
command.
Most often Vim is started to edit a single file with the command vim file
More generally Vim is started with:
vim [options] [filelist]

STEPS in using vi :-

1. Write vi filename and press enter.


2. In the editing environment, press Insert.
3. Write whatever you want to write.
4. To save and exit press – Esc:wq
5. Now run the file using sh command.
48

OUTPUT: pwd COMMAND


49

Command 21
‘pwd’ COMMAND

NAME : pwd - print name of current/working directory

SYNOPSIS : pwd [OPTION]

DESCRIPTION

Print the full filename of the current working directory.


50

OUTPUT: sh COMMAND

Command 22
‘sh’ COMMAND
51

NAME : bash - GNU Bourne-Again SHell

SYNOPSIS : bash [options] [file]

DESCRIPTION

Bash is an sh-compatible command language interpreter that executes commands read from the
standard input or from a file. Bash also incorporates useful features from the Korn and C shells.

Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools
specification (IEEE Working Group 1003.2). Bash can be configured to be POSIX-conformant by
default.

OPTIONS
In addition to the single-character shell options documented in the description of the set builtin
command, bash interprets the following options when it is invoked:

-i : If the -i option is present, the shell is interactive.

-l : Make bash act as if it had been invoked as a login shell.

-r : If the -r option is present, the shell becomes restricted.

--login : Equivalent to -l.

OUTPUT: grep COMMAND


52

Command 23
‘grep’ COMMAND
53

NAME : grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS : grep [options] PATTERN [FILE...]


grep [options] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION

grep searches the named input FILEs (or standard input if no files are named, or the file name - is
given) for lines containing a match to the given PATTERN. By default, grep prints the matching
lines.
In addition, two variant programs egrep and fgrep are available. Egrep is the same as grep -E.
Fgrep is the same as grep -F.

OPTIONS

-a, --text : Process a binary file as if it were text; this is equivalent to the --binary-
files=text option.

-b, --byte-offset : Print the byte offset within the input file before each line of output.

-E, --extended-regexp: Interpret PATTERN as an extended regular expression.

-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.
54

OUTPUT: cmp COMMAND

Command 24
‘cmp’ COMMAND
55

NAME : cmp - compare two files

SYNOPSIS : cmp [-l | -s] file1 file2 [skip1 [skip2]]

DESCRIPTION

The cmp utility compares two files of any type and writes the results to the standard output. By
default, cmp is silent if the files are the same; if they differ, the byte and line number at which the
first difference occurred is reported. Bytes and lines are numbered beginning with one.
The following options are available:

-l Print the byte number (decimal) and the differing byte values (octal) for each difference.

-s Print nothing for differing files; return exit status only.

The optional arguments skip1 and skip2 are the byte offsets from the beginning of file1 and file2,
respectively, where the comparison will begin. The offset is decimal by default, but may be
expressed as an hexadecimal or octal value by preceding it with a leading ââ0xââ or ââ0ââ.

The cmp utility exits with one of the following values:

0 The files are identical.

1 The files are different; this includes the case where one file is identical to the first part of the
other. In the latter case,
56

OUTPUT: diff COMMAND

Command 25
‘diff’ COMMAND
57

NAME : diff - find differences between two files

SYNOPSIS : diff [options] from-file to-file

DESCRIPTION

In the simplest case, diff compares the contents of the two files from-file and to-file. A file name
of - stands for text read from the standard input. As a special case, diff - - compares a copy of
standard input to itself.

If from-file is a directory and to-file is not, diff compares the file in from-file whose file name is
that of to-file, and vice versa. The non-directory file must not be -.

If both from-file and to-file are directories, diff compares corresponding files in both directories, in
alphabetical order; this comparison is not recursive unless the -r or --recursive option is given

-a : Treat all files as text and compare them line-by-line, even if they do not seem
to be text.

-b : Ignore changes in amount of white space.

-B : Ignore changes that just insert or delete blank lines.

--brief : Report only whether the files differ, not the details of the differences.

-c : Use the context output format.

-D name : Make merged if-then-else format output, conditional on the preprocessor


macro name.

-e : --ed Make output that is a valid ed script.

OUTPUT – firstprogram
58

OUTPUT - demols

PROGRAM – 1
59

Write a UNIX program to print HELLO WORLD on the screen

[sohit@localhost~]vi firstprogram

echo HELLO WORLD !!

PROGRAM – 2

Write a UNIX program to show the files or directories in the current directory on the screen.

[sohit@localhost~]vi demols

echo The files and directories in this current directory are :

ls

OUTPUT – demoinput
60

OUTPUT - demowho

PROGRAM – 3
61

Write a UNIX program to get a number as input from the user and print the same on the screen.

[sohit@localhost~]vi demoinput

echo “Please enter the number : “


read a
echo “The number you entered is $a”

PROGRAM – 4

Write a UNIX program to show all the users that have logged on.

[sohit@localhost~]vi demowho

echo “Following users are currently logged in : ”

who

OUTPUT – sum
62

OUTPUT - multiply

PROGRAM – 5
63

Write a UNIX program to add two numbers.

[sohit@localhost~]vi sum

echo “This is the program for Addition ”

echo “Enter the first number : ”


read a
echo “Enter the second number : ”
read b

c = ‘expr $a + $b’

echo “The sum of these numbers is : $c”

PROGRAM – 6

Write a UNIX program to multiply two numbers.

[sohit@localhost~]vi multiply

echo “This is the program for Multiplication ”

echo “Enter the first number : ”


read a
echo “Enter the second number : ”
read b

c = ‘expr $a \* $b’

echo “The sum of these numbers is : $c”

OUTPUT – lab (sum of digits)


64

OUTPUT – lab1 (reverse)

PROGRAM – 7
65

Write a UNIX program to add the digits of a number.

[sohit@localhost~]vi lab

echo “Enter a 5 digit Number : ”


read n

a=0
sum=0
while [$n –gt 0]
do
a = ‘expr $n % 10’ OUTPUT – simpleinterest
b = ‘expr $n / 10’
sum = ‘expr $sum +$a’
done
echo “Sum of the digits of this Number is $sum”

PROGRAM – 8

Write a UNIX program to reverse a number.

[sohit@localhost~]vi lab1

echo “Enter the Number : ”


read a

x=0
while [$a –ne 0]
do
b = ‘expr $a % 10’
x = ‘expr $x + $b’ OUTPUT – simpleinterest
x = ‘expr $x \* 10’
a = ‘expr $a / 10’
done
x = ‘expr $x / 10’
echo “$x”

OUTPUT – lab2 (simpleinterest)


66

OUTPUT - odd

PROGRAM – 9
67

Write a UNIX program to find the Simple Interest.

[sohit@localhost~]vi lab2

echo “This is the program for Simple Interest ”

echo “Enter Principal : ”


read p
echo “Enter Rate per annum : ”
read r
echo “Enter time in years : ”
read t

c = ‘expr $p \* $r \* $t/100’

echo “Calculated Simple Interest is : Rs $c”

PROGRAM – 10

Write a UNIX program to find if the given number is even or odd.

[sohit@localhost~]vi odd

echo “Enter the first number : ”


read a

b = ‘expr $a%2’

if[$b –eq 0]
then
echo “Even number”
else
echo “Odd number”
fi

OUTPUT – while
68

OUTPUT – switch

PROGRAM – 11
69

Write a simple UNIX program to show the syntax of while.

[sohit@localhost~]vi while

echo "enter a"


read a
while [ $a -lt 3 ]
do
echo "this statement is not working for me"
echo "enter a"
read a
done

PROGRAM – 12

Write a simple UNIX program to show the syntax of switch.

[sohit@localhost~]vi switch

echo "Enter any number"


read a"Enter any number"
echo
read$a
case a
in $a
case
in echo "1";;
1)
2) echo "2";;
1) "1";;
2) echo
3) echo "3";;
"2";;
*) echo "greater
3) "3";; than 3";;
*) echo "greater than 3";;
esac
esac

OUTPUT – circle
70

OUTPUT – leap

PROGRAM – 13
71

Write a UNIX program to calculate the area and circumference of a given circle.

[sohit@localhost~]vi circle

echo "Enter the radius of the circle"


read a

echo "The circumference of the circle is:"


echo `expr 2 \* 3 \* $a`

echo "The area of the circle is:"


echo `expr 3 \* $a \* $a`

PROGRAM – 14

Write a UNIX program to check if the given year is a leap year.

[sohit@localhost~]vi leap

echo "enter the year"


read yr

if [ `expr $yr % 100` -eq 0 -a `expr $yr % 400` -eq 0 ]


then
echo "yr is a leap year"
elif [ `expr $yr % 100` -ne 0 -a `expr $yr % 4` -eq 0 ]
then
echo "$yr is a leap year"
else
echo "$yr is not a leap year"
fi

OUTPUT – fact
72

OUTPUT – fibo

PROGRAM – 15
73

Write a UNIX program to calculate the area factorial of a number.

[sohit@localhost~]vi fact

echo "Enter the no."


read n
fact=1
i=1
while [ $i -le $n ]
do
fact=` expr $i \* $fact`
i=` expr $i + 1`
done
echo "Fact of a no $fact "

PROGRAM – 16

Write a UNIX program to print the Fibonacci sequence.

[sohit@localhost~]vi fibo

echo "Enter the value of n : "


read n
i=2
a=0
b=1
echo " 0 "

while [ $i -lt $n ]
do

c=` expr $b + $a`


echo " $c "
b=` expr $a`
a=` expr $c`

i=` expr $i + 1`
done

OUTPUT – smallest
74

PROGRAM – 17
75

Write a UNIX program to find the smallest of three given numbers.

[sohit@localhost~]vi smallest

echo "Enter a"


read a
echo "Enter b"
read b
echo "Enter c"
read c

if [ $a -gt $b ]
then
if [ $b -gt $c ]
then echo "c is smallest"
else
echo "b is smallest"
fi
else
if [ $a -gt $c ]
then echo "c is smallest"
else
echo "a is smallest"
fi
fi

OUTPUT – largest
76

OUTPUT – pattern

PROGRAM – 18
77

Write a UNIX program to find the largest of three given numbers.

[sohit@localhost~]vi largest

echo "Enter a"


read a
echo "Enter b"
read b
echo "Enter c"
read c

if [ $a -gt $b -a $a -gt $c ]
then
echo "a is largest"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "b is largest"
else
echo "c is largest"
fi

PROGRAM – 19

Write a UNIX program to demonstrate pattern matching.

[sohit@localhost~]vi pattern

echo "Enter the pattern to be searched: \c"


read pname
echo "Enter the file to be used: \c"
read flname
echo "Searching for $pname from file $Flname"
grep "$pname" $flname
echo "Selected records shown above"

Вам также может понравиться