unix in 15days

16
UNIX Introduction UNIX is a open system (source code) operating system. Operating System :: Collection of system programs. It acts as a interface between Hardware and the user. Let’s look at the below diagram for more information... UNIX is a Character User Interface (CUI) OS Different types of OS::  1 CUI(Character User Interface ): eg., MS-DOS,UNIX 2 GUI(Graphical User Interface ): eg., LINUX ,WINDOWS , MACINTOSH Advantages of UNIX 1. Multi User  2. Mul ti Tasking 3. Ope n Syst em 4. Security 5. Pr ogramming Faci li ty 6. Por tability 7. Communi cat ion Faci lit y 8. He lp Fa ci li ty Features of UNIX : 1. Multi User 2. Multi Tasking 3. Open System 

Upload: manne-vijji

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 1/16

UNIX Introduction

UNIX is a open system (source code) operating system.

Operating System :: Collection of system programs.

It acts as a interface between Hardware and the user.

Let’s look at the below diagram for more information...

UNIX is a Character User Interface (CUI) OS

Different types of OS:: 

1 CUI(Character User Interface): eg., MS-DOS,UNIX2 GUI(Graphical User Interface): eg., LINUX ,WINDOWS , MACINTOSH

Advantages of UNIX

1. Multi User 2. Multi Tasking3. Open System4. Security

5. Programming Facility6. Portability7. Communication Facility8. Help Facility

Features of UNIX :

1. Multi User 

2. Multi Tasking

3. Open System 

Page 2: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 2/16

4. Security

5. Programming Facility

6. Portability

7. Communication Facility

8. Help Facility

Architecture of UNIX :

Fig., 3.1 Architecture of UNIX

USER :: USER layer is the first layer in UNIX architecture.

Here the USERs sends the request to the next layer. USER never directly interactswith the KERNEL.

Request Eg:: Login and Password

SHELL:: It is a Command Line Interpreter (CLI) and it also act as scriptinglanguage.

Shell converts the high level language to machine level language.

SHELL receives request from USER and it checks command existence. If commandis existed then it converts into KERNEL understandable language (MachineLevel).And it passes the given request to KERNEL.SHELL acts as interface between USER and KERNEL.

KERNEL:: It is heart of the UNIX. It consists of 100s of system calls.System call means Low Level Program. All system calls written in 'C' Language.eg., open(),copy(),create(),kill(),proc(),fork()

Page 3: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 3/16

 UNIX/Hirerichal File System 

UNIX File System::

A file is the basic structure used to store information on the UNIX system.The UNIXfile system provides a logical method for organizing,storing,retreiving,manipulatingand information.

A file can store manuscripts and other word processing documents, instructions or  programs for the computer itself, an organized database of business information, a bitmap description of a screen image, or any other kind of information stored assequence of bytes on a computer.

With in UNIX, there are three different types of files ::

1. Regular Files2. Directory Files3. Special Files

Explanation ::

Regular Files

As a user, the information that you work with will be stored as an ordinary file.Ordinary files/ Regular file aggregates of characters that are treated as a unit by the

UNIX system.

A Regular file can contain normal ASCII characters such as text for manuscripts or  programs. Ordinary file can be created, changed, or deleted as you wish.

Directory Files

Directory is a file that holds other files and contains information about the locationsand attributes of these other files. For example a directory includes a list of all thefiles and sub directories that it contains, as well as their addresses, characteristics,files types and other attributes.

Special Files

Special files constitute an unusual feature of the UNIX file system. A special filerepresents a physical device. It may be a terminal, a communication device, or astorage unit such as disk drive.

There are two types of special files::

1. Block Special Files2. Character Special Files

Page 4: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 4/16

/ (root) ::

This is the root directory of the file system, the main directory of the entire filesystem, and the root directory for the super user.

/bin::

 bin stands for binary. This directory contains executable files for most of the UNIXcommands. UNIX can be either C program sw or shell programs. Shell programs arenothing but a collection of several UNIX commands.

/etc::

This contains system administration and configuration databases.

/dev::

This contains the special (device) files that include terminals, printers, and storagedevices. These files contain device numbers that identify devices to the operatingsystem, including:

/dev/console: the system console

/dev/dsk :

In UNIX, similar devices are located in sub directories of /dev. For example, all disk devices are in the subdirectory /dev/dsk .

/lib::

This directory contains all the library functions provided by UNIX for programmers.The programs written under UNIX make use of these library functions in the libdirectory.

Page 5: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 5/16

/sbin::

This contains programs used in booting the system and in system recovery.

/opt::

This is the root for the subtree containing the add-on application packages.

/home::

This contains the home directories and files of all users.If your logname is dwhlabs,your default home directory is home/dwhlabs

/tmp::

This contains all temporary files used by the UNIX system.

/mnt::

Contains entries for removable(mountable) media such as CD-ROMs and DLT tapes.

/unix::

This directory contains UNIX kernel programming.

/usr ::

This contains other accessible directories such as /usr/lib and usr/bin.

/usr/binContains many executable programs and UNIX systemutilities.

/usr/games Contains binaries for game programs and data for games

/usr/libContains libraries for programs and programminglanguages

/usr/sbin Contains executable programs for system administration

/usr/share/man Contains the online manual pages

/var ::

This contains the directories of all files that vary among systems.These include filesthat log system activity, accounting files, mail files, application packages, backupfiles for editors, and many other types of files that vary from system.Files in thisdirectory include:

/var/adm Contains system logging and accounting files./var/mail Contains user mail files./var/news Contains message of common interest.

/var/opt is the root of a subtree containing add-on application packages./var/tmp is a directory for temporary files.

Page 6: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 6/16

/var/uucp Contains log and status files for the UUCP system.

UNIX Commands:

1. Basic Command

2. Administrator Commands3. Create, Remove File4. Create, Remove Directory5. Copy, Rename file6.  ls command options

Basic Commands:

# system adimistrator prompt$ user working prompt

$pwd :: It displays present working directory. (If you login with dwhlabs)

/home/dwhlabs

$logname :: It displays the current username.

Dwhlabs

$clear :: It clears the screen.

$date :: It displays the current system date and time.

Day Month date hour: min: sec standard name year FRI MAY 23 10:50:30 IST 2008

$banner :: It prints a message in large letters.

$banner "tecno"

$exit :: To logout from the current user $finger :: It displays complete information about all the users who are logged in

$who :: To display information about all users who have logged into the systemcurrently (login name, terminal number, date and time) . $who am i :: It displays username, terminal number, date and time at which youlogged into the system.

$cal:: It displays previous , present and next month calendar 

$cal year $cal month year 

$ls :: This command is used to find list of files except hidden.

Page 7: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 7/16

Administrator Commands;

# system adimistrator prompt$ user working prompt

#useradd :: To add the user terminals.

#useradd user1

#password :: To give the password to a particular user terminal.

#password user1Enter password ::Retype password ::

System Run Levels

#init :: To change the system run levels.

#init 0 : To shut down the system#init 1or s: To bring the system to single user mode.#init 2 : To bring the system multi user mode with no resource shared.#init 3 : To bring the system multi user mode with resource shared.#init 6 : Halt and reboot the system to the default run level.

ls command options:

1. $ls :: This command is used to display list of files and directories.2. $ls -x :: It displays width wise3. $ls | pg :: It displays list of files and directories page wise & width wise.4. $ls -a :: It displays files and directories including . and .. hidden files5. $ls -f :: It displays files, directories, executable files, symbolic files

a1a2sample\letter*

notes@\ directory* executable files- symbolic link file

6. $ls -r ::It displays files and directories in reverse order.(descending order)7. $ls -R :: It displays files and directories recursively.8. $ls -t :: It displays files and directories based on date and time of creation.9. $ls -i :: It displays files and directories along with inode number.10. $ls -l :: It displays files and directories in long list format.

File type permission link uname Group sizeinbytes date filename

- rw-r 1 tec Group 560 nov 3 1:30 Sample

Page 8: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 8/16

D rwxr-wr 2 tec Group 430 mar 7 7.30 Student

Wild card characters: * , ? , [] ,-, . ,^,$

wild card character Description

 * It matches one or more characters

? It matches any single character 

[] It matches any single character in the given list

- It matches any character in that range

. It matches any single character except enter key character.

^ It displays line which starts with that character 

$ It displays line which ends with that character.

Examples :: * wild card $ ls t* It displays all files starting with 't' character. $ ls*s It displays all files ending with 's' $ ls b*k It displays all files starting with b andending with k ? wild card $ ls t?? It displays all files starting with 't' character and alsofile length must be 3 characters.. $ ls ?ram It displays all files starting with anycharacter and also the length of the file must be 4 characters. - wild card $ ls [a-z]ra Itdisplays all files starting with any character between a to z and ends with 'ra'.Thelength of the file must be 3 characters.

Examples :: [ ] wild card $ ls [aeiou]ra It displays all files starting with 'a' or 'e' or 'i' or 'o' or 'u' character and ending with 'ra'. $ ls [a-b]* It displays all files starting withany character from a to b. . wild card $ ls t..u It displays all files starting with 't'

character and ending with 'u'. The length of the file must be4 characters. It never includes enter key character. ^ wild card $ ls ^sta It displays all files which starts with'sta' and the length of the file must be 3 characters.. $ wild card $ ls *sta$

It displays all files which ends with 'sta'.The length of the word can be any number of characters.

Filter Commands:

$ grep ::Globally Search a Regular expression and print it.

This command is used to search a particular pattern(single string ) in a file or directory and regular expression(pattern which uses wild card characters).

syntax - $grep pattern filenameeg. $grep dwhlabs dwh (It displays all the lines in a file which as dwhlabs

 pattern/string) - Character Pattern

eg. $grep "dwhlabs website" dwh - character pattern

eg. $grep \< dwhlabs\> dwh (It displays all the line in a file which as a exact word'dwhlabs') - Word Pattern

Page 9: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 9/16

eg. $grep ^UNIX$ language (It displays all the lines in the file which has a singleword ‘UNIX’) - Line Pattern

$grep options

$grep -i pattern filename (ignore case sensitive)

$grep -c pattern filename (It displays total count of lines)

$grep -n pattern filename (It displays along with the line number)

$grep -l pattern filename (It displays filename in the current directory)

$grep -v pattern filename (It displays unmatched pattern lines)

Question ?

Q)How to remove blank lines in a file?A)$grep -v "^$" file1>tmp | mv tmp>file1

Explanation :: Piping concept is used to combine more than one command. Here wefirst identifing the non blank files in file1 and redirecting the result to tmp (temporaryfile).This temporary file is input to the next command. Now we are renaming the tmpinto file1.Now check the file1 result <You will not find any blank lines>

fgrep ::

This command is used to search multiple strings or one string but not regular expression.

syntax - $fgrep "pattern1>pattern2>....>patternn " filename

eg.$fgrep "unix>c++

>Data Warehouse" stud

egrep :: Extended grep

This command is used to search for single or multiple patterns and also regular expressions.

Filter Commands

$ cut :: This command is used to retreive the required fields or characters from a file.

100 Rakesh UNIX HYD

Page 10: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 10/16

101 Ramani C++ CHE

102 Prabhu C BAN

103 Jyosh DWH CHE

syntax - $cut -f 1-3 filenameeg. $cut -f 0-2 employee (all the fields in the above file)

100 Rakesh UNIX

101 Ramani C++

102 Prabhu C

103 Jyosh DWH

eg. $cut -c 1-8 employee (first 8 characters)

100 Rake101 Rama

102 Prab

103 Jyos

Delimiters

Default ( Tab )

: , ; * _  

$cut options

$cut -f 0-2 employee$cut -c 1-8 employee$cut -d ',' 1-8 employee

$ sort :: This command is used to sort the file records in ascending or descending

$sort Options

$sort -r filename (reverse)$sort -u filename (unique records)$sort -n filename(show the number along sorting)

How to sort data in field wise ?

syntax- $sort -f +pos1 -pos2 filenameeg. $sort -f +1 -2 employee

$ uniq :: This command is used to filter the duplicate lines from a file Note :: Always the file must be in sorting order.

syntax - $uniq filename

Page 11: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 11/16

eg. $uniq employee

$uniq Options

eg. $uniq -u employee (displays non duplicated lines)

eg. $uniq -d employee (displays duplicated lines)

Question ::

Q) How to remove duplicate lines in a file?A) $uniq employee>temp$mv temp employee

$ tr :: Translate the character in string1 from stdin into those in string2 in stdout.

$ sed :: This command is used for editing files from a script or from the command

line.

$ head :: Display first 'N' number of files.

$ tail :: Display last 'N' number of files.

$ cmp :: Compare two files andd list where differences occur.

$ diff :: Compare the two files and shows the difference.

$ wc :: Display word (or character or line ) count for file.

Shell Scripting Exercise

1. Write a shell script to open a given file? $vi open.sh

echo "enter a filename to open "read fnamecat $fname:wq

** How to execute shell script?$./open.sh or $sh open.sh

2. Write a script to delete the blank lines? $vi blank_lines.sh

echo "enter a filename to delete”read fnamegrep -v "^$" $fname >tempmv temp $fnameecho "after deleting the lines"cat $fname

:wq

Page 12: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 12/16

** How to execute shell script?$./open.sh or $sh open.sh

3. Write a script to delete duplicate lines from a file? $vi duplicate.sh

echo "enter a filename to delete "read fnamesort $fname | unique -u >tempmv temp $fnameecho "file with unique lines"cat $fname:wq

** How to execute shell script ?$./open.sh or $sh open.sh

4. Write a script to check the given number is positive or negative? $vi pos_neg.sh

echo "enter a number: "read nif [ $n gt 0 ]then

echo "entered number : positive number"elseecho "entered number : negative number"fi:wq

** How to execute shell script ?$./open.sh or $sh open.sh

5. Write a script to check the given number is even or odd number? $vi even_odd.sh

echo "enter a number: "read nif [ `expr $n % 2` = 0 ]thenecho "entered number : even number"elseecho "entered number : odd number "fi:wq

** How to execute shell script ?

$./open.sh or $sh open.sh

Page 13: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 13/16

6. Write a script to accept two strings and check the given strings are equal or not? $vitwo_strings.sh

echo "enter first string:"read s1

echo "enter second string:"read s2if ["$s1" = "$s2" ]thenecho "equal"elseecho "not equal"fi:wq

** How to execute shell script ?

$./open.sh or $sh open.sh

7. Write a script to delete the given file? $vi delete.sh

echo "enter filename:"read fnameif rm $fnamethenecho "deleted the file"elseecho "file is not existed”fi:wq

** How to execute shell script ?$./open.sh or $sh open.sh

8. Write a script to check the given user is logged in or not? $vi delete.sh

echo "enter login name:"read lname

if who | grep $lname > /dev/nullthenecho "user is online"elseecho "user is offline”fi:wq

** How to execute shell script ?$./open.sh or $sh open.sh

9. Write a script to check the given type of file? $vi type_file.sh

Page 14: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 14/16

echo "enter file name:"read fnameif [ -e $fname ]thenif [ -f $fname ]

thenecho "regular file "elseecho "directory file "elif [ -l $fname ]thenecho " link file"elif [ -b $fname -o -c $fname ]thenecho "Device file"fi

:wq

** How to execute shell script ?$./open.sh or $sh open.sh

10. Write a script open the given file? $vi open_file.sh

echo "enter file name:"read fnameif [ -e $fname ]thenif [ -f $fname ]thenif [ -r $fname ]thencat $fnameelsechmod u+r $fnamecat $fnameelse "it is not a regular file"else

echo "It is not a regualr file"elseecho "file doesnt exist"fi:wq

** How to execute shell script ?$./open.sh or $sh open.sh

11. Write a script to check the given two file are equal or not? $vi twofiles_same.sh

echo "enter first file "read f1

Page 15: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 15/16

echo "enter second file"read f2x =`cmp $f1 $f2`if [-z $x]then

echo "both are same"elseecho "both are not same"fi:wq

** How to execute shell script ?$./open.sh or $sh open.sh

12. Write a script to check the given type of character? $vi char_type.sh

echo "enter a character"read char case $char in[a-z])echo "lower case";;[A-Z])echo "upper case";;[0-9])echo "Digit";;?)echo "special character";;*)echo "Entered more than one character";;esac:wq

** How to execute shell script ?$./open.sh or $sh open.sh

13. Write a script to creat a menu using case? $vi menu_case.sh

clear tput cut 10 25echo "main menu"tput cut 11 25echo "------------"tput cut 12 25echo "1.Date "tput cut 13 25echo "2.All files"tput cut 13 25

echo "3.All Users"tput cut 14 25

Page 16: Unix in 15days

8/9/2019 Unix in 15days

http://slidepdf.com/reader/full/unix-in-15days 16/16

echo "4.No items found"read choicecase $choice in1)echo "Today Date ::" `date`;;

2)ls -f ;;3)finger ;;*)exit;;esac:wq

** How to execute shell script ?$./open.sh or $sh open.sh