0% found this document useful (0 votes)
5 views

Linux

The document provides a comprehensive overview of the UNIX operating system architecture, detailing components such as the kernel, shell, and utilities. It also covers essential commands and tools like vi editor, grep, sed, and awk, along with their functionalities and examples. Additionally, it includes information on shell variables and a sample shell script for a menu-driven program.

Uploaded by

phtkgmz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Linux

The document provides a comprehensive overview of the UNIX operating system architecture, detailing components such as the kernel, shell, and utilities. It also covers essential commands and tools like vi editor, grep, sed, and awk, along with their functionalities and examples. Additionally, it includes information on shell variables and a sample shell script for a menu-driven program.

Uploaded by

phtkgmz
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Explain the architecture of UNIX Operating system?

The kernel is the core part of an operating system (OS) It acts as an interface
between the software (applications) and hardware (CPU, memory, etc.). It manages
system resources, such as memory, processes, device drivers, and file systems, and
ensures that programs can run effectively without directly accessing hardware. When
user logins into system, kernel checks whether he/she is an authorized user or not.
there will be only one kernel for an O.S
Shell is known as a command-line Interpreter. It acts as an interface between user
& Kernal(e.g., Bash, Zsh).
Utility refers to a small, specialized program designed to perform a specific task
or set of tasks. Utilities are typically command-line tools that help users manage
and manipulate files, processes, system resources, and perform other system
administration functions.
Features of Linux:
Open Source
Multi Tasking Capability
Multitasking with Multi-user Capabilities
Security
Portability
Stability and Reliability
Customizability
Command Line Interface
Graphical User Interface (GUI)

->vi Editor in Linux


It is easy to invoke Vi.
Syntax: $vi filename
Modes of Operation in the vi editor
There are three modes of operation in vi:
Command Mode
Escape Mode
Insert Mode

Inserting Commands:
There are many ways to be in insert mode
i  Command Type (Press i)
a  Inserts text after current Cursor Location.
A  Inserts text at the end of current Cursor Location.
Deleting Characters:
X(Upper Case)  Deletes Character before Cursor Location.
x(Lower Case)  Deletes Character at Cursor Location

Copy & Paste:


yy  Copies current Line
P  puts the copied text after cursor

save and exit:

pwd Command Prints path for current working directory


Syntax: $ pwd
mkdir Command  It is used to create a new directory .
Syntax: $mkdir [option] directory_name
We can create multiple directories The following would create three directories in
the current directory with the names dir1, dir2 and dir3: mkdir dir1 dir2 dir3
Options:
-p -> When the -p option is specified, mkdir creates all of the directories in the
path
mkdir with -p option
:$ mkdir -p a/b/c

CHANGING DIRECTORIES: cd The cd(change directory) shell command changes a shell's


current working directory to directory Name. If the directory Name argument is
omitted, the shell is moved to is owner's home directory .
Syntax: cd [directory Name]
1. changes current working directory in to specified directory.
:~$ ]cd a
:~/a]$
2. using 'cd' (or) 'cd ~' command with out any arguments it changes to home
directory
:~/a/b/c]$ cd (or) :~/a/b/c]$ cd ~
:~]$

REMOVING DIRECTORY: rmdir


The rmdir utility removes all of the directories from the list of directory names.
A directory must empty before it can be removed.
Syntax: rmdir directoryName
Listing files and directories: ls
'ls' utility is used to lists the information about file or a directory .Its
synopsis is given as
Syntax: ls [OPTION]... [FILE]...

Long listing of files use –l option


$ls –l
-rw-rw-r-- 1 exam6 exam6 38 Jun 23 15:03 file3
-rw-rw-r-- 1 exam6 exam6 61 Jun 23 15:06 file4
-rw-rw-r-- 1 exam6 exam6 28 Jun 23 15:24 file5
drwxrwxr-x 2 exam6 exam6 4096 Jun 23 15:28 unix

Open Last Edited File Using ls -t


To open the last edited file in the current directory use the combination of ls,
head and vi commands as shown below.
ls -t sorts the file by modification time, showing the last edited file first. head
-1 picks up this first file.

Display One File Per Line Using ls -1


To show single entry per line, use -1 option.
Display Files Recursively Using ls -R
To show all the files recursively, use -R option. When you do this from /, it shows
all the unhidden files in the whole file system recursively.

Display File Inode Number Using ls –i


Sometimes you may want to know the inone number of a file for internal maintenance.
Use -i option to display inode number.

Explain grep, egrep and fgrep with examples?


Grep:
• Grep stands for “global regular expression print “
• A regular expression is a pattern consisting of sequence of character that is
matched against text.
• Grep is a family of programs that is used to search the input file for all lines
that matches a specified regular expression and writes them to the standard output
Syntax : $ grep options regexp filename
Example :
$ cat sa
hello unix programming
unix utilities
file handling
process handling
disc utilities
$ grep unix sa /* search for the word unix */
hello unix programming
unix utilities
Options:
(1)-n –> it assign line numbers on the standard out put stream.
Example : $ grep –n unix sa
1:hello unix programming
2:unix utilities
6:unix advanced
(2)–w –> it display whole word on the standard output stream .
Ex: $ grep -w unix sa
hello unix programming
unix utilities
unix advanced
$grep -wn unix sa
1:hello unix programming
2:unix utilities
6:unix advanced
(3 ) –v : reverse the filter
Ex: $ grep -v unix sa
hello world
programming
file handling
disc utilities
(4) –l:displays files from the given list, which contains given regular expression
Ex:
Grep –l unix f1 f2 f3 f4
(5) –i: ignores the case while comparing the pattern.

fgrep:
fixed global regular expression.
It is the fast version of grep.It searches only strings, won’t support regular
expressions.
Syntax:
fgrep option string filename
fgrep contains all options which are supported by grep(-n,-I,-l,-v,-w)and –x
-x :outputs only lines that are exactly equal to given string

egrep:
Extended Global Regular Expression.
Syntax:
egrep option regularexpression filename
Egrep supports all regular expressions supported by grep and it supports other
regular expressions like *,?,+,|
*:matches previous atom zero or more times.
+:matches previous atom one or more times.
?:matches previous atom zero or one time.
|:Alternation.

What is sed? Explain in detail about sed with examples?


sed is a “non-interactive” stream-oriented editor. Since its an “non-interactive”
it can be used to automate editing if desired.
The name sed is an abbreviation for stream editor, and the utility derives many of
its commands from the ed line-editor (ed was the first UNIX text editor).
This allows you to edit multiple files, or to perform common editing operations
without ever having to open vi or emacs. sed reads from a file or from its standard
input, and outputs to its standard output. sed has two buffers which are called
pattern buffer and hold buffer. Both are initially empty.
This is called as one execution cycle. Cycle continues till end of file/input is
reached.
Read a entire line from stdin/file.
Removes any trailing newline.
Places the line, in its pattern buffer.
Modify the pattern buffer according to the supplied commands.
Print the pattern buffer to stdout.
Sed command allows you to print only specific lines based on the line number or
pattern matches.
“p” is a command for printing the data from the pattern buffer.
To suppress automatic printing of pattern space use “-n” command with sed.
sed -n option will not print anything, unless an explicit request to print is
found.
Syntax: sed -n 'ADDRESS'p filename
sed -n '/PATTERN/p' filename
Let us first create the unix command file that will be used in all the examples
mentioned below::
Cat > unixcommand /*create a file */
Hello world
Unix programming
Implicit
Explicit
Operating system
Sed ADDRESS Format Examples ::
1.Sed Address Format 1: This will match only Nth line in the input.
Syntax:: sed -n ‘N’p filename
Ex: To print the third line from the input file unixcommand.
$sed -n '3'p unixcommand
Output::Implicit

Sed Address Format 2: M~N with “p” command prints every Nth line starting from line
M.
Syntax: sed -n ‘M~N’p filename
Ex: To prints every 2nd line starting from 3rd line .
$ sed -n '3~2‘p unixcommand
Output : Implicit
Operating system
3. Sed Address Format 3: M,N with “p” command prints Mth line to Nth line.
Syntax: sed -n ‘M,N’p filename
Ex :To prints from 2nd line to 4th line from input file
$sed -n ‘2,4'p unixcommand
Output: Unix programming
Implicit
Explicit

Explain the following awk concepts.


a. Operators
b. awk output statements
c. Control Structures

Like any other programming language Awk also has lot of operators for number and
string operations. In this article let us discuss about all the key awk operators.
There are two types of operators in Awk.
Unary Operator – Operator which accepts single operand is called unary operator.
Binary Operator – Operator which accepts more than one operand is called binary
operator.

let us review about awk loop statements – while, do while, for loops, break,
continue, and exit statements .
Awk looping statements are used for performing set of actions again and again in
succession.
It repeatedly executes a statement as long as condition is true.
Awk has number of looping statement as like ‘C’ programming language.
Awk While Loop
Syntax: while(condition)
actions
while is a keyword. condition is conditional expression actions are body of the
while loop which can have one or more statement. If actions has more than one
statement, it has to be enclosed with in the curly braces.
ex:-
BEGIN{}
{
i=1;
while (i <= 3)
{
print $i;
i++;
}
}
END{}

Awk Do-While Loop:


Awk Do while loop is called exit controlled loop, whereas awk while loop is called
as entry controlled loop.
Syntax:
do
action
while(condition)
ex:-
BEGIN{}
{
i=1
do
{
print $0
i++
} while (i <=3 )
}
END{}

Awk For Loop Statement


Awk for statement is same as awk while loop, but it is syntax is much easier to
use.
Syntax: for(initialization ; condition ; increment/decrement)
Actions
BEGIN{}
{
for (i=1;i <= NF;i++)
total=total+$i;
}
END{ print total}

Explain the following concepts about shell variables.


a. Accessing variables
b. Read only variables
c. Predefine variables
A variable is a character string to which we assign a value. The value assigned
could be a number, text, filename, device, or any other type of data. A variable is
nothing more than a pointer to the actual data. The shell enables you to create,
assign, and delete variables.

Variables can be accessed in several ways as explained below.


$name : replaced by the value of the variable name
${name} : same as above
${name-word} : replaced by the value of name if it was set, and word otherwise
${name+word} : replaced by word if name was set, and nothing otherwise
${name=word} : assigns word to name if it was not already set, and then is replaced
by the value of name
${name?word} : replaced by name if name was set, word is displayed at the standard
error channel. If word is omitted, then the standard error message is displayed
instead.
Examples:
1. $name :: ex: name= sai ram
echo $name
output :: sai ram
2. ${name} :: ex: name= ravi
echo ${ravi}
output ::ravi
3. ${name-word} :: ex: startDate=${startDate-`date`}
$startDate
output :: Sat Feb 2 10:07:06 IST 2013
4. ${name?word} :: ex: total=10
value=${total?'total is not set'}
echo $value
output :: 10

Read-only Variables:
The shell provides a way to mark variables as read-only by using the read only
command. After a variable is marked read-only, its value cannot be changed.
For example, following script would give error while trying to change the value of
NAME:
Example :
NAME="Zara Ali“
readonly NAME
NAME="Qadiri"
echo $NAME
This would produce following result:
/bin/sh: NAME: This variable is read only.

Bourne shell predefined environmental variables:


1.$IFS – Internal field separator refers to a variable which defines the character
or characters where to separate a pattern into tokens for some operations.IFS
typically includes the spaceIFS typically includes the space, tabIFS typically
includes the space, tab,and the newline.
2.$PS1- The value of this parameter is expanded (see PROMPTING below) and used as
the primary prompt string.
To display current prompt setting :$ echo $PS1
[\u@\h \W]\$
To change the current prompt : $ PS1="vijaya:" vijaya:

3.$PS2 - The "PS2" environment variable defines the secondary prompt, This is the
prompt you see when you execute a multi-line command, such as "for" or "if." You
also see it when you forget to terminate a quote. The default value is "> ."
To display prompt setting :: $ echo $PS2
>
4. $ SHENV :It the variable is not set ,the shell searches the user’s home
directory for “.profile “ startup when a new shell is created.
if the variable is set, then the shell searches the directory specified by
SHENV
To display environment :: $ set (OR) $ printenv (OR) $ env

Write shell scripts for the Menu driven program which has the following options.
i)display date
ii) list of users who have currently logged in.
iii) present working directory. iv) exit.
Program:
ch='y'
while [ $ch = 'y' ]
do echo "enter the choice"
echo "1 date"
echo "2 who"
echo "3 ls"
echo "4 pwd"
echo "5 exit"
read choice
case $choice in
1) date ;;
2) who ;;
3) ls ;;
4) pwd ;;
5) exit ;;
*) break
esac
echo "do u wish to continue (y/n)"
read choice
done

You might also like