logging in or signing up Fundamentals_Unix shashank249 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 248 Category: Education License: All Rights Reserved Like it (3) Dislike it (0) Added: March 13, 2011 This Presentation is Public Favorites: 2 Presentation Description No description available. Comments Posting comment... By: prabauma (3 month(s) ago) hai i am praba I think this Presentation is useful for my teaching.please if u can u send this ppt to my mail prabachandru@gmail.com Saving..... Post Reply Close By: wuqiang97 (2 month(s) ago) Fundamentals of UNIX: Copyright © 2007, Wipro Limited, Bangalore Fundamentals of UNIX Version Saving..... Edit Comment Close Premium member Presentation Transcript Fundamentals of UNIX: Copyright © 2007, Wipro Limited, Bangalore Fundamentals of UNIX Version 2.0Fundamentals of UNIX: 2 Fundamentals of UNIX Day 1 UNIX Operating System UNIX Commands Files and Directories UNIX Utilities Process Day 2 Shell Programming Lab ExercisesModule 1: UNIX Operating System: 3 Module 1: UNIX Operating System In this module, you will learn about: The functions of OS The history of Unix The features of UNIX The Unix architecture Process management CPU scheduling Memory management File managementOperating System (OS): 4 Operating System (OS) OS can be defined as an organized collection of software consisting of procedures for operating a computer. OS provides an environment for execution of programs. OS acts as an interface between the user and the hardware of the computer system.Operating System (Contd.).: 5 Operating System (Contd.). Operating system interacts with user in two ways: Operating system commands Enables user to interact directly with the operating system. Operating system calls Provides an interface to a running program and the operating system. System calls in UNIX are written in C.History of UNIX: 6 History of UNIX Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s Two versions of UNIX that emerged are AT&T Unix and BSD Unix In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4) Two of the main standards mainly in use are POSIX (Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium developed vendor-neutral Xwindow System.What is Linux?: 7 What is Linux? An open-source UNIX like operating system Initially created by Linus Torvalds for PC architecture Ports exist for Alpha and Sparc processors Developer community world-wide contribute to its enhancement and growthFeatures of UNIX: 8 Features of UNIX Multi-user, multitasking, timesharing Portability Modularity File structure Security Strong networking support & advanced graphicsLayered Architecture: 9 Layered Architecture hardware kernel shell ld as comp cp vi ed grep wc date a.out who sh sort ls banner ...UNIX System Architecture: 10 UNIX System Architecture Unix system follows a layered approach. It has four layers The innermost layer is the hardware layer In the second layer, the kernel is placed The utilities and other application programs form the third layer Fourth layer is the one with which the user actually interacts.Kernel: 11 Kernel Kernel is that part of the OS which directly makes interface with the hardware system. Actions: Provides mechanism for creating and deleting processes Provides processor scheduling, memory, and I/O management Provides inter-process communication.The Shell: 12 The Shell A utility program that comes with the UNIX system. Features of Shell are: Interactive Processing Background Processing I/O Redirection Pipes Shell Scripts Shell Variables Programming ConstructsProcess Management: 13 Process Management A process is a program in execution. Several processes can be executed simultaneously in a UNIX system. A process is generally created using the “fork( )” system call. The process that invokes the “ fork( )” system call is the parent process, and the newly created process is called the child process.CPU Scheduling: 14 CPU Scheduling Unix uses round-robin scheduling to support its multi-user and time-sharing feature. Round-robin fashion of scheduling is considered to be the oldest, simplest and widely used algorithm. Every process is given a time slice (10-100 millisec.)Memory Management: 15 Memory Management Virtual memory Swap area Demand pagingFile Management: 16 File Management UNIX uses a hierarchical file system with “/” as its root. Every non-leaf node of the tree is called as a directory file. Every leaf node can either be a file, or an empty directoryFile System: 17 File System dev bin tmp home etc lib usr console lp0 sh ls passwd inittab bin user1 user2 var src spoolFile System (Contd.).: 18 File System (Contd.). File system is the structure in which files are stored on disk File in UNIX is sequence of bytes organized in the form of blocks The size of each block is 512 bytes (depends on architecture) Block size can be decided while creating the file system structureFile System Structure: 19 File System Structure Boot Block Super Block Inode Block Data Block Type of the file Link counter Uid, gid, size Date and time of Creation Date and time of access Date and time of modification : : Address of datablock Address of datablock : : Address of the addr block Address of the addr block Address of the addr block Single indirect Double indirect Triple IndirectCommon UNIX Flavours: 20 Common UNIX Flavours BSD: Berkeley, BSD Solaris: Sun Microsystems, Sys 5/BSD Ultrix: Digital Equipment Corporation, BSD OSF 1: Digital Equipment Corporation, BSD/sys 5 HPUX: Hewlett-Packard, Sys 5 AIX: IBM, Sys 5 / BSD IRIX: Silicon Graphics, Sys 5 GNU/Linux: GNU, BSD/PosixTypes of UNIX Users: 21 Types of UNIX Users Broad classification of users root (most privileged) Non-root (less privileged) Group UNIX allows user IDs to be grouped A single user ID can be member of multiple groups Differentiating users w.r.to file access Owner Group OthersWorking With UNIX: 22 Working With UNIX User logs in with a valid user ID User logs out to terminate the login sessionSummary: 23 Summary In this module, you have learned about: The functions of OS The History of Unix The features of UNIX The Unix Architecture Process management CPU Scheduling Memory management File managementModule 2: UNIX Commands: 24 Module 2: UNIX Commands In this module, you will learn to: Use the basic Unix commands pwd date who ls man Use “man” pagesSimple Commands: 25 Simple Commands pwd Displays the current working directory. date Displays the current date and timeSimple Commands (Contd.).: 26 Simple Commands (Contd.). who Displays the names of all the users who have currently logged in who am i Displays the name of the current user.Meta Characters: 27 Meta Characters * Match with one or more characters or none $ls –l *.c file* abc.c , filepro ? Match any Single character $ ls –l file? filea [..] Match with any single character with in the bracket $ls –l file[abc] filea, fileb , filec ; Command seperator $cat filea; date displays the content of filea and displays the current date and time | Pipe two commands $ cat filea|wc -l Prints the number of lines of filea () Group commands, used when the output of the command group has to be redirected $(echo “***x.c***”;cat x.c)>out Redirects the content of x.c with a heading ***x.c*** to the file outListing the Directory Contents: 28 Listing the Directory Contents ls Syntax : ls [options] [file….] options: -l list in long format -a list all files including those beginning with a dot -i list inode no of file in first column -s reports disk blocks occupied by file -R recursively list all sub directories -F mark type of each file -C display files in columnsListing the Directory Contents (Contd.).: 29 Listing the Directory Contents (Contd.). $ ls –l total 6 -rwxr-xr-x 1 user1 projA 12373 Dec 15 14:45 a.out drwxr-xr-x 2 user2 projD 4096 Dec 22 14:00 awkpro -rw-r--r-- 1 user1 projA 12831 Dec 12 13:59 c -rw------- 1 user1 projA 61440 Dec 15 11:16 core -rw-r--r-- 1 user3 projC 255 Dec 20 14:29 cs File type File access permissions Link count Group id Date & time of modification User id File size in bytes File nameGetting Help on Commands: 30 Getting Help on Commands The Unix manual, usually called man pages, is available on-line to explain the usage of the Unix system and commands. Syntax: man [options] command_name Common Options -k keyword list command synopsis line for all keyword matches -M path path to man pages -a show all matching man pages (SVR4) info command_name - help for commands help -–command_name – gives command synatxSummary: 31 Summary In this module, you have learned to: use the basic Unix commands like pwd date who ls man use “man” pagesModule 3: Files and Directories: 32 Module 3: Files and Directories In this module, you will learn to: set file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid, linking files, stickybit create and edit files using the vi editorFile Access Permissions: 33 File Access Permissions Refers to the permissions associated with a file with respect to the following: Permission Levels User (owner) (u) Group (wheel, staff, daemon, etc.) (g) World (guest, anonymous and all other users) (o) Permission Settings Read (r) Write (w) Execute (x)File Access Permissions (Contd.).: 34 File Access Permissions (Contd.). No read permission does not allow the user to: List the contents of directory Remove the directory No Write permission does not allow the user to : copy files to the directory remove files from the directory rename files in the directory make a subdirectory remove a subdirectory from the directory move files to, and from the directoryFile Access Permissions (Contd.).: 35 File Access Permissions (Contd.). No execute permission does not allow the user to: display the contents of a directory file from within the directory change to the directory display a file in the directory copy a file to, or from the directoryChanging Permissions - chmod: 36 Changing Permissions - chmod chmod u+x file_name Syntax: chmod <category> <operation> <permission> <filename(s)> or chmod <octal number> filename Octal Number 4 - for read 2 - for write 1 - for execution $ chmod 744 xyz this sets read, write and execute permissions for owner, read permission for group and othersDirectory Creation: 37 Directory Creation Command Syntax mkdir [OPTION] DIRECTORY $ mkdir <path>/<directory> $ mkdir –m <directory> $ mkdir –p <directory1>/<directory2>/<directory3> Example: $ mkdir project1 This creates a directory project1 under current directory. Note: Write and execute permissions are needed for the directory in which user wants to create a directory.Directory Removal: 38 Directory Removal rmdir command removes directory Syntax rmdir <directory name> Example Removes project1 directory in the current directory rmdir project1 Remove multiple directories rmdir pos1 pos2 Remove the directory recursively rmdir –p dir1/dir2/dir3 rmdir removes a directory if it is empty and is not the current directory.Command - cd: 39 Command - cd cd command is used to change the directory: cd - take to the home directory cd .. - takes to the parent directory cd / - takes to the root directoryFile – related Commands: 40 File – related Commands File Operation Command Copying a file cp Moving a file mv Removing a file rm Displaying a file and concatenating files catCommand - cp: 41 Command - cp Used to copy files across directories Syntax: cp <source file> <new file name> Example: cp file1 file2Command – cp (Contd.).: 42 Command – cp (Contd.). Options to cp -p Copies the file and preserves the following attributes owner id group id permissions last modification time -r recursive copy; copy subdirectories under the directory if any -i interactive; prompts for confirmation before overwriting the target file, if it already existsCommand – mv: 43 Command – mv Used to move a file, or rename a file. Preserves the following details owner id group id permissions Last modification time -f suppresses all prompting (forces overwriting of target) -i prompts before overwriting destination fileCommand - rm: 44 Command - rm Used to remove a file Syntax : rm file(s) -f suppresses all prompting -I prompts before deleting destination file -r will recursively remove the file from a directory (can be used to delete a directory along with the content) Caution: Use “i” option along with “r” to get notified on deletionCommand – chown & chgrp : 45 Command – chown & chgrp $ ls –l -rwxr-xr-x 1 user1 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro $chown user2 a.out $ls –l -rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro $ chgrp training awkpro $ls –l -rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 training 4096 Dec 24 11:56 awkproCommand - umask: 46 Command - umask umask value is used to set the default permission of a file and directory while creating. umask command is used to see the default mask for the file permission. Default umask value will be set in the system environment file like /etc/profile umask 022 will set a mask of 022 for the current session The file permission after setting this umask value will be 644 And the directory permission will be 755Command - ln: 47 Command - ln Linking files: Hard Link (in the same filesystem) $ ln /usr/bin/clear /usr/bin/cls Hard link uses the same inode number Soft Link (in different filesystems also used to link directories) $ ln –s /usr/bin/clear /home/user1/clsSpecial Permission Bits: 48 Special Permission Bits Set user ID (SUID) This means that if the SUID bit is set for any application then your user ID would be set as that of the owner of application/file rather than the current user, while running that application “set user ID” bit can be set in one of the two ways: chmod u+s <filename> chmod 4755 <filename> The leftmost octal number 4 indicates “set user ID” bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.Special Permission Bits (Contd.).: 49 Special Permission Bits (Contd.). Set group id (SGID) Just like SUID, setting the SGID bit for a file sets your group ID to the file's group while the file is executing “set group ID” bit can be set in one of the two ways: chmod g+s <filename> chmod 2755 <filename> The leftmost octal number 2 indicates “set group ID” bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.Special Permission Bits (Contd.).: 50 Special Permission Bits (Contd.). Sticky bit (SVTX) Typically set to a directory that is shareable Any user can create a file in such sharable directory Only owner of the file or super user (root) can remove a file from the directory “sticky” bit can be set in one of the two ways: chmod +t <directoryname> chmod 1555 <directoryname> The leftmost octal number 1 indicates “sticky” bit to be set, other octal digits indicate regular file permissions.Vi Editor: 51 Vi Editor vi is a visual editor used to create and edit text files. A screen-oriented text editor Included with most UNIX system distributions Command driven Categories of commands include Cursor movement Editing commands Search and replace commands The vi editor is invoked by the following command: $ vi filenameNavigation: 52 NavigationEditing Commands: 53 Editing Commands Text insertion / replacement i - inserts text to the left of the cursor a - inserts text to the right of the cursor I - inserts text at the beginning of the line A - appends text at end of the line o - opens line below O - opens line above R - replaces text from cursor to right s - replaces a single character with any number of characters S - replaces entire lineEditing Commands (Contd.).: 54 Editing Commands (Contd.). Deletion x - to delete character at cursor position 3x - to delete 3 characters at cursor position dw - to delete word 2dw - to delete 2 word dd - to delete a line 2dd - to delete 2 linesEditing Commands (Contd.).: 55 Editing Commands (Contd.). Yanking Y - copy line into buffer 3Y - copy 3 lines into buffer p - copy buffer below cursor P - copy buffer above cursor Save and quit :w - to save :w! - to name a file (:w! filename -> save as) :x - save and quit :q - cancel changes :q! - cancel and quitSearch & Replace Commands: 56 Search & Replace Commands The following commands are applicable for vi editor in Linux: /pat searches for the pattern pat and places cursor where pattern occurs. / repeat last search :%s/old/new/g to change every occurrence in the whole file. :#,#s/old/new/g where #,# are replaced with the numbers of the two lines.Summary: 57 Summary In this module, you have learned how to: use file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid , linking the files, stickybit create and edit files using the vi editorModule 4: UNIX Utilities: 58 Module 4: UNIX Utilities In this module, you will learn how to: use the Unix utilities such as echo, touch, more, file, wc, find employ redirection operators use filters such as sort, grep, cut, head, tail, tr, and paste use communication commands ftp use backup commands tar and zip/gzipcat: 59 cat cat command takes the input from the keyboard, and sends the output to the monitor. We can redirect the input and output using the redirection operators. $ cat > file1 Type the content here press <ctrl d> $ cat file1 Displays the content of the file $cat >> file1 This will append standard input to the content of file1.touch: 60 touch touch is used to change the time stamp of the file Syntax: touch [options] file Options: -a to change the access time -m to change the modification time -c no create if not exists touch <file> will change the time of change of the file if the file exists. If the file does not exist, it will create a file of zero byte size.echo & read: 61 echo & read echo command is used to print output to the screen echo “This is an example” This is an example x=10 echo $x 10 read command allows to read input from user and assign it to the variable specified. read xGeneral Purpose Utilities: 62 General Purpose Utilities more Allows user to view one page-full of information at a time. file Used to display the type of the file tty Prints the terminal’s nameGeneral Purpose Utilities (Contd.).: 63 General Purpose Utilities (Contd.). wc A filter used to count the number of lines, words, and characters in a disk file or from the standard input. -l - displays the number of lines -w - displays the number of words -c - displays the number of charactersfind: 64 find Lets user to search set of files and directories based on various criteria Syntax: find [path...] [expression] [path] where to search [expression] What type of file to search (specified with –type option) What action to be applied (–exec, –print, etc.) Name of the files (specified as part of –name option, enclosed in “ ” ) Example find . –name “*.c” -print lists all files with .c extension from the current dir & its subdirectoriesfind (Contd.).: 65 find (Contd.). Finding files on the basis of file size – size [+ –]n[bc] n represents size in bytes (c) or blocks (b) of 512 bytes find . –size 1000c lists all files that are exactly 1000 bytes in size find . –size +1000c lists all files that are more than 1000 bytes in size find . –size –1000c lists all files that are less than 1000 bytes in sizefind (Contd.).: 66 find (Contd.). Finding files on the basis of access time (atime) or modified time (mtime) – atime [+-]n – mtime [+-]n n represents number of days ( actually 24 * n hours) find . –atime 2 lists files accessed exactly 2 days ago find . –atime +2 lists files accessed more than 2 days ago find / –mtime –2 lists files modified less than 2 days agofind (Contd.).: 67 find (Contd.). Applying a command on files matching the criteria with –exec and –ok options – exec command {} \; command is command to be applied on the matching files (does not prompt user) find . -name “*.dat” –exec ls –l {} \; Long listing of all files with .dat extension in the current and its subdirectories -ok command {} \; Functionality is similar to –exec, but prompts user before applying the command on the file matching the criteria.Standard Files: 68 Standard Files Standard Input file Keyboard, file descriptor is 0 Standard Output file Monitor, file descriptor is 1 Standard Error file Monitor, file descriptor is 2I/O Redirection: 69 I/O Redirection < file redirect standard input from file > file redirect standard output to file 2> file redirect standard error to file 2>&1 merge standard error with standard output $ cat > abc $ ls –l > outfile $ cat xyz abc > outfile 2> errfile $ cat xyz abc > outfile 2>&1Filters: 70 Filters Filters are programs that takes its input from the standard input file, process it, and sends it to the standard output file. Commonly used filter commands sort grep cut head tail pastesort: 71 sort Sorts the contents of the given file based on the first char of each line. -n numeric sort (comparison made according to strings numeric value) -r reverse sort -t specify delimiter for fields +num specify sorting field numbers +num [-num] to specify the rangegrep: 72 grep grep -Global Regular Expression Printer is used for searching regular expressions Syntax grep <options> <pattern> <filename(s)>grep Options: 73 grep Options -c displays count of the number of occurrences -n displays line numbers along with the lines -v displays all lines except lines matching pattern -i Ignores case for matchingPatterns: 74 Patterns * - matches 0 or more characters [^pqr] - Matches a single character which is not p, q or r ^pqr -Matches pqr at the beginning of the line pqr$ -Matches pqr at the end of the line “.” - Matches any one character \ - ignores the special meaning. grep “New\[abc\]” filenameFilter Command - head: 75 Filter Command - head Displays the first n lines of the file $ head -3 file1Filter Command - tail: 76 Filter Command - tail Displays the last n lines of a file $ tail -3 file1 Can also specify the line number from which the data has to be displayed till the end of file. $ tail +5 file1Filter Command - tr: 77 Filter Command - tr tr - translate filter used to translate a given set of characters Example : tr [a-z] [A-Z] < filename This converts standard input read from lower case to upper case. option -s can be used to squeeze the repeated characters.Filter Command – tr (Contd.).: 78 Filter Command – tr (Contd.). Useful options for tr -s char Squeeze multiple contiguous occurrences of the character into single char -d char Remove the characterCommand Piping: 79 Command Piping Allows the output (only the standard output) of a command to be sent as input to another command. Multiple pipes may appear in one command line. Example: $ cat * | wc $ cat fil1 | head | wc -lFilter Command – tee: 80 Filter Command – tee tee command allows the normal output to the standard output, as well as to a file Useful to capture intermediate output of a long command pipeline for further processing, or debugging purpose. Example who | tee userlist cat - | tee file1 | wc -lFilter Command – cut: 81 Filter Command – cut Used to extract specified columns of a text Option remark -c used to extract characters -d Delimiter for fields -f Field no. Examples: $ cut -c2-5 file1 $ cut -d “|” -f2,3 file1ftp: 82 ftp ftp is a file transfer program Provides necessary user interface to the standard File Transfer Protocol Allows users to transfer files to and from a remote host Syntax $ ftp hostnameftp - commands: 83 ftp - commands ftp program supports the following commands get receive file from host mget receive multiple files from host put send file to host mput send multiple files from hostftp – commands (Contd.).: 84 ftp – commands (Contd.). ftp program supports the following commands as well ls list directory of host cd change directory on the host lcd change directory on the local machine To set transfer format ascii set to ascii mode binary set to binary modeftp – commands (Contd.).: 85 ftp – commands (Contd.). Progress indication of transfer hash command Quitting ftp session bye commandTape Archive - tar: 86 Tape Archive - tar Tar is an archiving utility to store and retrieve files from an archive, known as tarfile. Though archives are created on a tape, it is common to have them as disk files as well. tar c|t|x [vf destination] source...Tape Archive – tar (Contd.).: 87 Tape Archive – tar (Contd.). Examples: Create a new tar file containing all .dat files (assuming a.dat, b.dat and c.dat exist) $ tar –cf mytar *.datCompression Utilities: 88 Compression Utilities gzip , Usage is very similar to compress and pack utilities in Unix: gzip [-vc] filename where -v displays the compression ratio. -c sends the compressed output to standard output and leaves the original file intact. gunzip gunzip can uncompress files originally compressed with compress.Summary: 89 Summary In this module, you have learned to: use the Unix Utilities like cat, echo, touch, more, file, wc, find employ redirection operators use filters like sort, grep, cut, head, tail, tr, ftp backup commands tar and zip/gzipModule 5: Process: 90 Module 5: Process In this module, you will learn to: Use process-related commands like ps, kill, sleep Start a background process Use background and foreground-related commands like bg, fg, jobs , nice , nohupWhat is a Process?: 91 What is a Process? Process - a program in execution When program is executed, a new process is created The process is alive till the execution of the program is complete Each process is identified by a number called pidLogin shell: 92 Login shell As soon as the user logs in, a process is created which executes the login shell. Login shell is set for each login in /etc/passwd file.ps: 93 ps The ps command is used to display the characteristics of a process It fetches the pid, tty, time , and the command which has started the process. -f lists the pid of the parent process also. -u lists the processes of a given user -a lists the processes of all the users -e lists all the processes including the system processesBackground Process: 94 Background Process Enables the user to do more than one task at a time. If the command terminates with an ampersand (&), UNIX executes the command in the background Shell returns by displaying the process ID (PID) and job id of the processControlling Background Process: 95 Controlling Background Process jobs List the background process fg % <job id> Runs a process in the foreground bg %<job id> Runs a process in the backgroundThe kill Command: 96 The kill Command kill : Kills or terminates a process kill command send a signal to the process The default signal is 15 ( SIGTERM) kill -9 (SIGKILL) Terminates the process abruptly pkill : command used to kill a process by name pkill <command name> Pkill -9 <command name>Process Priority: 97 Process Priority nice Used to reduce the priority of jobs renice Used to change the priority of running processnohup Command: 98 nohup Command nohup Lets processes to continue to run even after logout The output of the command is sent to nohup.out if not redirected $ nohup command args $ nohup sort emp.lst & [1] 21356 nohup: appending output to `nohup.out'wait Command: 99 wait Command wait command can be used when a process has to wait for the output of a background process The wait command, can be used to let the shell wait for all background processes terminate. $ wait It is possible to wait for completion of one specific process as well.Summary: 100 Summary In this module, you learned to: Define a process Use process-related commands like ps, kill, sleep Start a background process Use background and foreground-related commands like bg, fg, jobs, nice and nohupModule 6: Shell Programming: 101 Module 6: Shell Programming In this module, you will learn to: Use shell variables Write scripts to process positional parameters Use “test” command Use “if” construct Use “for” loop Use “while” loop Use “case” construct Define and use functions Debug shell scriptsUnix Shell: 102 Unix Shell Bourne shell sh C shell csh Korn shell ksh Bourne again shell bash (shell distributed with linux)Command Processing: 103 Command Processing Displays the shell prompt and reads the command typed by the user. Interprets the command and classifies it as an internal (built-in), or an external command. If it is NOT a built-in command, searches for the command in the PATH-specified directories, and executes that command if it is found.Additional Shell Features: 104 Additional Shell Features Each shell, apart from the basic features, provides additional features such as: Maintaining command history ( C, korn and bash ) Renaming (aliasing) a command (C, korn, bash) Command editing (C, korn and bash) Programming language (all shells)History: 105 History Some of the UNIX shells support command history Facility to keeps track of commands that were executed Facility to rerun previously executed commands bash shell supports the following history displays the command history !! recall the last command and execute it. !num execute n th command where n is the the num specified after !alias: 106 alias alias can be used to give new name to an existing command A better name that represents a single command or a sequence of commands to be executed, often with appropriate options alias is an internal command alias newname=command $ alias l=‘ls –l’ The unalias command cancels previously defined alias.File Name Substitution: 107 File Name Substitution When the user enters a command string, the shell parses the string into following components: Command (the first part of the string, till the first space char) Command arguments (the subsequent parts of the string) For example, given the command-string “ls –l *.c”, this string contains the “ls” command and two arguments “-l” and “*.c”.File Name Substitution (Contd.).: 108 File Name Substitution (Contd.). In arguments of a command, the shell recognizes certain characters – such as *, ?, [ ], and - as special characters and expands these characters into a filename list before executing the command. To see how this works, enter following commands while in /bin directory $ ls a* $ ls ??Configuration Scripts: 109 Configuration Scripts bsh ksh csh bash System Profile /etc/profile /etc/profile /etc/login /etc/profile User profile ~.profile ~.profile ~.login ~.bash_profile Script file ~.shrc ~.kshrc ~.cshrc ~.bashrc ~ is used to represent the home directory of the userScripting: 110 Scripting Allows Defining and referencing variables Logic control structures such as if, for, while, case Input and outputShell Variables: 111 Shell Variables A variable is a name associated with a data value, and it offers a symbolic way to represent and manipulate data variables in the shell. They are classified as follows: user-defined variables environment variables predefined variables value assigned to the variable can then be referred to by preceding the variable name with a $ sign.Shell Variables (Contd.).: 112 Shell Variables (Contd.). The shell provides the facility to define normal, and environment variables. A normal variable can be only used in the shell where it is defined. An environment variable can be used in the shell where it is defined, plus any child shells invoked from that shell.Using Normal Variables: 113 Using Normal Variables To define a normal variable, use the following syntax: variable_name=value Examples: x=10 textline_1=‘This line was entered by $USER’ textline_2=“This line was entered by $USER” allusers=`who` usercount=`who | wc –l`Using Normal Variables (Contd.).: 114 Using Normal Variables (Contd.). Once variables are defined, one can use the echo command to display the value of each variable: echo $x echo $textline_1 echo $textline_2 echo $allusers echo $usercountUsing Environment Variables: 115 Using Environment Variables To define an environment variable, use following syntax: variable_name=value export variable_name Examples: $ x=10; export x $ allusers=`who` ; export allusersBuilt-in Environment Variables: 116 Built-in Environment Variables PATH BASH_ENV HOME PWD SHELL TERM MAIL USER LOGNAME PS1 PS2Sample Shell Script: 117 Sample Shell Script #! /bin/bash # # The above line has a special meaning. It must be the # first line of the script. It says that the commands in # this shell script should be executed by the bash # shell (/bin/bash). # --------------------------------------------------------------- echo “Hello $USER….” echo “Welcome to programming shell scripts..” # ---------------------------------------------------------------Executing Shell Scripts: 118 Executing Shell Scripts There are two ways of executing a shell script: By passing the shell script name as an argument to the shell. For example: sh script1.sh If the shell script is assigned execute permission, it can be executed using it’s name. For example: ./script1.shPassing Parameters to Scripts: 119 Passing Parameters to Scripts parameter can be passed to a shell script parameters are specified after the name of the shell script when invoking the script. Within the shell script, parameters are referenced using the predefined variables $1 through $9 . In case of more than 9 parameters, other parameters can be accessed by shift ing.Positional Variables: 120 Positional Variables Following are built-in variables supported $0, $1…$9 - positional arguments $* - all arguments $@ - all argumentsPassing Parameters to Scripts - example: 121 Passing Parameters to Scripts - example Consider following shell script: ----------------------script2.sh-------------------------- echo “Total parameters entered: $#” echo “First parameter is : $1” echo “The parameters are: $*” shift echo “First parameter is : $1” ------------------------------------------------------------ Execute the above script using the “script2.sh these are the parameters” command.Passing Parameters to Scripts – example (Contd.).: 122 Passing Parameters to Scripts – example (Contd.). The shell parameters are passed as strings. to pass a string containing multiple words as a single parameter, it must be enclosed within quotes. For example, $ script2.sh “this string is a single parameter”Doing Arithmetic Operations: 123 Doing Arithmetic Operations Arithmetic operations within a shell script can be performed using expr command. Example, x=10 y=5 number_1 = `expr $x + $y` number_2 = `expr $x - $y` number_3 = `expr $x / $y` number_4 = `expr $x \* $y` number_5 = `expr $x % $y `Using the test Command: 124 Using the test Command The general syntax of test command is: test <expression> The expression can be formed using a combination of shell variables and the operators supported by the test command. These operators provide facility to compare numbers, string and logical values, file types and file access modes.Using the test Command (Contd.).: 125 Using the test Command (Contd.). To compare two integers using test following operators are available: -eq (equal to) -ne (not equal to) -lt (less than) -le (less than or equal to) -gt (greater than) -ge (greater than or equal to)Using the test Command (Contd.).: 126 Using the test Command (Contd.). Please note that you can only compare two integers using test. General syntax test [expression] test integer1 operator integer2 OR [ integer1 operator integer2 ]Using the test Command (Contd.).: 127 Using the test Command (Contd.). To compare two strings using the test command, following operators are available: $string1 = $string2 (equal to, please note it is a single =) $string1 != $string2 (not equal to) $string1 (string is not NULL) -z $string1 (string is NULL and exists)Using the test Command (Contd.).: 128 Using the test Command (Contd.). The syntax for this string comparison is: test string1 operator string2 OR [ string1 operator string2 ] OR test operator string OR [ operator string ]Using the test Command (Contd.).: 129 Using the test Command (Contd.). To check a file type/access permissions using the test command, following operators are available: -s file (file is not empty and exists) -f file (Ordinary file and exists) -d file (file is a directory and exists) -r file (file is readable and exists) -w file (file is write-able and exists) -x file (file is executable and exists)Using the test Command (Contd.).: 130 Using the test Command (Contd.). To check a file type/access permissions using the test command, following operators are available: -b file (file is a block device and exists) -c file (file is a character device and exists) -p file (file is a named pipe and exists) -g file (file has sticky bit set) -u file (file has setuid bit set) -t file_des (file descriptor is standard output)Combining Conditions: 131 Combining Conditions It is possible to combine conditions by using following operators: -a (logical AND operator) -o (logical OR operator) ! (logical NOT operator)Combining Conditions (Contd.).: 132 Combining Conditions (Contd.). The syntax for this is: test expression_1 –a expression _2, OR [ expression _1 –a expression _2 ] test expression_1 –o expression _2, OR [ expression_1 –o expression_2 ] test ! expression _1 OR [ ! expression _1 ]Condition Checking in Scripts: 133 Condition Checking in Scripts Bash shell provides the if command to test if a condition is true. The general format of this command is: if condition then command fi The condition is typically formed using the test command.Example: 134 Example # to check if the current directory is the same as your home directory curdir=`pwd` if test “$curdir” != “$HOME” then echo your home dir is not the same as your present working directory else echo $HOME is your current directory fiChecking Multiple Conditions: 135 Checking Multiple Conditions The complex form of if statement is as follows: if condition_1 then command elif condition_2 then command else command fiUsing for Loop: 136 Using for Loop The Bash shell provides a for loop. The syntax of this loop is: for variable in list do command … command done Example: for i in 1 2 3 4 5 do echo -n $i \* $i = " " echo `expr $i \* $i ` doneExample: 137 Example ----------------------script.sh-------------------------- #! /bin/sh usernames=`who | cut –d “ “ –f1` echo “Total users logged in = $#usernames” # for user in ${usernames} do echo $user done ------------------------------------------------------------Using while Loop: 138 Using while Loop The Bash shell provides a while loop. The syntax of this loop is: while condition do command … command doneExample: 139 Example Shell script checks for a blank/non blank string eg: read nam while [ -z “$nam” ] do read nam done echo the string is $nam The above piece of code keeps accepting string variable nam until it is non zero in length.Example (Contd.).: 140 Example (Contd.). Shell script to compute factorial of a given number #!/bin/bash n=$1 if [ $n -eq 0 ]; then fact=0 else fact=1 while [ $n –ne 0 ] do fact=`expr $fact \* $n` n=`expr $n – 1` done fi echo $factThe case Statement: 141 The case Statement -THE CASE STATEMENT case value in pattern1) command command;; pattern2) command command;; patternn) command;; esacExample: 142 Example #!/bin/bash echo enter a choice read choice case $choice in 1) echo enter 2 nos read num1 read num2 res=`expr $num1 + $num2` echo result is $res;; 2) who;; *) echo invalid choice;; esacExample (Contd.).: 143 Example (Contd.). #!/bin/bash read number case $number 1) echo 1st;; 2) echo 2nd;; 3) echo 3rd;; *) echo ${number}th;; esacFunctions: 144 Functions Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular“ command. Shell functions are executed in the current shell context; no new process is created to interpret them. Functions are declared using this syntax: [ function ] name () { command-list; }Functions (Contd.).: 145 Functions (Contd.). Shell functions can accept arguments Arguments are passed in the same way as given to commands Functions refer to arguments using $1, $2 etc., similar to the way shell scripts refer to command line argumentsFunctions (Contd.).: 146 Functions (Contd.). Function to convert standard input into upper case toupper() { tr a-z A-Z } This function can be used as $ cat abc | toupperDebugging Shell Scripts: 147 Debugging Shell Scripts Two options help in debugging shell scripts “-v” (verbose) option: causes shell to print the lines of the script as they are read. $ bash –v script-file “-x” (verbose) option: prints commands and their arguments as they are executed. $ bash –x script-fileProgramming in C vs. Shell: 148 Programming in C vs. Shell Comparison between: A solution in C A shell solution written like a C program A proper “shell/unix” solution e.g: The problem is to count the no of lines in a file ( the file is called the_file)Programming in C vs. Shell (Contd.).: 149 Programming in C vs. Shell (Contd.). A Solution in C #include <stdio.h> void main(void) { int lcount=0; FILE *infile; char line[500]; infile=fopen("the_file","r"); while(!feof(infile)) { fgets(line,500,infile); lcount ++; } printf("no of lines is %d\n",lcount); } A Shell Solution count=0 while read line do count=`expr $count + 1` done < the_file echo Number of lines is $count Solution using existing commands $ wc –l the_fileSummary: 150 Summary In this module, you have learned to: Use Shell variables Write scripts to process positional parameters Use “test” command Use “if” construct Use “for” loop Use “while” loop Use “case” construct Define and use functions Debug shell scriptsSuggested References : 151 Suggested References Abrahams , W.P. & Larson, R. B. (1992). UNIX for the impatient. Addison-Wesley Publishing Company, 1992. Kernighan, W. B. & Pike, R. (1984). The UNIX programming environment . Prentice Hall, 1984. A classic for serious folks. UNIX in a nutshell: a desktop quick reference for system V & Solaris 2.0. O'Reilly & Associates, Inc., 1992. A handy reference for SysV and Solaris 2. UNIX in a nutshell for BSD 4.3: a desktop quick reference for Berkeley. O'Reilly & Associates, Inc., 1990. A handy reference for BSD. Websites: http://knet.wipro.comSlide 152: 152Module 7: Lab Exercises: 153 Module 7: Lab ExercisesSlide 154: 154Slide 155: 155Slide 156: 156Slide 157: 157Slide 158: 158 You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Fundamentals_Unix shashank249 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 248 Category: Education License: All Rights Reserved Like it (3) Dislike it (0) Added: March 13, 2011 This Presentation is Public Favorites: 2 Presentation Description No description available. Comments Posting comment... By: prabauma (3 month(s) ago) hai i am praba I think this Presentation is useful for my teaching.please if u can u send this ppt to my mail prabachandru@gmail.com Saving..... Post Reply Close By: wuqiang97 (2 month(s) ago) Fundamentals of UNIX: Copyright © 2007, Wipro Limited, Bangalore Fundamentals of UNIX Version Saving..... Edit Comment Close Premium member Presentation Transcript Fundamentals of UNIX: Copyright © 2007, Wipro Limited, Bangalore Fundamentals of UNIX Version 2.0Fundamentals of UNIX: 2 Fundamentals of UNIX Day 1 UNIX Operating System UNIX Commands Files and Directories UNIX Utilities Process Day 2 Shell Programming Lab ExercisesModule 1: UNIX Operating System: 3 Module 1: UNIX Operating System In this module, you will learn about: The functions of OS The history of Unix The features of UNIX The Unix architecture Process management CPU scheduling Memory management File managementOperating System (OS): 4 Operating System (OS) OS can be defined as an organized collection of software consisting of procedures for operating a computer. OS provides an environment for execution of programs. OS acts as an interface between the user and the hardware of the computer system.Operating System (Contd.).: 5 Operating System (Contd.). Operating system interacts with user in two ways: Operating system commands Enables user to interact directly with the operating system. Operating system calls Provides an interface to a running program and the operating system. System calls in UNIX are written in C.History of UNIX: 6 History of UNIX Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s Two versions of UNIX that emerged are AT&T Unix and BSD Unix In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4) Two of the main standards mainly in use are POSIX (Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium developed vendor-neutral Xwindow System.What is Linux?: 7 What is Linux? An open-source UNIX like operating system Initially created by Linus Torvalds for PC architecture Ports exist for Alpha and Sparc processors Developer community world-wide contribute to its enhancement and growthFeatures of UNIX: 8 Features of UNIX Multi-user, multitasking, timesharing Portability Modularity File structure Security Strong networking support & advanced graphicsLayered Architecture: 9 Layered Architecture hardware kernel shell ld as comp cp vi ed grep wc date a.out who sh sort ls banner ...UNIX System Architecture: 10 UNIX System Architecture Unix system follows a layered approach. It has four layers The innermost layer is the hardware layer In the second layer, the kernel is placed The utilities and other application programs form the third layer Fourth layer is the one with which the user actually interacts.Kernel: 11 Kernel Kernel is that part of the OS which directly makes interface with the hardware system. Actions: Provides mechanism for creating and deleting processes Provides processor scheduling, memory, and I/O management Provides inter-process communication.The Shell: 12 The Shell A utility program that comes with the UNIX system. Features of Shell are: Interactive Processing Background Processing I/O Redirection Pipes Shell Scripts Shell Variables Programming ConstructsProcess Management: 13 Process Management A process is a program in execution. Several processes can be executed simultaneously in a UNIX system. A process is generally created using the “fork( )” system call. The process that invokes the “ fork( )” system call is the parent process, and the newly created process is called the child process.CPU Scheduling: 14 CPU Scheduling Unix uses round-robin scheduling to support its multi-user and time-sharing feature. Round-robin fashion of scheduling is considered to be the oldest, simplest and widely used algorithm. Every process is given a time slice (10-100 millisec.)Memory Management: 15 Memory Management Virtual memory Swap area Demand pagingFile Management: 16 File Management UNIX uses a hierarchical file system with “/” as its root. Every non-leaf node of the tree is called as a directory file. Every leaf node can either be a file, or an empty directoryFile System: 17 File System dev bin tmp home etc lib usr console lp0 sh ls passwd inittab bin user1 user2 var src spoolFile System (Contd.).: 18 File System (Contd.). File system is the structure in which files are stored on disk File in UNIX is sequence of bytes organized in the form of blocks The size of each block is 512 bytes (depends on architecture) Block size can be decided while creating the file system structureFile System Structure: 19 File System Structure Boot Block Super Block Inode Block Data Block Type of the file Link counter Uid, gid, size Date and time of Creation Date and time of access Date and time of modification : : Address of datablock Address of datablock : : Address of the addr block Address of the addr block Address of the addr block Single indirect Double indirect Triple IndirectCommon UNIX Flavours: 20 Common UNIX Flavours BSD: Berkeley, BSD Solaris: Sun Microsystems, Sys 5/BSD Ultrix: Digital Equipment Corporation, BSD OSF 1: Digital Equipment Corporation, BSD/sys 5 HPUX: Hewlett-Packard, Sys 5 AIX: IBM, Sys 5 / BSD IRIX: Silicon Graphics, Sys 5 GNU/Linux: GNU, BSD/PosixTypes of UNIX Users: 21 Types of UNIX Users Broad classification of users root (most privileged) Non-root (less privileged) Group UNIX allows user IDs to be grouped A single user ID can be member of multiple groups Differentiating users w.r.to file access Owner Group OthersWorking With UNIX: 22 Working With UNIX User logs in with a valid user ID User logs out to terminate the login sessionSummary: 23 Summary In this module, you have learned about: The functions of OS The History of Unix The features of UNIX The Unix Architecture Process management CPU Scheduling Memory management File managementModule 2: UNIX Commands: 24 Module 2: UNIX Commands In this module, you will learn to: Use the basic Unix commands pwd date who ls man Use “man” pagesSimple Commands: 25 Simple Commands pwd Displays the current working directory. date Displays the current date and timeSimple Commands (Contd.).: 26 Simple Commands (Contd.). who Displays the names of all the users who have currently logged in who am i Displays the name of the current user.Meta Characters: 27 Meta Characters * Match with one or more characters or none $ls –l *.c file* abc.c , filepro ? Match any Single character $ ls –l file? filea [..] Match with any single character with in the bracket $ls –l file[abc] filea, fileb , filec ; Command seperator $cat filea; date displays the content of filea and displays the current date and time | Pipe two commands $ cat filea|wc -l Prints the number of lines of filea () Group commands, used when the output of the command group has to be redirected $(echo “***x.c***”;cat x.c)>out Redirects the content of x.c with a heading ***x.c*** to the file outListing the Directory Contents: 28 Listing the Directory Contents ls Syntax : ls [options] [file….] options: -l list in long format -a list all files including those beginning with a dot -i list inode no of file in first column -s reports disk blocks occupied by file -R recursively list all sub directories -F mark type of each file -C display files in columnsListing the Directory Contents (Contd.).: 29 Listing the Directory Contents (Contd.). $ ls –l total 6 -rwxr-xr-x 1 user1 projA 12373 Dec 15 14:45 a.out drwxr-xr-x 2 user2 projD 4096 Dec 22 14:00 awkpro -rw-r--r-- 1 user1 projA 12831 Dec 12 13:59 c -rw------- 1 user1 projA 61440 Dec 15 11:16 core -rw-r--r-- 1 user3 projC 255 Dec 20 14:29 cs File type File access permissions Link count Group id Date & time of modification User id File size in bytes File nameGetting Help on Commands: 30 Getting Help on Commands The Unix manual, usually called man pages, is available on-line to explain the usage of the Unix system and commands. Syntax: man [options] command_name Common Options -k keyword list command synopsis line for all keyword matches -M path path to man pages -a show all matching man pages (SVR4) info command_name - help for commands help -–command_name – gives command synatxSummary: 31 Summary In this module, you have learned to: use the basic Unix commands like pwd date who ls man use “man” pagesModule 3: Files and Directories: 32 Module 3: Files and Directories In this module, you will learn to: set file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid, linking files, stickybit create and edit files using the vi editorFile Access Permissions: 33 File Access Permissions Refers to the permissions associated with a file with respect to the following: Permission Levels User (owner) (u) Group (wheel, staff, daemon, etc.) (g) World (guest, anonymous and all other users) (o) Permission Settings Read (r) Write (w) Execute (x)File Access Permissions (Contd.).: 34 File Access Permissions (Contd.). No read permission does not allow the user to: List the contents of directory Remove the directory No Write permission does not allow the user to : copy files to the directory remove files from the directory rename files in the directory make a subdirectory remove a subdirectory from the directory move files to, and from the directoryFile Access Permissions (Contd.).: 35 File Access Permissions (Contd.). No execute permission does not allow the user to: display the contents of a directory file from within the directory change to the directory display a file in the directory copy a file to, or from the directoryChanging Permissions - chmod: 36 Changing Permissions - chmod chmod u+x file_name Syntax: chmod <category> <operation> <permission> <filename(s)> or chmod <octal number> filename Octal Number 4 - for read 2 - for write 1 - for execution $ chmod 744 xyz this sets read, write and execute permissions for owner, read permission for group and othersDirectory Creation: 37 Directory Creation Command Syntax mkdir [OPTION] DIRECTORY $ mkdir <path>/<directory> $ mkdir –m <directory> $ mkdir –p <directory1>/<directory2>/<directory3> Example: $ mkdir project1 This creates a directory project1 under current directory. Note: Write and execute permissions are needed for the directory in which user wants to create a directory.Directory Removal: 38 Directory Removal rmdir command removes directory Syntax rmdir <directory name> Example Removes project1 directory in the current directory rmdir project1 Remove multiple directories rmdir pos1 pos2 Remove the directory recursively rmdir –p dir1/dir2/dir3 rmdir removes a directory if it is empty and is not the current directory.Command - cd: 39 Command - cd cd command is used to change the directory: cd - take to the home directory cd .. - takes to the parent directory cd / - takes to the root directoryFile – related Commands: 40 File – related Commands File Operation Command Copying a file cp Moving a file mv Removing a file rm Displaying a file and concatenating files catCommand - cp: 41 Command - cp Used to copy files across directories Syntax: cp <source file> <new file name> Example: cp file1 file2Command – cp (Contd.).: 42 Command – cp (Contd.). Options to cp -p Copies the file and preserves the following attributes owner id group id permissions last modification time -r recursive copy; copy subdirectories under the directory if any -i interactive; prompts for confirmation before overwriting the target file, if it already existsCommand – mv: 43 Command – mv Used to move a file, or rename a file. Preserves the following details owner id group id permissions Last modification time -f suppresses all prompting (forces overwriting of target) -i prompts before overwriting destination fileCommand - rm: 44 Command - rm Used to remove a file Syntax : rm file(s) -f suppresses all prompting -I prompts before deleting destination file -r will recursively remove the file from a directory (can be used to delete a directory along with the content) Caution: Use “i” option along with “r” to get notified on deletionCommand – chown & chgrp : 45 Command – chown & chgrp $ ls –l -rwxr-xr-x 1 user1 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro $chown user2 a.out $ls –l -rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro $ chgrp training awkpro $ls –l -rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out -rwxr-xr-x 3 user1 training 4096 Dec 24 11:56 awkproCommand - umask: 46 Command - umask umask value is used to set the default permission of a file and directory while creating. umask command is used to see the default mask for the file permission. Default umask value will be set in the system environment file like /etc/profile umask 022 will set a mask of 022 for the current session The file permission after setting this umask value will be 644 And the directory permission will be 755Command - ln: 47 Command - ln Linking files: Hard Link (in the same filesystem) $ ln /usr/bin/clear /usr/bin/cls Hard link uses the same inode number Soft Link (in different filesystems also used to link directories) $ ln –s /usr/bin/clear /home/user1/clsSpecial Permission Bits: 48 Special Permission Bits Set user ID (SUID) This means that if the SUID bit is set for any application then your user ID would be set as that of the owner of application/file rather than the current user, while running that application “set user ID” bit can be set in one of the two ways: chmod u+s <filename> chmod 4755 <filename> The leftmost octal number 4 indicates “set user ID” bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.Special Permission Bits (Contd.).: 49 Special Permission Bits (Contd.). Set group id (SGID) Just like SUID, setting the SGID bit for a file sets your group ID to the file's group while the file is executing “set group ID” bit can be set in one of the two ways: chmod g+s <filename> chmod 2755 <filename> The leftmost octal number 2 indicates “set group ID” bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.Special Permission Bits (Contd.).: 50 Special Permission Bits (Contd.). Sticky bit (SVTX) Typically set to a directory that is shareable Any user can create a file in such sharable directory Only owner of the file or super user (root) can remove a file from the directory “sticky” bit can be set in one of the two ways: chmod +t <directoryname> chmod 1555 <directoryname> The leftmost octal number 1 indicates “sticky” bit to be set, other octal digits indicate regular file permissions.Vi Editor: 51 Vi Editor vi is a visual editor used to create and edit text files. A screen-oriented text editor Included with most UNIX system distributions Command driven Categories of commands include Cursor movement Editing commands Search and replace commands The vi editor is invoked by the following command: $ vi filenameNavigation: 52 NavigationEditing Commands: 53 Editing Commands Text insertion / replacement i - inserts text to the left of the cursor a - inserts text to the right of the cursor I - inserts text at the beginning of the line A - appends text at end of the line o - opens line below O - opens line above R - replaces text from cursor to right s - replaces a single character with any number of characters S - replaces entire lineEditing Commands (Contd.).: 54 Editing Commands (Contd.). Deletion x - to delete character at cursor position 3x - to delete 3 characters at cursor position dw - to delete word 2dw - to delete 2 word dd - to delete a line 2dd - to delete 2 linesEditing Commands (Contd.).: 55 Editing Commands (Contd.). Yanking Y - copy line into buffer 3Y - copy 3 lines into buffer p - copy buffer below cursor P - copy buffer above cursor Save and quit :w - to save :w! - to name a file (:w! filename -> save as) :x - save and quit :q - cancel changes :q! - cancel and quitSearch & Replace Commands: 56 Search & Replace Commands The following commands are applicable for vi editor in Linux: /pat searches for the pattern pat and places cursor where pattern occurs. / repeat last search :%s/old/new/g to change every occurrence in the whole file. :#,#s/old/new/g where #,# are replaced with the numbers of the two lines.Summary: 57 Summary In this module, you have learned how to: use file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid , linking the files, stickybit create and edit files using the vi editorModule 4: UNIX Utilities: 58 Module 4: UNIX Utilities In this module, you will learn how to: use the Unix utilities such as echo, touch, more, file, wc, find employ redirection operators use filters such as sort, grep, cut, head, tail, tr, and paste use communication commands ftp use backup commands tar and zip/gzipcat: 59 cat cat command takes the input from the keyboard, and sends the output to the monitor. We can redirect the input and output using the redirection operators. $ cat > file1 Type the content here press <ctrl d> $ cat file1 Displays the content of the file $cat >> file1 This will append standard input to the content of file1.touch: 60 touch touch is used to change the time stamp of the file Syntax: touch [options] file Options: -a to change the access time -m to change the modification time -c no create if not exists touch <file> will change the time of change of the file if the file exists. If the file does not exist, it will create a file of zero byte size.echo & read: 61 echo & read echo command is used to print output to the screen echo “This is an example” This is an example x=10 echo $x 10 read command allows to read input from user and assign it to the variable specified. read xGeneral Purpose Utilities: 62 General Purpose Utilities more Allows user to view one page-full of information at a time. file Used to display the type of the file tty Prints the terminal’s nameGeneral Purpose Utilities (Contd.).: 63 General Purpose Utilities (Contd.). wc A filter used to count the number of lines, words, and characters in a disk file or from the standard input. -l - displays the number of lines -w - displays the number of words -c - displays the number of charactersfind: 64 find Lets user to search set of files and directories based on various criteria Syntax: find [path...] [expression] [path] where to search [expression] What type of file to search (specified with –type option) What action to be applied (–exec, –print, etc.) Name of the files (specified as part of –name option, enclosed in “ ” ) Example find . –name “*.c” -print lists all files with .c extension from the current dir & its subdirectoriesfind (Contd.).: 65 find (Contd.). Finding files on the basis of file size – size [+ –]n[bc] n represents size in bytes (c) or blocks (b) of 512 bytes find . –size 1000c lists all files that are exactly 1000 bytes in size find . –size +1000c lists all files that are more than 1000 bytes in size find . –size –1000c lists all files that are less than 1000 bytes in sizefind (Contd.).: 66 find (Contd.). Finding files on the basis of access time (atime) or modified time (mtime) – atime [+-]n – mtime [+-]n n represents number of days ( actually 24 * n hours) find . –atime 2 lists files accessed exactly 2 days ago find . –atime +2 lists files accessed more than 2 days ago find / –mtime –2 lists files modified less than 2 days agofind (Contd.).: 67 find (Contd.). Applying a command on files matching the criteria with –exec and –ok options – exec command {} \; command is command to be applied on the matching files (does not prompt user) find . -name “*.dat” –exec ls –l {} \; Long listing of all files with .dat extension in the current and its subdirectories -ok command {} \; Functionality is similar to –exec, but prompts user before applying the command on the file matching the criteria.Standard Files: 68 Standard Files Standard Input file Keyboard, file descriptor is 0 Standard Output file Monitor, file descriptor is 1 Standard Error file Monitor, file descriptor is 2I/O Redirection: 69 I/O Redirection < file redirect standard input from file > file redirect standard output to file 2> file redirect standard error to file 2>&1 merge standard error with standard output $ cat > abc $ ls –l > outfile $ cat xyz abc > outfile 2> errfile $ cat xyz abc > outfile 2>&1Filters: 70 Filters Filters are programs that takes its input from the standard input file, process it, and sends it to the standard output file. Commonly used filter commands sort grep cut head tail pastesort: 71 sort Sorts the contents of the given file based on the first char of each line. -n numeric sort (comparison made according to strings numeric value) -r reverse sort -t specify delimiter for fields +num specify sorting field numbers +num [-num] to specify the rangegrep: 72 grep grep -Global Regular Expression Printer is used for searching regular expressions Syntax grep <options> <pattern> <filename(s)>grep Options: 73 grep Options -c displays count of the number of occurrences -n displays line numbers along with the lines -v displays all lines except lines matching pattern -i Ignores case for matchingPatterns: 74 Patterns * - matches 0 or more characters [^pqr] - Matches a single character which is not p, q or r ^pqr -Matches pqr at the beginning of the line pqr$ -Matches pqr at the end of the line “.” - Matches any one character \ - ignores the special meaning. grep “New\[abc\]” filenameFilter Command - head: 75 Filter Command - head Displays the first n lines of the file $ head -3 file1Filter Command - tail: 76 Filter Command - tail Displays the last n lines of a file $ tail -3 file1 Can also specify the line number from which the data has to be displayed till the end of file. $ tail +5 file1Filter Command - tr: 77 Filter Command - tr tr - translate filter used to translate a given set of characters Example : tr [a-z] [A-Z] < filename This converts standard input read from lower case to upper case. option -s can be used to squeeze the repeated characters.Filter Command – tr (Contd.).: 78 Filter Command – tr (Contd.). Useful options for tr -s char Squeeze multiple contiguous occurrences of the character into single char -d char Remove the characterCommand Piping: 79 Command Piping Allows the output (only the standard output) of a command to be sent as input to another command. Multiple pipes may appear in one command line. Example: $ cat * | wc $ cat fil1 | head | wc -lFilter Command – tee: 80 Filter Command – tee tee command allows the normal output to the standard output, as well as to a file Useful to capture intermediate output of a long command pipeline for further processing, or debugging purpose. Example who | tee userlist cat - | tee file1 | wc -lFilter Command – cut: 81 Filter Command – cut Used to extract specified columns of a text Option remark -c used to extract characters -d Delimiter for fields -f Field no. Examples: $ cut -c2-5 file1 $ cut -d “|” -f2,3 file1ftp: 82 ftp ftp is a file transfer program Provides necessary user interface to the standard File Transfer Protocol Allows users to transfer files to and from a remote host Syntax $ ftp hostnameftp - commands: 83 ftp - commands ftp program supports the following commands get receive file from host mget receive multiple files from host put send file to host mput send multiple files from hostftp – commands (Contd.).: 84 ftp – commands (Contd.). ftp program supports the following commands as well ls list directory of host cd change directory on the host lcd change directory on the local machine To set transfer format ascii set to ascii mode binary set to binary modeftp – commands (Contd.).: 85 ftp – commands (Contd.). Progress indication of transfer hash command Quitting ftp session bye commandTape Archive - tar: 86 Tape Archive - tar Tar is an archiving utility to store and retrieve files from an archive, known as tarfile. Though archives are created on a tape, it is common to have them as disk files as well. tar c|t|x [vf destination] source...Tape Archive – tar (Contd.).: 87 Tape Archive – tar (Contd.). Examples: Create a new tar file containing all .dat files (assuming a.dat, b.dat and c.dat exist) $ tar –cf mytar *.datCompression Utilities: 88 Compression Utilities gzip , Usage is very similar to compress and pack utilities in Unix: gzip [-vc] filename where -v displays the compression ratio. -c sends the compressed output to standard output and leaves the original file intact. gunzip gunzip can uncompress files originally compressed with compress.Summary: 89 Summary In this module, you have learned to: use the Unix Utilities like cat, echo, touch, more, file, wc, find employ redirection operators use filters like sort, grep, cut, head, tail, tr, ftp backup commands tar and zip/gzipModule 5: Process: 90 Module 5: Process In this module, you will learn to: Use process-related commands like ps, kill, sleep Start a background process Use background and foreground-related commands like bg, fg, jobs , nice , nohupWhat is a Process?: 91 What is a Process? Process - a program in execution When program is executed, a new process is created The process is alive till the execution of the program is complete Each process is identified by a number called pidLogin shell: 92 Login shell As soon as the user logs in, a process is created which executes the login shell. Login shell is set for each login in /etc/passwd file.ps: 93 ps The ps command is used to display the characteristics of a process It fetches the pid, tty, time , and the command which has started the process. -f lists the pid of the parent process also. -u lists the processes of a given user -a lists the processes of all the users -e lists all the processes including the system processesBackground Process: 94 Background Process Enables the user to do more than one task at a time. If the command terminates with an ampersand (&), UNIX executes the command in the background Shell returns by displaying the process ID (PID) and job id of the processControlling Background Process: 95 Controlling Background Process jobs List the background process fg % <job id> Runs a process in the foreground bg %<job id> Runs a process in the backgroundThe kill Command: 96 The kill Command kill : Kills or terminates a process kill command send a signal to the process The default signal is 15 ( SIGTERM) kill -9 (SIGKILL) Terminates the process abruptly pkill : command used to kill a process by name pkill <command name> Pkill -9 <command name>Process Priority: 97 Process Priority nice Used to reduce the priority of jobs renice Used to change the priority of running processnohup Command: 98 nohup Command nohup Lets processes to continue to run even after logout The output of the command is sent to nohup.out if not redirected $ nohup command args $ nohup sort emp.lst & [1] 21356 nohup: appending output to `nohup.out'wait Command: 99 wait Command wait command can be used when a process has to wait for the output of a background process The wait command, can be used to let the shell wait for all background processes terminate. $ wait It is possible to wait for completion of one specific process as well.Summary: 100 Summary In this module, you learned to: Define a process Use process-related commands like ps, kill, sleep Start a background process Use background and foreground-related commands like bg, fg, jobs, nice and nohupModule 6: Shell Programming: 101 Module 6: Shell Programming In this module, you will learn to: Use shell variables Write scripts to process positional parameters Use “test” command Use “if” construct Use “for” loop Use “while” loop Use “case” construct Define and use functions Debug shell scriptsUnix Shell: 102 Unix Shell Bourne shell sh C shell csh Korn shell ksh Bourne again shell bash (shell distributed with linux)Command Processing: 103 Command Processing Displays the shell prompt and reads the command typed by the user. Interprets the command and classifies it as an internal (built-in), or an external command. If it is NOT a built-in command, searches for the command in the PATH-specified directories, and executes that command if it is found.Additional Shell Features: 104 Additional Shell Features Each shell, apart from the basic features, provides additional features such as: Maintaining command history ( C, korn and bash ) Renaming (aliasing) a command (C, korn, bash) Command editing (C, korn and bash) Programming language (all shells)History: 105 History Some of the UNIX shells support command history Facility to keeps track of commands that were executed Facility to rerun previously executed commands bash shell supports the following history displays the command history !! recall the last command and execute it. !num execute n th command where n is the the num specified after !alias: 106 alias alias can be used to give new name to an existing command A better name that represents a single command or a sequence of commands to be executed, often with appropriate options alias is an internal command alias newname=command $ alias l=‘ls –l’ The unalias command cancels previously defined alias.File Name Substitution: 107 File Name Substitution When the user enters a command string, the shell parses the string into following components: Command (the first part of the string, till the first space char) Command arguments (the subsequent parts of the string) For example, given the command-string “ls –l *.c”, this string contains the “ls” command and two arguments “-l” and “*.c”.File Name Substitution (Contd.).: 108 File Name Substitution (Contd.). In arguments of a command, the shell recognizes certain characters – such as *, ?, [ ], and - as special characters and expands these characters into a filename list before executing the command. To see how this works, enter following commands while in /bin directory $ ls a* $ ls ??Configuration Scripts: 109 Configuration Scripts bsh ksh csh bash System Profile /etc/profile /etc/profile /etc/login /etc/profile User profile ~.profile ~.profile ~.login ~.bash_profile Script file ~.shrc ~.kshrc ~.cshrc ~.bashrc ~ is used to represent the home directory of the userScripting: 110 Scripting Allows Defining and referencing variables Logic control structures such as if, for, while, case Input and outputShell Variables: 111 Shell Variables A variable is a name associated with a data value, and it offers a symbolic way to represent and manipulate data variables in the shell. They are classified as follows: user-defined variables environment variables predefined variables value assigned to the variable can then be referred to by preceding the variable name with a $ sign.Shell Variables (Contd.).: 112 Shell Variables (Contd.). The shell provides the facility to define normal, and environment variables. A normal variable can be only used in the shell where it is defined. An environment variable can be used in the shell where it is defined, plus any child shells invoked from that shell.Using Normal Variables: 113 Using Normal Variables To define a normal variable, use the following syntax: variable_name=value Examples: x=10 textline_1=‘This line was entered by $USER’ textline_2=“This line was entered by $USER” allusers=`who` usercount=`who | wc –l`Using Normal Variables (Contd.).: 114 Using Normal Variables (Contd.). Once variables are defined, one can use the echo command to display the value of each variable: echo $x echo $textline_1 echo $textline_2 echo $allusers echo $usercountUsing Environment Variables: 115 Using Environment Variables To define an environment variable, use following syntax: variable_name=value export variable_name Examples: $ x=10; export x $ allusers=`who` ; export allusersBuilt-in Environment Variables: 116 Built-in Environment Variables PATH BASH_ENV HOME PWD SHELL TERM MAIL USER LOGNAME PS1 PS2Sample Shell Script: 117 Sample Shell Script #! /bin/bash # # The above line has a special meaning. It must be the # first line of the script. It says that the commands in # this shell script should be executed by the bash # shell (/bin/bash). # --------------------------------------------------------------- echo “Hello $USER….” echo “Welcome to programming shell scripts..” # ---------------------------------------------------------------Executing Shell Scripts: 118 Executing Shell Scripts There are two ways of executing a shell script: By passing the shell script name as an argument to the shell. For example: sh script1.sh If the shell script is assigned execute permission, it can be executed using it’s name. For example: ./script1.shPassing Parameters to Scripts: 119 Passing Parameters to Scripts parameter can be passed to a shell script parameters are specified after the name of the shell script when invoking the script. Within the shell script, parameters are referenced using the predefined variables $1 through $9 . In case of more than 9 parameters, other parameters can be accessed by shift ing.Positional Variables: 120 Positional Variables Following are built-in variables supported $0, $1…$9 - positional arguments $* - all arguments $@ - all argumentsPassing Parameters to Scripts - example: 121 Passing Parameters to Scripts - example Consider following shell script: ----------------------script2.sh-------------------------- echo “Total parameters entered: $#” echo “First parameter is : $1” echo “The parameters are: $*” shift echo “First parameter is : $1” ------------------------------------------------------------ Execute the above script using the “script2.sh these are the parameters” command.Passing Parameters to Scripts – example (Contd.).: 122 Passing Parameters to Scripts – example (Contd.). The shell parameters are passed as strings. to pass a string containing multiple words as a single parameter, it must be enclosed within quotes. For example, $ script2.sh “this string is a single parameter”Doing Arithmetic Operations: 123 Doing Arithmetic Operations Arithmetic operations within a shell script can be performed using expr command. Example, x=10 y=5 number_1 = `expr $x + $y` number_2 = `expr $x - $y` number_3 = `expr $x / $y` number_4 = `expr $x \* $y` number_5 = `expr $x % $y `Using the test Command: 124 Using the test Command The general syntax of test command is: test <expression> The expression can be formed using a combination of shell variables and the operators supported by the test command. These operators provide facility to compare numbers, string and logical values, file types and file access modes.Using the test Command (Contd.).: 125 Using the test Command (Contd.). To compare two integers using test following operators are available: -eq (equal to) -ne (not equal to) -lt (less than) -le (less than or equal to) -gt (greater than) -ge (greater than or equal to)Using the test Command (Contd.).: 126 Using the test Command (Contd.). Please note that you can only compare two integers using test. General syntax test [expression] test integer1 operator integer2 OR [ integer1 operator integer2 ]Using the test Command (Contd.).: 127 Using the test Command (Contd.). To compare two strings using the test command, following operators are available: $string1 = $string2 (equal to, please note it is a single =) $string1 != $string2 (not equal to) $string1 (string is not NULL) -z $string1 (string is NULL and exists)Using the test Command (Contd.).: 128 Using the test Command (Contd.). The syntax for this string comparison is: test string1 operator string2 OR [ string1 operator string2 ] OR test operator string OR [ operator string ]Using the test Command (Contd.).: 129 Using the test Command (Contd.). To check a file type/access permissions using the test command, following operators are available: -s file (file is not empty and exists) -f file (Ordinary file and exists) -d file (file is a directory and exists) -r file (file is readable and exists) -w file (file is write-able and exists) -x file (file is executable and exists)Using the test Command (Contd.).: 130 Using the test Command (Contd.). To check a file type/access permissions using the test command, following operators are available: -b file (file is a block device and exists) -c file (file is a character device and exists) -p file (file is a named pipe and exists) -g file (file has sticky bit set) -u file (file has setuid bit set) -t file_des (file descriptor is standard output)Combining Conditions: 131 Combining Conditions It is possible to combine conditions by using following operators: -a (logical AND operator) -o (logical OR operator) ! (logical NOT operator)Combining Conditions (Contd.).: 132 Combining Conditions (Contd.). The syntax for this is: test expression_1 –a expression _2, OR [ expression _1 –a expression _2 ] test expression_1 –o expression _2, OR [ expression_1 –o expression_2 ] test ! expression _1 OR [ ! expression _1 ]Condition Checking in Scripts: 133 Condition Checking in Scripts Bash shell provides the if command to test if a condition is true. The general format of this command is: if condition then command fi The condition is typically formed using the test command.Example: 134 Example # to check if the current directory is the same as your home directory curdir=`pwd` if test “$curdir” != “$HOME” then echo your home dir is not the same as your present working directory else echo $HOME is your current directory fiChecking Multiple Conditions: 135 Checking Multiple Conditions The complex form of if statement is as follows: if condition_1 then command elif condition_2 then command else command fiUsing for Loop: 136 Using for Loop The Bash shell provides a for loop. The syntax of this loop is: for variable in list do command … command done Example: for i in 1 2 3 4 5 do echo -n $i \* $i = " " echo `expr $i \* $i ` doneExample: 137 Example ----------------------script.sh-------------------------- #! /bin/sh usernames=`who | cut –d “ “ –f1` echo “Total users logged in = $#usernames” # for user in ${usernames} do echo $user done ------------------------------------------------------------Using while Loop: 138 Using while Loop The Bash shell provides a while loop. The syntax of this loop is: while condition do command … command doneExample: 139 Example Shell script checks for a blank/non blank string eg: read nam while [ -z “$nam” ] do read nam done echo the string is $nam The above piece of code keeps accepting string variable nam until it is non zero in length.Example (Contd.).: 140 Example (Contd.). Shell script to compute factorial of a given number #!/bin/bash n=$1 if [ $n -eq 0 ]; then fact=0 else fact=1 while [ $n –ne 0 ] do fact=`expr $fact \* $n` n=`expr $n – 1` done fi echo $factThe case Statement: 141 The case Statement -THE CASE STATEMENT case value in pattern1) command command;; pattern2) command command;; patternn) command;; esacExample: 142 Example #!/bin/bash echo enter a choice read choice case $choice in 1) echo enter 2 nos read num1 read num2 res=`expr $num1 + $num2` echo result is $res;; 2) who;; *) echo invalid choice;; esacExample (Contd.).: 143 Example (Contd.). #!/bin/bash read number case $number 1) echo 1st;; 2) echo 2nd;; 3) echo 3rd;; *) echo ${number}th;; esacFunctions: 144 Functions Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular“ command. Shell functions are executed in the current shell context; no new process is created to interpret them. Functions are declared using this syntax: [ function ] name () { command-list; }Functions (Contd.).: 145 Functions (Contd.). Shell functions can accept arguments Arguments are passed in the same way as given to commands Functions refer to arguments using $1, $2 etc., similar to the way shell scripts refer to command line argumentsFunctions (Contd.).: 146 Functions (Contd.). Function to convert standard input into upper case toupper() { tr a-z A-Z } This function can be used as $ cat abc | toupperDebugging Shell Scripts: 147 Debugging Shell Scripts Two options help in debugging shell scripts “-v” (verbose) option: causes shell to print the lines of the script as they are read. $ bash –v script-file “-x” (verbose) option: prints commands and their arguments as they are executed. $ bash –x script-fileProgramming in C vs. Shell: 148 Programming in C vs. Shell Comparison between: A solution in C A shell solution written like a C program A proper “shell/unix” solution e.g: The problem is to count the no of lines in a file ( the file is called the_file)Programming in C vs. Shell (Contd.).: 149 Programming in C vs. Shell (Contd.). A Solution in C #include <stdio.h> void main(void) { int lcount=0; FILE *infile; char line[500]; infile=fopen("the_file","r"); while(!feof(infile)) { fgets(line,500,infile); lcount ++; } printf("no of lines is %d\n",lcount); } A Shell Solution count=0 while read line do count=`expr $count + 1` done < the_file echo Number of lines is $count Solution using existing commands $ wc –l the_fileSummary: 150 Summary In this module, you have learned to: Use Shell variables Write scripts to process positional parameters Use “test” command Use “if” construct Use “for” loop Use “while” loop Use “case” construct Define and use functions Debug shell scriptsSuggested References : 151 Suggested References Abrahams , W.P. & Larson, R. B. (1992). UNIX for the impatient. Addison-Wesley Publishing Company, 1992. Kernighan, W. B. & Pike, R. (1984). The UNIX programming environment . Prentice Hall, 1984. A classic for serious folks. UNIX in a nutshell: a desktop quick reference for system V & Solaris 2.0. O'Reilly & Associates, Inc., 1992. A handy reference for SysV and Solaris 2. UNIX in a nutshell for BSD 4.3: a desktop quick reference for Berkeley. O'Reilly & Associates, Inc., 1990. A handy reference for BSD. Websites: http://knet.wipro.comSlide 152: 152Module 7: Lab Exercises: 153 Module 7: Lab ExercisesSlide 154: 154Slide 155: 155Slide 156: 156Slide 157: 157Slide 158: 158