LINUX JOURNAL

May 31, 2016 | Author: Haseeb Shahid | Category: Book Excerpts
Share Embed Donate


Short Description

LINUX...

Description

Faculty of Computer Science and Engineering

CS311L Operating Systems Lab Manual

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi. 1

Table of Contents

Lab Number

Page

Lab-1 …………………………………………………………………………………………………………………………………. 3 Lab-2 …………………………………………………………………………………………………………………………………. 9 Lab-3 …………………………………………………………………………………………………………………………………. 14 Lab-4 …………………………………………………………………………………………………………………………………. 21 Lab-5 …………………………………………………………………………………………………………………………………. 28 Lab-6 …………………………………………………………………………………………………………………………………. 31 Lab-7 …………………………………………………………………………………………………………………………………. 34 Lab-8 …………………………………………………………………………………………………………………………………. 38 Lab-9 …………………………………………………………………………………………………………………………………. 41 Lab-10 …………………………………………………………………………………………………………………………………. 45 Lab-11 …………………………………………………………………………………………………………………………………. 49 Lab-12………………………………………………………………………………………………………………………………….. 53

2

Operating System CS211L

Lab – 01 Basic Terminology, Introductory Commands

Operating System Operating System is a type of System Software and is a collection (set) of programs, which performs two specific functions. First, it provides a user interface so that human user can interact with the machine. Second, the operating system manages computer resources. It accepts user commands, and then it detrains, allowing the programs to run where they are loaded into memory first and communication between various hardware devices is coordinated. Windows, UNIX, Linux, DOS are popular operating systems.

Linux Linux is an operating system, which is a flavor of UNIX. Linux is a multi-user and multi-tasking operating system. Because of the likeness with UNIX, all the programs written for UNIX can be compiled and run under Linux. Linux operating system runes on variety of machines like 486/Pentium, Sun SPARC, PowerPC, etc. One of the important features of Linux is communication through TCP/IP protocols. Linus Torvalds at the University of Helsinki, Finland, developed Linux. UNIX programmers around the world assisted him in the development of Linux.

Linux System Linux System can be split into two parts. i) Shell ii) Kernel Formally, a Shell is interface between a user and a Linux operating system, i.e. user interacts with the Linux operating system through the shell. The shell performs two tasks; it accepts commands from a user and then interprets those commands. Two shells, which are commonly used, are Bourne shell and C shell. One other shell, which is rather complex, is Korn shell. Kernel is the core of Linux Operating System, which is operational as long as the computer system is running. The kernel is part of the Linux Operating system which consists of routines which interact with underlying hardware, and routines which include system calls handling, process management, scheduling, signals, paging, swapping, the file system, and I/O to storage devices. So, shell accepts commands from the user, interprets them and delivers these interpreted commands to the kernel for execution. After execution, the shell displays results of the executed commands.

Files File is a mechanism through which we store information. Normally, there are two modes of storing information. i) File ii) Directories

i) File A simple file stores some type of information. The information it has may be in text format, or in binary format. 3

ii) Directories Directories are special types of files which contain information about the files stored inside that directory, and may contain other directories (called Subdirectories). So directories are also files, which contain some vital information about the files, and other directories. There’s a file (management) system in an operating system, which manipulates file and directories. The major operations which can be performed on files and directories are given below: · Create · Delete · Open · Close · Read · Write · Append · Seek · Rename · Get Attributes · Set Attributes

File Attributes Since file contains some information about something, information about the file itself is also needed. This information is called file attributes. All operating systems associate some extra information with each file, for example date and time of when the file was created or modified, file size etc. These items are called file attributes. Some of the usual file attributes are given below: · File Name · Creator · Date created · Date of last read access · Date of last modification · Current size of the file in bytes

Linux File System Hierarchy Unlike DOS, which permits you to organize your folders (directories) and files anyway you please, the Linux file system is organized into a standard directory structure. A portion of the Linux directory structure is pictured below: /(Root Directory)

etc/ sbin/

bin/

usr/

home/ div/

lib/

info/

tmp

----------bin/

lib/

info/

sbin/

Root Directory The top most directory is called root directory. The Linux File system hierarchical structure begins with a root directory. The name of root directory is /. 4

Home Directory The directory selected by Linux as the working directory when a user logs on. When a user logs on, Linux selects home directory (its name usually matches your login name) as his/her working directory. This is where a user normally performs his routine tasks, stores files and data. This is where various configuration settings about the user are stored.

Current Working Directory The directory which the user is currently working in.

Pathname Pathname is a reference to identify a file within the directory structure. For example the following file name indicates the file in the hierarchy of directories: /usr/users/bill/letters/pay The first slash (/) indicates the root directory, moves down to usr, then users, then bill, then letters and finally to the file pay. So this pathname is a reference to the file pay with respect to the root directory (/). A path may be of two types.

Absolute Pathname Absolute pathname identifies a file or a directory irrespective of the current state of the user. The user's "current directory" is part of the user's state. The absolute pathname always starts from the root directory. For example, to locate filename in lib directory, the absolute path of the file is: /usr/local/lib/filename

Relative pathname The pathname which identifies a file or a directory in a way that depends on the state of the user i.e. users current directory. Relative pathname identifies files with respect to user's current directory.

Consider the following diagram. Current Directory (Working Directory) thesis chapter4.tex

To refer to a file chapter4.tex in the subdirectory thesis with respect to the current directory, we use pathname. thesis/chepter4.tex Some standard Linux directories are given below: /home

Users’ home directory

/etc

All system administrator commands, configuration files, and installation control files. 5

/bin

The core set of system commands and programs. Most systems cannot boot (initially start) without executing some of the commands in this directory.

/dev

The device files used to access system peripherals (for example, your terminal can be accessed from /dev/tty). The standard set of programming libraries required by Linux programs. Temporary files created and used by many Linux programs. Log files, spool files etc. The root user’s home directory. Common commands and programs. Documentation Games Header files Online documentation Manual pages (help) Shared information

/lib /tmp /var /root /usr/bin /usr/doc /usr/games /usr/include /usr/info /usr/man /usr/share

Logging In As we know that shell is an interface between a user and Linux kernel. The first step that you must accomplish before you can use the shell is to log in to your machine. This is usually a very straightforward process provided that you have a login ID and a password. When you login successfully, for an ordinary user account, the system will execute a program called the shell. And it is your shell process that is responsible for giving you a command prompt. By default, the prompt for a non-privileged user is a dollar ($) symbol, and for a privileged (root) user is a hash (#) symbol.

Logging out Logging out of Linux system is done by typing control-d (^d).

Command A request from a user to the Linux operating system asking that a specific function be performed; for example, a request to list all files in your current directory. Shell commands operate on files, directories, and various devices – disks, printers, etc.

Common File and Directory Commands Commonly used directory commands are ls, cd, mkdir, pwd, rmdir, etc and common file commands include cat, cp, mv, rm, etc.

Exploring the File Hierarchy When you login, there is a special directory associated with your login name called your home directory. Your home directory is actually your initial current working directory. The simplest way to find out where in the directory hierarchy your home directory is located is just to use the command pwd straight after you login. The pwd command tells you your present working directory.

6

$ pwd /home/your_account_name Note:- Linux commands are case-sensitive. All standard Linux commands use lower case letters only. In order to move away from your home directory to somewhere else in the directory hierarchy, you use the cd (change directory) command. So, to change to the root directory you would use the command. $ cd /

Directory Commands Command ls

Description List the file in the directory, just like dir command in DOS. Options -a Display all the files, and subdirectories, including hidden files. -l Display detailed information about each file, and directory. -r Display files in the reverse order.

Example When you enter ls on the command prompt, just after the command cd / .

[email protected]:/$ ls bin dev home lib mail mnt proc sbin usr boot etc initrd lost+found misc opt root tmp var And when you enter ls -r on the command prompt.

[email protected]: /$ ls -r var tmp root opt misc lost+found initrd etc boot usr sbin proc mnt mail lib home dev bin

Command mkdir directory-name

Description Creates a new directory. Directory-name specifies the name of the new directory. If the name doesn’t begin with a slash, the new directory is created as a subdirectory of the current working directory. If the name begins with a slash, the name defines the path from the root directory to the new directory.

Try to use the following command first because this will bring you back to your home directory. $ cd Now try mkdir command [email protected]:~$ mkdir books This command will create a new directory under the home directory. [email protected] 7

| books Though you have created a sub-directory books, but you are still in the home (parent directory of books) directory i.e. [email protected]:~$

How would you go to the directory books? Command

Description

cd For Example:

Change to another directory.

$cd dir-name To change to any sub-directory under the current directory.

[email protected]:~$cd books (when Enter is pressed, the prompt becomes)

[email protected]:~/books$ Do you see any difference between the two prompts? Now you are in books directory, a step down to home. How could you go up?

[email protected]:~/books$ cd .. (there’s space between cd and ..) Now you will again be in your parent directory. And the prompt becomes:

[email protected]:~$

8

Operating System CS211L

Lab – 02

Basic Terminology, Introductory Commands What is Linux Kernel The kernel is the heart of the Linux operating system. It manages the resources of Linux such as: • • • • • • • •

File management Multitasking Memory management I/O management Process management Device management Networking support including IPv4 and IPv6 Many other advanced features...

The kernel decides who will use these resources and for how long and when. The kernel acts as an intermediary between the computer hardware and various applications.

Unix philosophy The Unix philosophy is philosophical approaches to developing software based on the experience of leading developers of the Unix operating system. The following philosophical approaches also apply to Linux operating systems.

• • •

Do one thing and do it well - Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface. Everything is file - Ease of use and security is offered by treating hardware as a file. Small is beautiful. 9

• • •

• •

Store data and configuration in flat text files - Text file is a universal interface. Easy to create, backup and move to another system. Use shell scripts to increase leverage and portability - Use shell scripts to automate common tasks across various UNIX / Linux installations. Chain programs together to complete complex task - Use shell pipes and filters to chain small utilities that perform one task at time. Choose portability over efficiency. Keep it Simple, Stupid (KISS).

Unix shell A Linux shell is a command-line interpreter or shell that provides a traditional user interface for the Linux operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands. The most influential Unix shells have been the Bourne shell and the C shell. The Bourne shell, sh, was written by Stephen Bourne at AT&T as the original Unix command line interpreter; it introduced the basic features common to all the Unix shells. The C shell, csh, was written by Bill Joy while a graduate student at University of California, Berkeley. The language of this shell was modeled on C. The C shell introduced a large number of features for interactive work. The Concept of a shell... The most generic sense of the term shell means any program that users employ to type commands. A shell hides the details of the underlying operating system with the shell interface and manages the technical details of the operating system kernel interface, which is the lowest-level, or 'inner-most' component of most operating systems. In Unix-like operating systems users typically have many choices of command-line interpreters for interactive sessions. When a user logs in to the system, a shell program is automatically executed, which is called the login shell. In addition, a user is typically allowed to execute another shell program interactively. The Unix shell was unusual when it was introduced. It is both an interactive command language as well as a scripting programming language, and is used by the operating system as the facility to control (through shell scripts) the execution of the system. Shells created for other operating systems than Unix, often provide similar functionality.

On systems with a windowing system (Graphical User Interface), some users may never use the shell directly. On Unix systems, the shell is still the implementation language of system startup scripts, 10

including the program that starts the windowing system, the programs that facilitate access to the Internet, and many other essential functions. Graphical user interfaces for Unix, such as GNOME, KDE, and Xfce are often called visual or graphical shells. Command line overview The default command line (shell) on most Linux systems is /bin/bash – the Bourne Again Shell (an enhanced version of the Bourne Shell /bin/sh). Bash executes programs on your system for each command that you enter on the command line. Here are some rules to live by when typing on the command line: •

Press Enter a lot – If you type something, the computer will only act on it if you press Enter (the return key). Until you press Enter nothing happens.



Press Ctrl+C – If you have a bad feeling about what the computer is doing in response to your last command, press Ctrl+C. This terminates the program that is currently running.



Press Tab a lot – The bash shell used by most Linux distributions has a handy feature that will type the rest of commands and file names for you.

Linux Filesystem Hierarchy When migrating from another operating system such as Microsoft Windows to another; one thing that will profoundly affect the end user greatly will be the differences between the filesystems. Like UNIX, Linux chooses to have a single hierarchical directory structure. Everything starts from the root directory, represented by /, and then expands into sub-directories instead of having so-called 'drives'. In the Windows environment, one may put one's files almost anywhere: on C drive, D drive, E drive etc. Such a file system is called a hierarchical structure and is managed by the programs themselves (program directories), not by the operating system. On the other hand, Linux sorts directories descending from the root directory / according to their importance to the boot process. If you're wondering why Linux uses the frontslash / instead of the backslash \ as in Windows, it's because it's simply following the UNIX tradition. Linux, like Unix also chooses to be case sensitive. What this means is that the case, whether in capitals or not, of the characters becomes very important. So this is not the same as THIS. This feature accounts for a fairly large proportion of problems for new users especially during file transfer operations whether it may be local or remote. If you install a program in Windows, it usually stores most of its files in its own directory structure. A help file for instance may be in C:\Program Files\[program name]\ or in C:\Program Files\[programname]\help or in C:\Program Files\[program -name]\humpty\dumpty\doo. In Linux, programs put their documentation into /usr/share/doc/[program-name], man(ual) pages into /usr/share/man/man[1-9] and info pages into /usr/share/info. They are merged into and with the system hierarchy.

11

As all Linux users know, unless you mount a partition or a device, the system does not know of the existence of that partition or device. This might not appear to be the easiest way to provide access to your partitions or devices, however it offers the advantage of far greater flexibility when compared to other operating systems. This kind of layout, known as the unified filesystem, does offer several advantages over the approach that Windows uses. Let's take the example of the /usr directory. This subdirectory of the root directory contains most of the system executables. With the Linux filesystem, you can choose to mount it off another partition or even off another machine over the network using an innumerable set of protocols such as NFS (Sun), Coda (CMU) or AFS (IBM). The underlying system will not and need not know the difference. The presence of the /usr directory is completely transparent. It appears to be a local directory that is part of the local directory structure. As we all know Linux file system starts with /, the root directory. All other directories are 'children' of this directory. The partition which the root file system resides on is mounted first during boot and the system will not boot if it doesn't find it. The following figure shows the Linux filesystem hierarchy and purpose of some of the directories.

12

13

Operating System CS211L

Lab – 03 Linux Commands - 1

What Next? In the previous Lab there was a brief introduction about Operating Systems and Linux. Now we are going to start doing Linux commands. You must have used DOS commands copy, format, dir etc. Similarly Linux has such commands but with different syntax or format. You should get used to these commands because you will be using them frequently. There is syntax for each command, using wrong syntax the command will not execute. As a first step we look at how a user logs in to Linux. Then we will see the general syntax for all Linux commands.

Logging In As we know that shell is an interface between a user and Linux kernel. The first step that you must accomplish before you can use the shell is to log in to your machine. This is usually a very straightforward process provided that you have a login ID and a password. When you login successfully, for an ordinary user account, the system will execute a program called the shell. And it is your shell process that is responsible for giving you a command prompt. By default, the prompt for a non-privileged user is a dollar ($) symbol, and for a privileged (root) user is a hash (#) symbol.

Logging out Logging out of Linux system is done by typing control-d (^d).

Command A request from a user to the Linux operating system asking that a specific function be performed; for example, a request to list all files in your current directory. Shell commands operate on files, directories, and various devices – disks, printers, etc. Exploring the File Hierarchy When you login, there is a special directory associated with your login name called your home directory. Your home directory is actually your initial current working directory. The simplest way to find out where in the directory hierarchy your home directory is located is just to use the command pwd straight after you login. The pwd command tells you your present working directory. $ pwd /home/your_account_name Note: - Linux commands are case-sensitive. All standard Linux commands use lower case letters only.

In order to move away from your home directory to somewhere else in the directory hierarchy, you use the cd (change directory) command. So, to change to the root directory you would use the command. $ cd / 14

Directory Commands Command ls

Description List the file in the directory, just like dir command in DOS. Options -a Display all the files, and subdirectories, including hidden files. -l Display detailed information about each file, and directory. -r Display files in the reverse order.

Example When you enter ls on the command prompt, just after the command cd / .

[email protected]:/$ ls bin dev home lib mail mnt proc sbin usr boot etc initrd lost+found misc opt root tmp var And when you enter ls -r on the command prompt.

[email protected]: /$ ls -r var tmp root opt misc lost+found initrd etc boot usr sbin proc mnt mail lib home dev bin

Command mkdir directory-name

Description Creates a new directory. Directory-name specifies the name of the new directory. If the name doesn’t begin with a slash, the new directory is created as a subdirectory of the current working directory. If the name begins with a slash, the name defines the path from the root directory to the new directory.

Try to use the following command first because this will bring you back to your home directory. $ cd Now try mkdir command [email protected]:~$ mkdir books This command will create a new directory under the home directory. [email protected] | books Though you have created a sub-directory books, but you are still in the home (parent directory of books) directory i.e. [email protected]:~$

How would you go to the directory books? Command

Description

cd For Example:

Change to another directory. 15

$cd dir-name To change to any sub-directory under the current directory.

[email protected]:~$cd books (when Enter is pressed, the prompt becomes)

[email protected]:~/books$ Do you see any difference between the two prompts? Now you are in books directory, a step down to home. How could you go up?

[email protected]:~/books$ cd .. (there’s space between cd and ..) Now you will again be in your parent directory. And the prompt becomes:

[email protected]:~$ Example Create other directory chemistry under books, and move to chemistry directory. [email protected]:~$ cd books [email protected]:~/books$ mkdir chemistry [email protected]:~/books$ cd chemistry [email protected]:~/books/chemistry$ Now you are quite away from your home directory. How would you go to your home directory? Your current location is [email protected]:~$/books / chemistry To go to your home directory: [email protected]:~/books/chemistry$ cd (enter) The prompt will become: [email protected]:~$ Type pwd at prompt see where are you. Example Now you are in your home directory. How will you go directly to chemistry directory? [email protected]:~$ cd books/chemistry (enter) What do you think books/chemistry is relative or absolute path? Make the following directory hierarchy. Books

math trigonometry

physics calculus quantum

computer classical

nuclear

Example 16

If I’m in directory classical, how would find I where I am? The command used for that is pwd. When I entered this command when I was in directory classical the following information was printed on my screen. The path printed was absolute path. How will you add a directory graphics under the directory computer while you are in physics’ subdirectory classical? Now you can create directories. How do we remove them?

Command

Description

rmdir

(‘remove directory”) Deletes a directory.

For example $rmdir dirname Note:- rmdir will only work if the directory you are trying to remove does not contain any file. So first remove all files from the directory. a) Your are in books directory, from here try to remove sub-directory quantum under the directory physics. b) Now move to directory computer, from here remove sub-directory calculus under the directory math. c) Now recreate both of the removed directories.

Creating a text file through 'nano' editor There are a number of editors through which files can be created, but the easiest way to use is through the nano editor. It is a text-based editor (works in text based environment).

Example $ nano notes With this command an editor will be opened. Enter text in the file. Press Ctrl-O to save the file, and CtrlX to exit from the editor. In order to view existing files use cat command.

$ cat notes The cat command will just print the file-contents; in order to make changes in the file the text editor must be used.

Removing/Deleting files Command rm

Description (‘remove’) Removes a file permanently. For example, $rm filename Options -r Deletes an entire directory, and all the files it contains. -i This option puts the rm command into interactive mode and prompts you before it removes it. 17

Try to remove a directory having some files in it, and observe the response of system.

$rm directoryname Now use the above command with the –i, see what happens. Note:- To remove all files from a directory use $ rm dirname/* Inode Numbers and Filenames Each Linux file or directory, when created, the system allots it an inode number.

How can we access that inode number? For example, to display inode number of a file notes in the subdirectory graphics under the directory computer; on my computer I entered the command: [email protected]:~$ ls -i computer/graphics/notes The following information is displayed: 241467 computer/graphics/notes The left number indicates the inode number of the file notes.

Complete Information about a File/Directory In the previous example we just get the information about the inode number of the file, but there are other things other than inode about the file. When I entered following command on my command prompt I got following results:

[email protected]:~$ ls -l notes -rw-r--r-1 mumtaz mumtaz 1 2 3 4 The following information is printed in the given order: 1: file permissions (coming lab) 2: file links (coming next) 3: Owner of file 4: group (coming lab) 5: size of file 6: last change 7: Time of change 8: File/directory name

23 5

Jan 26 15:39 notes 6 7 8

You must remember the sequence in which the above information displayed. You can create file now, how will you copy it?

Copying file Command cp

Description (‘copy’) Creates a new copy of the existing file.

Example $cp oldfile newfile 18

To copy file from the current directory to another directory the general format is cp oldfile directory[/newfile] Here item in square brackets [] is optional i.e. if you want to copy the desired file to new location with new name then you can use this option, otherwise the file will be copied with the original name to the new location.

Example There’s a file atomic-notes in the directory books with the new name at-notes. Copy this file to sub-directory nuclear under the directory physics. [email protected]:~/books$ cp atomic-notes physics/nuclear/at-notes You know now copying files, now it’s we look how files are moved to new locations.

Renaming files Command mv

Description (‘moves’) Renames a file or moves it from one directory to another.

For example, $mv oldfile newfile The oldfile specifies the existing file you want to rename. newfile specifies the new name to use for the file. $mv filename directory[/newname] The filename specifies the file you want to move, directory specifies the directory into which you want to move the file. newname specifies the new name to use for the file.

Example Rename file atomic-notes with at-notes in the books directory [email protected]:~ books$ mv atomic-notes at-notes

File Links Command ln

Description Create a new link to a file.

For example $ ln existingfile newname The existingfile specifies the file to which you want to create a new link; newname specifies the name given to new link. $ln existingfiles directory The existingfiles specifies the file (or files) to which you want to create a new link (or links). The directory specifies the directory in which you want the new link. 19

Example Create a file book-list in books directory. Then a link of this file is created with name list in the same directory. The following command will do the work. [email protected]:~/books$ ln book-list list Now see listing of both files. [email protected]:~/books ls -li book-list 243022 -rw-r--r-- 2 uzair uzair 15 Jan 27 10:51 book-list [email protected]:~/books$ ls -li list 243022 -rw-r--r-- 2 uzair uzair 15 Jan 27 10:51 list You see the inode of both files are same. So when a link to a file is created, no new file is created, only a new name is created (both file-names point to the same file).

Operating System CS211L

Lab – 04 20

Linux Commands – 2 and Redirection, Pipes, File Security Command

Description

date

The date command displays the current date and time on the screen. The system administrator sets the date users cannot change them.

Example $date Wed Feb 7 10:35:41 PKT 2007 There are number of options in which date can be displayed. If you want to see only date, you can do this like this: $ date +”%d” 07 for time only $date +”%r” 10:38:11 AM %Y = Year. %H = Hour(00..23), %I = Hour (01..12), %m = Month

Command

Description

clear

Clears the screen.

Command

Description

echo

Echoes back whatever you type on the command line after echo. Options -n doesn’t begin a new line after echoing the information.

Example echo Hello there Example echo -n Hello

Command

Description

sort

Sorts a column in a file in alphabetical order. By default the the output is displayed on your terminal, but you can specify a filename as the argument or redirect the out put to a file. Option -r Sorts in reverse order -b Ignores leading blanks -f Ignores the distinction between lowercase and upper case letters -o Sorts the output in the specified file -n Numbers are sorted by their arithmetic values

More on Sorting: The sort command sorts the specified file on a line by line basis. If the first characters on two lines are the same, it compares the second characters to determine the order of the sort. If the second characters are same, it compares the third characters, and this process goes on until two characters differ or the line ends. If two lines are identical, then it does not matter which one is placed first.

Example 21

Following is a file whose name is sortfile. Observe following sort operation on it: (Note: Create the file sortfile using nano and put the following content in that file.) file to be sorted File To be Sorted 25 years old End of file apple on the table 6 apples $ sort –f sortfile 25 years old 6 apples apple on the table End of file file to be sorted File To be Sorted $ sort -fn sortfile apple on the table End of file file to be sorted File To be Sorted 6 apples 25 years old $ sort -f -r sortfile File To be Sorted file to be sorted End of file apple on the table 6 apples 25 years old

Command

Description

wc

(‘word count’) Counts the number of words, lines, and characters in a file. Options -c Display only the number of characters in the file. -l Display only the number of lines in the file. -w Display only the number of words in the file.

Command

Description

who

Who command lists the login names, terminal lines, and login times of the users who are currently logged on to the system.

Example $ who mumtaz pts/3 Feb 7 14:54 qamar pts/0 Feb 7 09:21 yousuf pts/2 Feb 7 10:12 22

If you type whoami, Linux displays who the system thinks you are: $ whoami mumtaz pts/2 Feb 7 09:25

Streams In Computer Science streams is an abstract idea. Informally, anything flow into the computer in the form of input, and anything flow out of computer in form of output are referred as streams. Formally, input flowing from input device like keyboard to memory is referred as input stream, and output flowing from memory to output device is referred as output stream. So streams are associated with flow of data. As we know when a Linux command executes it may take input before execution and gives some results after execution. Therefore, each command in Linux is associated with streams. Streams associated with the execution of Linux commands are: i) ii) iii)

Standard Input Stream (Stdin) Standard Output Stream (Stdout), and Standard error Stream (Stderr)

Normally, standard input flows from keyboard to the memory, and standard output and standard error flow to the terminal. When a Linux command executes, in case of errors, errors messages flow as output from computer to the terminal, is called standard error stream.

I/O Redirection Redirection changes the assignments for standard input and standard output. In normal cases standard input comes from keyboard, and standard output goes to the screen. The term redirection is used when the standard input will not be coming from the keyboard, but from some other sources like a file, and standard output will not be going to screen, but to some other sources like file or to other commands. In the case of I/O redirection the shell should be informed. To tell the shell to redirect input or output of any command, following characters are used.

Operator > >> < |

Description Redirects output of a command to a file or device (e.g. printer). It overwrites the exiting file. It is similar to >, except if the target file already exists, the new output is append to its end. Redirects input of command from a file or device. Sends the output of one command to become the input of another command.

Redirecting Output Normally, the output is sent to screen, output can be sent to some other sources like a file. The operator > is used with a command to redirect output. The format is as follow:

command > filename or

command >> filename Example As we know when the date command executes it sends its output to the screen. The output from the date command, for example, can be sent to a file xyz. 23

$ date > xyz Note:1) If the file already exists, then it will be over written, and the contents of the existing file lost. 2) If the specified filename does not exist, then the shell creates one to save the output in it.

Redirecting Input Normally, the input is taken from keyboard; input can be taken from other source like file. The operator < is used for redirecting input. The standard input may be received from a file rather then the keyboard. The operator < reads standard input from file rather than keyboard. The format is as follow: command < filename Example $cat < datafile The contents of datefile are read into the standard input by redirection operation. Then the cat command reads the input and displays the contents. Combining Redirection Operators The redirection operations for both standard input and standard output can be combined. For example, in this example, the standard input has been redirected to receive its data from a file, and the standard output has been redirected to place its data in a file. $ cat < fileone > filetwo Pipe Operator ( | ) In case of redirection the standard input and output are redirected to some sources other then default sources. The pipe command sends output of one Linux command to another Linux command. The pipe ( | ) operator (vertical bar) is placed between the two commands, forms a connection between them. The pipe operation receives output from the command placed before the pipe operator and sends this output as an input command placed after the pipe operator. The general format is as follows: CommandA | CommandB Example The commands ls and sort can be combined with the pipe. The list of filenames output by the ls command is piped into the sort command. $ ls | sort

File and Directory Security Files/Directories can be created by setting permissions, allow people to read, write, or execute your file. Each file on the machine divides the users of machine into three categories: · The file’s owner (who creates the file) · A group of users · Other users 24

Actually, there is one more type of user; that’s superuser. The system administrator may be the only superuser, but often several people have access to the superuser password. Anyone logged in as the superuser has total access to every file directory in the system.

Types of Access There are three types of access: · read · write · execute

File Permissions If a file has read permissions, it can be examined at a terminal, printed, viewed by an editor, and so on. If it has write permissions, the contents of the file can be changed (for example, by an editor), the file can be overwritten or deleted. If it has execute permissions, and is binary program or a shell script, that program can be run. Having a type of access is referred to as having access permission. You can change the access permission for your own file. For example, if you do not want anyone else to access a file, you can remove read access for anyone but you. If you want other users in your group to be able to write to group of files, you can extend write permission to group. Each of the three user types (the owner, the group, and others) can have any combination of the three access types for each file or directory.

Directory Permissions Directories have permissions modes that work in a similar manner to file permission modes. However, the directory access permissions have different meanings: Read: The read (r) permission in a directory means you can use the ls command to the filenames. Write: The write (w) permission in a directory means you can add remove files from that directory. Execute: The execute (x) permission in a directory means you can use the cd command to change to that directory. Note:- Your installation has default set up for all newly created files and directories. You can check your default access through the ls -l command.

Access Specification When you create a file or directory it come into existence with some default access specifications. It may give all access permissions to the owner, just read and write permissions to the group, and just read permission to every one, or there may be any situation. Following figure shows how different groups and characters represent access permissions.

drwx rw− r- user group others The first character indicates whether the file is a directory (d) or not (-) (- is for file). The next nine places are divided into three sets, each of length 3. The first set of three indicates the owner access, the next set of three indicates the group access, and the final set of three indicates the access for everybody else. The maximum access is represented by rwx, indicating read, writes, and execute. Whenever a dash (-) appears, an access permission has not been given.

Checking Access 25

The access privileges in a given directories can be checked by giving command ls -l

Example -rw-r- - r- access

1 links

saleem owner

stud 700 group size

June 19 08:00 last change

data.file name

In this example, the first character is −, indicating that object is a file. The owner’s group indicates that the owner i.e. saleem can read and write, but could not execute. Any group member can read the file but do not execute. Also some other person can read this file but do not write it or can execute it.

Groups It is the authority of administrator to create a group of users. Each group has number of users, and given a unique name. Each group has also a unique number (group id), and each user has also a unique number, called user-id or UID.

Command

Description

id

It gives user’s name together with the groups they are member, both names and numbers, and the users user-id and current group-id.

Example $ id (enter) uid=275(john1), group=50(staff) $id chris uid=145(charis) gid=12(ugrads) groups=12(ugrads), 417(proj) This shows that chris is a member of groups ugrads, and proj, with GID numbers 12 and 417 respectively. Currently chris is allotted to group ugrads. The access privileges can be changed. A command chmod is used to change the access privileges.

Command

Description

chmod

Change access permission for one or more files. chmod user-type [operations][permissions] filelist user-type u User or owner of file. g Group that owns the file. o Other. a All three user types. operations + Add the permission. Remove the permission. = Set permission; all other permission reset. permissions r Read permission. w Write permission. x Execute(run) permission.

Examples $chmod u-w result.comp 26

Write permission for owner (user) removed from file result.comp. $chmod go+r stock Group and other users get read access for file stock. $chmod g=r+x myfile It will set group access for reading and executing, but not write to it. However, you can specify the new mode as a three-digit number that is computed by adding together the numeric equivalents of the desired permission. Following table shows numeric value assigned to each permission letter.

owner w r x 4 2 1

Group w r x 4 2 1

Other w r x 4 2 1

Example Change access permission to allow the read, write, and execute permission to all users. $ chmod 777 final.

Operating System CS211L

Lab – 05

Background/foreground Execution, Wild Cards In this lab we will discuss Background/Foreground execution of shell programs and Wild Cards.

Background and Foreground Processing 27

Since Linux is a multi-tasking system, therefore, there are number of processes running alternatively. When a command is entered through the command prompt, the operating system starts the process and the command is executed. The command prompt does not appear until the command is finished. After executing the command, it will re-display the prompt to indicate your process is finished. Such a processing is called foreground processing. There can only be one foreground process.

Background Processing As there are number of processes running at the same time, all the processes except foreground process are known as background processes.

When to run a process in background The answer is straightforward. If a process will take a bit longer time in its execution, and does not require any user/interactive input, then it would be desirable to run the process in the background for example, sorting a long data file. So if a time consuming process is running in the background, one can continue working at the terminal on other tasks.

How to execute a command/process in background: It is possible to move some of the programs to the background so that you can work on something else. The following example shows you how to move a process to the background. cat > colors red green blue When we press control-z, we get the prompt again. What happened? The cat command moved into the background. You can see that it still exists (check out the output of ps command) command. Now type: $fg The ‘cat’ command will be brought out to the foreground again. Type in a few more lines. Press CTRL-d to finish your input into the file. Note: fg is an abbreviation for ForeGround. This was one way of moving a process into the background. There is another way, which we will now explore. Try the following command. ls –alR / 2> /dev/null | sort > myfile

Explanation of The Above Command: We want to list all files under the root directory (/). We use -R option to ls, so that it recursively traverses all the directory hierarchy under the root (/). As normal users (while you are logged on as oslab...) you don't have enough permissions to access all the files/directories under the root directory that's why we specify 2>/dev/null, which means that we are going to redirect all the error messages (Permission denied error messages...) to /dev/null. 2 here stands for the error stream (stderr). /dev/null is a file which discards/drops everything that is put into it. So 2>/dev/null means we are trying to get rid of all the error messages (i.e. we don't want them showing up on our screen). Then we pipe this listing of files into sort command, which sorts it and saves it in a file called myfile. You can specify any other file name instead of myfile. 28

Now, the system will wait until the above command ends, after which the prompt comes back. Now if we try: ls –alR / 2>/dev/null | sort > myfile & The last character (&) indicates that we want to run this process in the background. Therefore, you get the prompt immediately. Typing ps again will show you that the command is still there. Now there is a difference b/w ”&” and ctrl-z. With ctrl-z alone, the command moves into the background, and stops executing, which means that you can wait as long as you want, and it will not finish. To make it start executing again, enter bg [for BackGround execution]. This command tells the background process to start executing again, but in the background. To bring it to the foreground, type fg.

The ps Command It is used to display the currently running processes on Unix/Linux systems. If you know the 'TaskManager" which pops up under Windows NT/2000/XP when you press CTRL+ALT+DEL then you will quickly grasp what ps does on Linux.

Terminating a Process Not all programs behave normally all the time. A program might be in an infinite loop or waiting for resources that are not available. Linux provides kill command to terminate the unwanted process. The kill command sends a signal to the specified process. The signal is an integer number, and the process is identified by the process ID number (PID).

$ kill -9 5432 Here –9 is kill signal and 5432 is process ID, which is going to be killed. Try to kill a process. First use ps command to list the processes.

Wildcards Wildcards are special characters (metacharacters), which are applied with a command. These characters operate on group of files/directories. Most of times these commands having wildcards, operate on group of files/directories which have some common features in their names. When wild card(s), and other characters are grouped together they form a “pattern”. For example, in case there is a group of files in the current directory, in this group following files have common feature in their names that start with the character ‘t’. - - - t t1 tile1 tile2 - - - In order to select these files from the group of files, a command like ‘ls’ must be operated with a suitable wildcard. The symbols used for wild cards are following.

Symbol

Meaning

* ? []

It matches zero or any number of characters It matches any single character in a file/directory It matches any one character in the bracket.

Example 29

Following are the files in a given directory, the results returned by different wildcards when used with ‘ls’ is shown below.

fan fat fest foo loo on p1 p2 p7 p9 t t1 test tile1 tt2 NOTE: You don't have to create all the above files using nano as that would be cumbersome. You can instead use touch command and specify all the filenames as arguments to it. It would create empty files for you with just one command as in:

$touch file1 file2 myfile wild card * $ls *t returns fat fest t $ls t*

(last character should be ‘t’ with any number of preceding characters)

returns t t1 tile tt2 (first character should be ‘t’ with any number of following characters)

$ls t*t returns test between them)

(first and last characters should be ‘t’ with any number of characters

$ls f *t returns fat fest (first and last characters should be ‘f’ and ‘t’ respectively with any number of characters between them) $ls *oo returns foo loo zoo (last two characters should be ‘oo’ with any number of preceding characters.)

wildcard ? $ls t?

returns t1

(first character should be ‘t’ with only one character following it)

Note:- file named ‘t’ has not been selected. $ls t?t returns tat (first and last character should be ‘t’ with only one character between them)

wildcard [] $ls p[12] returns p1 p2 (starting character should be ‘p’ and ending character should be ‘1’ or ‘2’) $ls p[1-9] returns p1 p2 p7 p9 (starting character should be ‘p’ and ending character could be anything between ‘1’ to ‘9’) $ls p[!1-7]

returns p9 (starting character should be ‘p’ and ending character should not be ‘1’ to ‘7’)

More on wild cards ???t Four character file name. First three characters may be any, but last character should be ‘t’. ??[a-c] Three character filename beginning with any two characters, but the last character should be ‘a’, ‘b’, or ‘c’. [a-c][1-9] Two character file name starting with ‘a’, ‘b’, or ‘c’ and ending with any character between ‘1’ and ‘9’.

Operating System CS211L

Lab – 06 Regular Expressions (GREP)

Global Regular Expression Parser (grep): Grep allows very fast search for regular expressions through files. 30

Command

Description

grep

Find lines in one or more files that contain a particular word phrase.

The command format for grep is as follows. $ grep [options] Text/RE files Options : -c Print number of matches only. -i Ignore case. -I Print only filenames containing the pattern. -n Precede each matched line with its line number in the file. -s Suppress file error messages -v Print line NOT containing the pattern -B 5 Display the context - i.e. 5 lines of context before a match -A 3 Display the context - 3 lines of context after a match If the RE contains symbols (like ^, *, []), then it must be quoted in single or double apostrophes. But it is recommended to use single quote. · Files may be specified with wild card characters. Before going to basic regular expressions let’s have a look at grep command in general: Example 1 $ grep Text filenames Here Text is a string to be searched in file(s). $ grep –n S filename Print all lines (with line number) containing a S. $ grep -i “ha” * Prints HA/Ha/hA/ha from all files in the current directory. $ grep -c pass result Print number of times the string pass occurred in a file result. $ grep -v -i -c ‘s’ testfile Print number of lines not containing s or S.

Regular Expressions In Linux regular expressions are used to match a character string within text (e.g. a file) that you want to match. For example, a character string ‘cool’ has a match within the text ‘in the cool nights of winter’. The character string to be matched is called pattern, and associating (matching) this pattern with a text is called pattern matching. So pattern matching is performed in Linux through regular expressions.

Symbol

Description

. * \

Matches any single text character Matches zero or more preceding characters Turns off any special meaning of the character following; 31

^

It matches the beginning of a line, if used at the beginning of a regular expression. $ It matches end of the line, it is used at the end of a regular expression. [str] Matches any single character in str. [^str ] Matches any single character not in str. [a - b] Matches any character between a and b. The basic regular expressions are applied through grep command. The regular expressions are formed using meta characters in some suitable format. The format of the pattern based on meta-characters will depend upon our searching criteria.

Example 2 h.t ho*t \. ^The ed$

Matches hot, hit, hst, h8t, h&t, and so on in afile. Matches hot (zero occurrence of the preceding pattern), hoot, hooot etc. Matches any line containing a period. Note the use of the backslash to show we mean a period and not the special symbol for any character. Matches any line starting with The. Matches any line ending with ed.

$ grep -n '^[a-f]' test Print lines that start with either a, b, c, d, e or f.

Difference between Wildcards and Regular Expressions Regular expressions and wildcards are both used for pattern matching. However, there is a difference between them. Wildcards are used for matching file names, with file commands like ls etc. Where as regular expressions are used for matching patterns within text-files.

Character Classes Characters may be letters, numbers, punctuation marks, etc; these are character classes. Linux provides notations for referring to these classes, which is used by some utilities like grep. The general form of character classes is [: classname:] Following are the character classes: [:alnum:] letters and digits [:alpha:] letters [:blank:] space and TAB [:digit:] digits [:cntrl:] all control letters [:lower:] lower case letters [:punct:] punctuation marks [:upper:] upper-case letters [:xdigit:] hexadecimal digits (0-9A-Fa-f) There are some other character classes which are not mentioned here.

Examples Following are the examples in which character classes are used in regular expressions. 32

Matches any 3-chracter string commencing with a letter. $grep “[[:alpha:]] ..” filename like a12, bb3, sad, ga1 Matches any string three or more digits $grep “[[:digit:]][ [:digit:]][ [:digit:]]*” filename

Operating System CS211L

Lab-07 Shell Programming Introduction

Shell Programming What is Linux Shell? 33

Shell accepts your instructions or commands and if they are valid, then they are executed. Shell however also, is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file. Several shells are available with Linux usually including:

Shell Name BASH ( BourneAgain SHell )

Developed by Brian Fox and Chet Ramey

Where Free Software Foundation

Remark Most common shell in Linux. It's Freeware shell.

CSH (C SHell)

Bill Joy

University of California (For BSD)

The C shell's syntax and usage are very similar to the C programming language.

KSH (Korn SHell) TCSH

David Korn See the man page. Type $ man tcsh

AT & T Bell Labs --------

------TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH)

Note that each shell does the same job, but each understand different command syntax and provides different built-in functions. To find your current shell type, issue the following command:

$ echo $SHELL The shell is also a program, which acts as interface between a user and the Linux system. It allows a user to enter commands for operating system to execute. The Shell is running/ operational as long as you are logged on to the system.

Shell Script A Shell Script consists of a sequence of commands. The shell language is a command language with a lot of features common to many computer programming languages. A shell script is a file, which contains Linux commands, language constructs as well as other specific commands specific to the shell. The shell executes shell script in sequence.

Creating Shell Script A shell script is created by using any text/ ASCII editor. This file is interpreted by Shell. Example 1 The following is a simple Shell script. This script first clears the screen and then says hello. Write this script in a file named first. clear echo “Welcome to Shell Scripting” Before executing this file, first set the execution permission. $ chmod u+x first And execute this script on the command line by typing $ . /first 34

When the Shell executes this script, first the screen is cleared, and then the string Welcome to Shell Scripting will be printed on the screen. Example 2 Create a file name myscript with the following content: echo –e “The current date and time : \c” date echo –e “The number of users : \c” who | wc –l echo –e “Personal status : \c” whoami exit 0 Note : First remove -e then execute and then \c and execute. Finally remove both and execute. Notice the changes in output.

Comments in the Script Comments are messages, which are ignored by the Shell when it interprets a script. Comments are for the benefit of the programmer, and for anyone reading the script. The shell recognizes # as the comment symbol; therefore characters after the # are ignored. The following command sequences show examples of comment lines: #…………………….. This is a comment line. # program 3 This is also a comment line date #show the current date Comment in the middle of the line

Shell Variables In computer programming, a variable is a name associated with a value. It offers a symbolic way to represent and manipulate data. Most computer languages have variables, and so does the shell. Following are types of Shell variables. · User Defined · Predefined and Positional Parameters · System Shell Variables At the moment we are doing only user defined variables, rest we will do in the coming labs.

User Defined Variables To store a value in a variable, you simply write the name of the variable with an equal sign and the value you want to store in the variable, like this: variable=value Note:- Spaces are not permitted on either sides of the equal sign. The Linux shell does not support data types (integers, character, floating point, etc.) as some programming languages do. It interprets any value assigned to a variable as a string of characters. Example 3 pet=bulldog This statement creates variable having the name pet; its value is the “string” 35

bulldog. weight=123 menu=”The Main Menu” If your string contains embedded white spaces, then you must place it between quotation marks.

Null String There is one special string of some importance, and that is null string. It is a string consisting of no characters. Any of the following equates a variable to the null string: turn=’ ’ next= new=””

Displaying of Variables Value stored in a variable can be displayed by using echo command. To print value of a variable the variable name must be preceded by a $ sign. $ echo $pet Example 4 TIME=”The current date and time: \c” USERS=”The number of users : \c” PERSONAL=”Personal Identity : \c” echo –e $TIME date echo –e $USERS who | wc –l echo –e $PERSONAL whoami exit 0

Reading Variables from Keyboard Variable values can also be read from keyboard. Reading from keyboard is possible through read command. The format of the read statement is: $ read list of variables e.g. $ read X Y Z (Note there is no comma. Also pressing enter would complete our input.)

Example 5 Write the following script, and see what it does. QUESTION=”What is your name: \c” echo –e $QUESTION read NAME echo “How do you do $NAME ?” 36

Operating System CS211L

Lab-08 Arithmetic in bash, Command Substitution

Doing Arithmetic in Linux All variables used in a bash are string variables. If in Linux one has to use numeric variables, and has to perform numeric computations, then they are: Performed using expr command 37

The basic syntax for expr command is expr arithmetic expression expr can perform simple arithmetic calculations involving addition, subtraction, multiplication, division, and remainder operation for integers. It can also perform comparisons using C like comparison operators. Here are some examples to understand expr command: $ expr 2 + 3 (Note that there is a space between 2 and +, and, + and 3) 5 $ expr 2 + 3 / 3 (there must be spaces between arithmetic operators and numbers) 3 $ num=25 $ expr $num % 10 See another example $ result=`expr $num % 6` (Note ` is the back quote, ) $ echo $result

Example 1 A file contains the following script # A Script with numeric variables X=5 Y=0 echo "The value of X = " $X Y=`expr "$X" + 5` echo "The value of Y = " $Y

(Notice back quotes in this case)

The out put of the above program is The value of X = 5 The value of Y = 10 Since * is used as a wildcards and in regular expressions as well, so in multiplication shell should be told to ignore its original meanings, e.g. $expr 5 \* 5 25 We are solving expressions only containing integer constants or integer variables. The question is can we solve expression containing floating-point values. The answer is to first to do an experiment. Try to solve the following expression containing a floating point: [email protected]~$ expr 2.1 + 2 expr: non-numeric argument So using expr we cannot solve expressions involving floating-points. Then what shell we do? The solution is here: 38

Solving Expressions Involving Floating-points Values/Variables The floating-point arithmetic involves the bc command. So we look at the bc command. bc is a calculator, which is used for arithmetic operations. To work with bc, write bc on the command prompt and press enter. A working area will be displayed, in that you can do arithmetic operations. For example when you give bc command at the prompt: (press ctrl-d, when you finish) [email protected]~$ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 2+4 6 2*5 10 10/5 2 15/2 7 Press ctrl-d when you finish. You saw that when you divide 15 by 2 it returned 7, not 7.5. The default behavior of bc is to treat all numbers as integers. To get floating point numbers, scale command is used. For example, the following input tells bc that you want it to set four decimal places: [email protected]~$ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. scale=4 15/2 7.5000 ctrl-d Similarly scale=2 sqrt(50) 7.07 So now you can perform floating-point arithmetic using bc command. Now we come to point that how we shall perform floating-point arithmetic on command prompt or in shell programming. First to note that we don’t use expr command for floating point operations. For example to calculate 5.5 + 6.9, we do as follow: [email protected]er~$ echo 5.5+6.9 | bc 12.4 [email protected]~$ echo "scale=3; 15/2" | bc 7.500 [email protected]~$ echo "scale=4; 1043/6" | bc 39

173.8333 The result may be assigned to a variable like this: [email protected]~$ var=`echo "scale=3; sqrt(30)" | bc` [email protected]~$ echo $var 5.477 The quotes `` may be replaced by braces preceded by $ sign: [email protected]~$ var=$(echo "scale=3; sqrt(30)" | bc) [email protected]~$ echo $var 5.477

Command Substitution Variables can be assigned values by command substitution. How can it be possible? Before going to that, first look at command substitution. The single back quotation marks ` (button just above the tab key on your keyboard) tell the shell to execute the enclosed command and to insert the command’s output at the same point on the command line. It is also called command substitution. The format is as follow: `command` Where command is any valid Linux command that is to be executed. The following command sequences show examples of command substitution. $ echo "The date and time is: `date` " The date and time is: Thu Mar 6 14:46:10 PKT 2003 Using command substitution, variables can be assigned values, for example: $ A=”`date`” $ echo $A Thu Mar 6 14:50:04 PKT 2003

Operating System CS211L

Lab -09 Shell Programming/Scripting

Predefined or Positional Parameters (Variables) These variables are predefined, and we can only access them. We can only store values in them, but not define them, because they are predefined. As we know that a Linux command can take arguments. Similarly, a Script can also take arguments. Let’s make it clear with an example. As we know that commands accept arguments in the command line. For example, the command 40

mv here there Uses the arguments here and there to tell the mv command the original and the desired filenames. We can use the same approach in a shell script. Shell provides positional variables, which we use inside the script to store arguments. We refer to the first argument as $1, the second argument as $2, and so on. In addition, the special symbol $0 stands for the name of the command itself, $* stands for all the arguments from $1 on up. One more special notation is $#, which represents the number of arguments. The shell positional variables are summarized in the following table: Positional Variable $0 $1,$2, ….$9

Description Contains name of the script, as typed on command line. Contains the first through ninth command line parameters, respectively.

$# $*

Contains the number of command line parameters Contains all command line arguments “$1,$2, ….$9” passed to the program

[email protected]

Contains all command line arguments “$1,$2, ….$9”, just like $*

$$

Contains the PID (Process ID) number of executing process

Example 1 Put this code in a file testpv, and executes with two arguments as given below: $./testpv A S echo “The following is the output of $0 script:” echo “The total number of command line arguments: $#” echo “The first parameter: $1” echo “The second parameter: $2” echo “This is the list of all the parameters: $*” The output of this program is: The following is the output of ./testpv script: The total number of command line arguments: 2 The first parameter: A The second parameter: S This is the list of all the parameters: A S

Assigning values to positional variables An other way to assign values to the positional variables is to use the set command. What you type as the set command arguments are assigned to the positional variables. 41

$ set A B C $ echo $1 $2 $3 ABC

Shell Programming: Control Flow We have seen different capabilities of shell. But like other programming languages, it has ability to control the flow of program. The shell can loop through a script of similar operations, and it can choose among alternatives, taking actions that depend on the current conditions. In this lab we will look at shell’s mechanism for control flow. There are different options for doing this. Let’s start doing this one by one.

Testing Conditions There are two ways to evaluate a condition. One way is by using test command, and the other way is by using [] constructs.

The test command The test command can be applied on the integers, strings, and files. i) Numeric Values test command can be used to compare two integers numbers. You can also combine expressions comparing numbers with logical operators. The format is as follows: test expression-1 logical operator expression-2 The operators available for comparing variables that hold numeric values are summarized in the following table. Mathematical Meaning Operator

Normal Statements

For test statement with if command

For [ expr ] statement with if command

-eq -ne -lt -le -gt -ge

is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ] is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ] is less than 5= 6 if test 5 -ge 6 if [ 5 -ge 6 ] equal to NOTE: == is equal, != is not equal. These comparison operators can be grouped together with logical operators. The logical operators are as follow:

Operator

Operator

! expression expression1 -a expression2 expression1 -o expression2

Logical NOT Logical AND Logical OR

ii) String Values You can also compare strings with the test command. The test command provides a different set of operators for the string comparison. These operators are summarized in the following table. 42

Operator String1 = string2 string1 != string2 -n string1 -z string1

Meaning string1 is equal to string2 string1 is NOT equal to string2 string1 is NOT NULL and does exist string1 is NULL and does exist

iii) Files You can use test command to test file characteristics, such as file size, file type, and file permissions. There are more than ten file attributes that can be checked; a few of them are introduced here. The following table summarizes the file test operators. Test -s file -f file -d dir -w file -r file -x file

Meaning Non empty file Is File exist or normal file and not a directory Is Directory exist and not a file Is writeable file Is read-only file Is file is executable

The Exit Status of a Command $? reads the exit status of the last command executed. By convention, it returns 0 on success or an integer in the range 1 to 255 on error. Command Syntax: echo $? A script contains following code: Example 1 dog=lassie test $dog = lassie echo $? test $dog = pluto echo $?

#no spaces before and after = #spaces before and after = #test is true, therefore $? sets to 0 #test is false, therefore $? sets to 1

Its output is as follow: 0 1

The [] command As we have seen that test command is used to test a condition. There’s another way to test a condition. Using [] we can test a condition. What you do that, you must put spaces between the [] braces and the condition being checked. Writing [] is just the same as writing test. On my system, there’s a file in my current directory. The file name is script1. [email protected]:~$ test -r script1 [email protected]:~$ echo $? 0 OR 43

[email protected]:~$ [ -r script1 ] [email protected]:~$ echo $? 0

([] must have spaces)

and there’s no file like count, therefore: [email protected]:~$ test -r count [email protected]:~$ echo $? 1 OR [email protected]:~$ [ -r count ] [email protected]:~$ echo $? 1

Operating System CS211L

Lab -10 Shell Programming/Scripting

Shell Programming: Control Flow We have seen different capabilities of shell. But like other programming languages, it has ability to control the flow of program. The shell can loop through a script of similar operations, and it can choose among alternatives, taking actions that depend on the current conditions. In this lab we will look at shell’s mechanism for control flow. There are different options for doing this. Let’s start doing this one by one. 44

Testing Conditions There are two ways to evaluate a condition. One way is by using test command, and the other way is by using [] constructs.

The test command The test command can be applied on the integers, strings, and files. i) Numeric Values test command can be used to compare two integers numbers. You can also combine expressions comparing numbers with logical operators. The format is as follows: test expression-1 logical operator expression-2 The operators available for comparing variables that hold numeric values are summarized in the following table. Mathematical Meaning Operator

Normal Statements

For test statement with if command

For [ expr ] statement with if command

-eq -ne -lt -le -gt -ge

5 == 6 5 != 6 5= 6

if test 5 -eq 6 if test 5 -ne 6 if test 5 -lt 6 if test 5 -le 6 if test 5 -gt 6 if test 5 -ge 6

if [ 5 -eq 6 ] if [ 5 -ne 6 ] if [ 5 -lt 6 ] 6 if [ 5 -le 6 ] if [ 5 -gt 6 ] if [ 5 -ge 6 ]

is equal to is not equal to is less than is less than or equal to is greater than is greater than or equal to

NOTE: == is equal, != is not equal. These comparison operators can be grouped together with logical operators. The logical operators are as follow:

Operator

Operator

! expression expression1 -a expression2 expression1 -o expression2

Logical NOT Logical AND Logical OR

ii) String Values You can also compare strings with the test command. The test command provides a different set of operators for the string comparison. These operators are summarized in the following table. Operator String1 = string2 string1 != string2 -n string1 -z string1

Meaning string1 is equal to string2 string1 is NOT equal to string2 string1 is NOT NULL and does exist string1 is NULL and does exist 45

iii) Files You can use test command to test file characteristics, such as file size, file type, and file permissions. There are more than ten file attributes that can be checked; a few of them are introduced here. The following table summarizes the file test operators. Test -s file -f file -d dir -w file -r file -x file

Meaning Non empty file Is File exist or normal file and not a directory Is Directory exist and not a file Is writeable file Is read-only file Is file is executable

The Exit Status of a Command $? reads the exit status of the last command executed. By convention, it returns 0 on success or an integer in the range 1 to 255 on error. Command Syntax: echo $?

The [] command As we have seen that test command is used to test a condition. There’s another way to test a condition. Using [] we can test a condition. What you do that, you must put spaces between the [] braces and the condition being checked. Writing [] is just the same as writing test. On my system, there’s a file in my current directory. The file name is script1. [email protected]:~$ test -r script1 [email protected]:~$ echo $? 0 OR [email protected]:~$ [ -r script1 ] [email protected]:~$ echo $? 0

([] must have spaces)

and there’s no file like count, therefore: [email protected]:~$ test -r count [email protected]:~$ echo $? 1 OR [email protected]:~$ [ -r count ] [email protected]:~$ echo $? 1

Making Choices: The if¾then Construct The if statement provides a mechanism to check whether a condition is true or false. Depending on the results of the if test, you may change the sequence of command execution in your program. The following shows the if statement syntax: 46

if [ condition ] then commands ..... last-command fi If the condition is true, then all of the commands between the then and fi, what is called the body of if loop, are executed. If the condition is false, the body of if is skipped, and line after fi is executed. Look at this simple script: A=1 B=10 if test $B -gt $A then echo "$B is greater then $A" fi The output of the above script is: 10 is greater then 1 Expressions can be combined with the logical operators AND (-a), OR(-o), and NOT (!). Example if test $A –gt $B -a $X –lt $Y

The if-then- else Construct By adding the else clause to the if construct, you can execute certain commands when the test of condition returns a false status. The if-then-else construct has the following syntax: if [ condition ] then commands . . else commands . . fi If the condition is true, then all commands between the then and else, will be executed. If the condition is false, the body of if is skipped and all the commands between else and fi, the body of else, will be executed.

The if then- elif then- else Construct There is one further extension of the if form, one that lets you string several alternatives (multiples else) one after the other. The key is using the keyword elif, which is short for else if. The general form looks like this: if [ condition ] then command(s) elif [ condition ] 47

then command(s) else commands(s) fi If the first condition is successful, then the commands after the first then up to the elif are executed. Otherwise, second condition after the elif is attempted; if it is successful, then the commands after the following then up to the next elif or else are executed. This process continues until the closing fi is reached.

Sequential Commands: (Semicolon) You can put several commands on the same line by separating them with a semicolon. You might do this, for example, to make sure that related commands in a shell script are kept together. For example:

$echo “Good Bye”; exit In the above example two commands echo and exit are put on the same line.

Operating System CS211L

Lab – 11 Shell Programming/Scripting

The case Structure The case structure allows you to specify any number of different actions for different values of a variable. The general format of case statement is: case expression in value 1) commands;; value2) commands;; value3) commands;; 48

........ ........ *) commands;; esac

Example A script contains following commands in a file testcase, run this script and see the results. echo "Enter a number between (1-3)" read NUMBER case "$NUMBER" in 1) echo "You pressed one.";; 2) echo "You pressed two.";; 3) echo "You pressed three";; *) echo "you pressed invalid number.";; esac There are different options available with the case construct. You can match strings too.

Example echo "Input a string : " read STRING case $STRING in one) echo "You have written one";; two) echo "You have written two";; three) echo "You have written three";; *) echo "You have written an invalid string!!.";; esac

Setting up choices: You can give choices in the label. As in above example, when a user writes one the script writes You have written one. But what if the user enters 1. The user would not get You have written one. In order to attach more than one label to the same response, we use the pipe symbol ( | ). Here | does not mean pipe, but it means or. For example, in order to print the message You have written one, when a user enter 1 or one, we amend the label and now it will look like this (there should be spaces before and after | ): 1 | one) echo "You have written one";; We can use Linux shell’s pattern-matching abilities (aka wildcards) in the choice labels. We used one example already, when we used *) to match any pattern. We can also use the ? and [] meta-characters. They are used for matching patterns in the same way as they are used in matching files on the command-line. For example: case $1 in dog) echo “Man's best friend” ;; [aeiouAEIOU]*) echo word beginning with a vowel;; ??) echo a two-letter word;; *) echo “I don't know” ;; esac 49

Here the pattern [aeiouAEIOU]*) stands for any one letter from within the brackets followed by any characters whatsoever.

More on using case The case value portion can be a shell variable, a shell script argument or any thing else that produces a value. For example: Case `pwd` in The back quotes cause the output of pwd, to be the value against which the choices are compared.

The For Loop: The for-in-done Construct The for loop is used to execute a set of commands a specified number of times. The basic format is as follow: for variable in list-of-values do commands . . . done In more general form the above format has the following form: for variable in do commands . . . done

value1 value2 value3 . . . . .

The shell scans list-of-values, stores the first value in the loop variable, and all the commands between do and done (body of the loop) are executed. Next, the second word is assigned to the loop variable, and again the body of the loop is executed. The commands in the body of the loop are executed for as many values as the number of list-of-values.

Example for count in 1 2 3 do echo “In the loop for $count times” done The output of the above program is given by: In the loop for 1 times In the loop for 2 times In the loop for 3 times Example 50

#Script which prints the names of planets count=1 echo "Names of planets" for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $count")" $planet count=$(expr $count + 1) done The output of the above script is given by: Names of planets 1) Mercury 2) Venus 3) Earth 4) Mars 5) Jupiter 6) Saturn 7) Uranus 8) Neptune 9) Pluto

Generating Values for a for Loop There are many ways to get values to a for loop variables. Here are four: 1. List the values explicitly after in, as in for name in one two three 2. Take the values from the script arguments, as in for name in $* 3. Take files/subdirectories from a directory as values. for file in * 4. Take values from shell variables. read Name for N in $Name

The While Loop: The while-do-done Construct The second type of looping construct is while loop. Unlike the for loop, whose number of iterations depends on the number of values in the list, the while loop continues as long as the loop condition is true. The format is as follow: while condition do commands . . . done Example # While loop example X=3 51

while [ $X -ne 0 ] do X=`expr $X - 1` echo "X = " $X done It’s out is given by: X=2 X=1 X=0

Operating System CS211L

Lab – 12 Shell Programming/Scripting

Shell Programming Today’s lab is last lab on scripting. In the last lab we had studied for, while, and case constructs. Now you are familiar with selection and iteration. In this lab we would continue looping and look into function.

The Until Loop: The until-do-done Construct until-do-done construct is a third way to iterate commands. The until loop is similar to the while loop, expect that it continues executing the body of the loop as long as the condition of the loop is false (non52

zero). The until loop is useful in writing scripts whose execution depends upon other events occurring. The format is as follow: (Execute statements as long as a condition is false) until condition do commands ……. ……. done

Example This script will iterate until you enter 0 (zero) echo "It will not terminate until you press some non-zero" echo "Read a value" read X until [ $X -eq 0 ] do echo "The will not terminate until you press some non-zero" echo "Read a value" read X done echo “Good Bye” The results of above script: The will not terminate until you press some non-zero Read a value 2 The will not terminate until you press some non-zero Read a value 1 The will not terminate until you press some non-zero Read a value 9 The will not terminate until you press some non-zero Read a value 0 Good Bye

Functions We can define functions in the shell. Function helps dividing a script into small parts. To define a shell function, we simply write its name, followed by empty () parentheses and enclose the statement in {} braces: You must always define a function before you invoke it. function_name() { 53

statements } The function can be called any where in the script by using its name. function_name

Example # function hello() just prints a message hello() { echo "Hello" } echo "Calling function hello()..." hello echo "You are now out of function hello()"

Passing parameters to a function # function hello() receives parameters hello() { echo $1 $2 $3 $4 echo $# echo $* } This is how it is called: hello 1 2 3 4 # 1 2 3 4 are parameters The out put of the script is: 1234 4 1234 In this script parameters 1 2 3 4 are passed to the function hello. In side the function they are accessed through the positional parameters to the script, $*, $#, $1, $2, $3 etc. That’s how you read parameter passed to the function. When the function finishes, they are restored to their previous values.

Passing parameters and returning values The parameters and return value can be making understood by the following script. hello() { A=`expr $1 + 5` echo $A #return value contained by A } value=`hello 10` # hello is called, and10 is passed as parameter echo $value

The bash shell allows a number of methods for accessing elements of variable arrays. This recipe demonstrates some of these techniques. Given the array defined by the following code: 54

names=( Jennifer Tonya Anna Sadie Molly Millie) The individual elements in the array can be accessed by their numeric index (remember that they start counting from zero) with: ${names[0]} -> Jennifer ${names[3]} -> Sadie All of the elements can be accessed at the same time (which is useful in a for loop) with the following: ${names[@]} ${names[*]} The number of elements in the array can be obtained with: ${#names[@]} -> 6 A range of elements can easily be specified with the following syntax: ${names[@]:2:3} -> Anna Sadie Molly ${names[@]:3} -> Sadie Molly Millie The first example starts at element 2 (the third element) and returns the next three elements (:2:3). The second example starts at record 3 and returns all of the remaining records (:3).

55

View more...

Comments

Copyright ©2017 KUPDF Inc.