You are on page 1of 12

PRACTICAL-1

AIM: Study of Linux operating system (Kernel, shell, Basic commands, Pipe and Filter
commands)

What isLinux?

The primary author of Linux is Linus Torvalds. Since his original versions, it has been
improved by countless numbers of people. Linux is a freely distributed, multitasking,
multiuser operating system that behaves like UNIX. The term Li is actually somewhat
vague. L is used in two ways: specifically to refer to the kernel itself the heart of
any version of Linux and more generally to refer to any collection of applications that run
on the kernel, usually referred to a distribution. The kerne job is to provide the basic
environment in which applications can run, including the basic interfaces with hardware.
Today, thousands of software developers are busy upgrading it, all the while, and
extending support online.
Today, Linux is used for a variety of applications. This includes:
File and Print Server
E-mail Server
Fax Server
Internet gateway
Firewall
Database Server
ISP Server
Application Server
Desktop OS

Basic Features of Linux Operating System

Linux systems excel in many areas, ranging from end user concerns such as
stability, speed, and ease of use, to serious concerns such as development and
networking.
The important features are listed here.
Multi-programming Time Sharing Multi-tasking
High Speed Virtual memory Shared libraries
POSIX compliance X concept Samba
Dos emulator CRON scheduler Office suits
Data archiving utilities Licensing Web server
Network information service(NIS)
Support for programming languages
Text processing and Word processing
Linux architecture

1. .Linux kernel
The central nervous system of Linux is the kernel, the operating system code that runs
the whole computer. The Linux kernel is the heart of Linux operating system and was
originally developed for the Intel 80386 CPU's. Mem01y management is especially
strong with the 80386 (compared to earlier CPU's). Linux kernel has the ability to have
full access to the entire hardware capabilities of the machine. Actually there is no
restriction on what a kernel module is allowed to do.

Typically a kernel might implement a device driver, a file system or a networking


protocol. To support 1t large memory requirements when only small amounts of physical
RAM are available, Linux suppo1ts swap space. Swap space allows pages of memory to
be written to a reserved area of a disk and treated as an extension of physical memory.
By moving pages back and forward between the swap space and RAM, Linux can
effectively behave as if it had much more physical RAM than it does. Besides this,
Linux kernel supports the following features:

Has memory protection between processes, so that one program can't


bring the whole system down.
Demand loads executable: Linux only. reads from disk those parts of a
program that are actually use.

2. Linux Shell
A shell is a program that run every time you log in. It is a program that lets you interact with
t even notice the fact that they're manning a shell. They just
know that a prompt appears and that from there, they can read their mail and perform other
tasks. This prompt is the shell that waits for you to tell what to do. Thus, Linux shell is a
user program that the kernel runs when you log in. The shell acts as an interface between the
user and the Linux operating system and helps in command inte1pretation for the kernel.

The figure shows that each user gets a copy of the shell where he is allowed to work.
The shell interacts with the kernel through system calls. System calls provide a set of
routines that allow an application to access kernel services.
The structure of the Linux operating system is as follows:

U
User 1 Programs User 2 Programs User 3 Programs s
e
r
Shell Shell Applica on 1 Applica on 2

System Calls K
e
r
n
Memory Process File Network I/O e
Manager Manager Manager Manager l
Manager

Kernel

Hardware

Structure of Linux operating system

Shells available in Linux

We have several different shells available here:


sh
The first shell, historically, was sh also known as the Bourne shell. It is good for
Writing shell scripts, but not so popular for interactive use.
csh
Also known as C-Shell, csti features a syntax somewhat like the C language. It allows
(among other things) adding your commands (aliasing), hist01y substitution (re-
execution of previously typed commands), and filename-completion.
tcsh
This shell allows you to edit your command line while you're typing it, using emacs
like commands. It has a number of other nifty features, but is otherwise compatible
with csh.
zsh
The z shell has the best of the features of the Tcsh shell. It also has the capability to
emulate all the features of the. Kim shell and supports a large number of utilities and a
detailed documentation.
bash
Bash is an acronym for 'BoUine again shell'. It is an enhancement to the BoUine shell
and is the default shell for most of the Linux systems. Compatible with sh for
programming purposes, it has many of the good features of csh and tcsh: file name
completion, job control, history substitution, emacslike command-line editing, and
many more.

Pdksh
Pdksh stands for public domain Kom shell and is an enhancement of the Kom shell. It
has been Witten by several voluntary programmers. On Linux systems, ksh is the
symbolic link to the pdksh shell.

Basic Linux commands

The date command


Linux maintains a system clock. As for now you can simply display the current date
with the date command, which shows the date and time for the nearest second as shown:
$ date
Thur Nov 4 11:23:52 IST 1999

The who command


Linux maintains an account of all the current users of the system. A list of all the users
is displayed by the who command. The who command produces a three column output
.This indicates the number of users of the system with their login names in the first
column. The second column shows the device names of their respective terminals. The
third column indicates the date and time of logging in.
The H option prints the column headers:
$ who H

The tty command


Linux treats even terminals as files. It is therefore reasonable to expect a command which
tells you the device name of the terminal you are using with the help of the tty command.
$ tty
/dev/ttyp1
$

The cal command


This command is used for printing the calendar of any particular month or the entire year.
Any calendar from the year 1 to 9999 can be displayed with this command:
$ cal 2000

The man command


Linux systems maintain an online documentation about each command so that you can get to
know the complete description about a particular command. The command:
$ man
will give a description of the man command in general.
The man command is used to view the description of other commands. For this purpose the
man command is followed by the command name. It has the following format:
$ man command_name

The finger utility


This utility is used to display the status of all the users currently logged on to the Linux
system. The finger utility without any parameter, displays a single line output for each user
currently.

name, write status, idle time, login time, the


logged in and the office number. The write permission is displayed along with the
terminal name as an asterisk(*). If the asterisk appears after the terminal name, it means
that the write permission is denied. The syntax of the finger command is as follows:
finger [options] [user name]

The chfn utility

etc/passwd file
and allows the user to chane information. The syntax for this utility is as follows:
chfn [options] [username]
The chfn asks for the password of the user to authenticate the user for changing the finger
information. If you do not want to change any particular information, you can press Enter
at the corresponding prompt and proceed with the remaining parameters.

The head command


The head command is used to display the top few records of the file. The syntax of
the command is as follows:
$ head [option] file
The option is as follows:
count Display the first count lines of file
The easiest way to use this command is to specify a filename without specifying
the number of lines to be displayed. If this is the case, the first ten records of the
file are displayed.
$ head tmp.lst
will display the first ten records of the file tmp.lst.

The tail command


The tail command displays the end few records of the file. If no line count is given, the
tail command displays the last ten lines of the file. We also have an option to specify a
count and select that many lines from the end of file.
Consider an example: $ tail 3 emp.lst

The mesg command


The user has the option to allow or disallow other users to write on his terminal. The two
options available with the mesg command are as follows:
mesg y
This option allows other users to write to your terminal
mesg n
This option disallows other users to write to your terminal
If you type the mesg command without any option, it displays the status of your
terminal, say for example,
$ mesg
is y

The wall command


The wall command is used to write to all the users. The wall command sends the message
to all the users who are currently logged on to the Linux system and have their mesg
f the command is as follows:

wall type in the above command at the command prompt. Press <Enter>
Now write the message you want to broadcast. Press ctrl <d>. The message would be
broadcasted to all the users currently logged on.

pwd (Print working directory) command


Use the pwd command to print the working directory (the current directory you are in).

> pwd
/home
> cd /home/rich/www
> pwd
/home/rich/www
>

Line 1 of this example shows the command pwd has been entered
Line 2 displays, or 'prints' the output of the pwd command (ie: the directory you are in -
/home in this case)
Line 3 uses the cd command (change directory) to move to the /home/rich/www directory
Line 4 enters the pwd command again
Line 5 shows we are now in the /home/rich/www directory
Line 6 is the prompt again

cd (Change directory) command


Use the cd command to change to another directory.

The syntax is cd followed by the name of the directory you want to go to.
Example: cd /home/user/www will change the directory you are in to /home/user/www.

> cd /home/rich/www
> pwd
/home/rich/www
>
Line 1 shows the command cd /home/rich/www, which should put me in the folder
/home/rich/www
Line 2 is the pwd command (print working directory) to see if we are in the right directory
Line 3 is the output of the pwd command - which shows that we are indeed in
/home/rich/www
Line 4 is the prompt again

Pipe Command
If you have a series of commands in which the output of one command is the input of the
next, you can pipe the output without saving it in temporary files:
first_command | next_command
For example, if you wanted to point out a s01ted version of a file that contained a list of
names
and phone numbers, you could use a pipe (as well as input redirection):
sort < my_phone_list|lpr
Similarly, in order to display the contents of the current directory, a screen-full at a time, you
can give the following commands:
$ls > myfile
$ more myfile
Here, the listing of the direct01y is stored in the file, myfile, by the first co and this file
is then used as input by the more command.
The above two steps can be combined and executed as a single command without
creating a temporary file,
$ ls| more
The ve1tical bar (|) is the pipe character which indicates to the shell that the output of
the command before the '|' is the input to the command after the '|'.

Filter Commands
A filter is a program that takes its input from the standard input file, processes (or filters)it
and sends its output to file standard output file. Linux has a rich set of filters that can be used
to work on data in an efficient way. Some examples of filters are:

The grep command


The grep command searches a file for a specified pattern ad displays it on screen. The syntax
of this command is as follows:
grep[options] regular expression filename[s]

The tr command
The tr command is used to squeeze the space between columns or it transliterates characters
i.e. it copies the standard input to standard output with substitution or deletion of specific
characters.

The sort command


The sort command sorts lines of all the specified files together and writes the result on the
standard output.
Example
$ sort myfile
will sort the file considering each record of a file as one single field.

The wc command
This command is used to count the number of lines, words and characters in specified file or
standard input.
Three options are possible:
-w for words
-l for lines
-c for characters

The cut command


This command is used to delete some columns in multi columnar output.

Some more basic linux commands

Command Description

Ls lists the content of a directory

Cd change directory

cd .. parent directory

Mkdir creates a new directory

Rmdir eliminates a directory

Cp copy a file

Mv moves a file

Rm removes a file

Passwd
changes the user's password
Cat displays the file's content and creates
file

More displays the file's content with pauses

Lpr prints the requested file


PRACTICAL-2

AIM: Study of shell programming.

Normally shells are interactive. It means shell accept command from you (via keyboard) and execute
them. But if you use command one by one (sequence of 'n' number of commands) , the you can store
this sequence of command to text file and tell the shell to execute this text file instead of entering the
commands. This is known as shell script.

Shell script defined as:

"Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-
DOS but have more power than the MS-DOS batch file."

Benefits of shell script

Shell script can take input from user, file and output them on screen.
Useful to create our own commands.
Save lots of time.
To automate some task of day today life.
System Administration part can be also automated.

How to write shell script

Following steps are required to write shell script:

(1) Use any editor like vi or mcedit to write shell script.

(2) After writing shell script set execute permission for your script as follows

syntax:

chmod permission your-script-name

Examples:

$ chmod +x your-script-name
$ chmod 755 your-script-name

Note: This will set read write execute(7) permission for owner, for group and other permission is read
and execute only(5).

(3) Execute your script as

syntax:

bash your-script-name
sh your-script-name
./your-script-name

Examples:
$ bash bar
$ sh bar
$ ./bar

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command
file in current shell without starting the new copy of shell, The syntax for . (dot) command is as
follows

Syntax:
. command-name

Example:
$ . foo

FIRST SHELL SCRIPT THAT WILL PRINT "KNOWLEDGE IS POWER" ON SCREEN

$ vi first
#
# My first shell script
#
clear
echo "Knowledge is Power"

After saving the above script, you can run the script as follows:
$ ./first

This will not run script since we have not set execute permission for our script first; to do this type
command
$ chmod 755 first
$ ./first

First screen will be clear, then Knowledge is Power is printed on screen.

Script Command(s) Meaning


$ vi first Start vi editor
# followed by any text is considered as
comment. Comment gives more information
#
about script, logical explanation about shell
# My first shell script
script.
#
Syntax:
# comment-text
Clear clear the screen
To print message or value of variables on
screen, we use echo command, general form of
echo "Knowledge is Power" echo command is as follows
syntax:
echo "Message"
Variables in shell

To process our data/information, data must be kept in computers RAM memory. RAM memory is
divided into small locations, and each location had unique number called memory location/address,
which is used to hold our data. Programmer can give a unique name to this memory location/address
called memory variable or variable (Its a named storage location that may take different values, but
only one at a time).

In Linux (Shell), there are two types of variable:

(1) System variables - Created and maintained by Linux itself. This type of variable defined in
CAPITAL LETTERS.

(2) User defined variables (UDV) - Created and maintained by user. This type of variable
defined in lower letters. You can see system variables by giving command like $ set,
some of the important System variables are:

System Variable Meaning


BASH=/bin/bash Our shell name

BASH_VERSION=1.14.7(1) Our shell version name

COLUMNS=80 No. of columns for our screen

HOME=/home/vivek Our home directory


LINES=25 No. of columns for our screen

LOGNAME=students students Our logging name

OSTYPE=Linux Our Os type

PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings

PS1=[\u@\h \W]\$ Our prompt settings

PWD=/home/students/Common Our current working directory

SHELL=/bin/bash Our shell name

USERNAME=vivek User name who is currently login to this PC

You might also like