1 / 39

Unix Comp-145

Unix Comp-145. Lecture 4: The Shell Command Structure Source: S. Das, “Your Unix: The ultimate Guide”, 2 nd Edition, McGraw Hill, 2006. Objectives. Review of File Access Permissions / chmod Understand the shell as a command processor Learn about wild cards ( *, ?, [] ).

afi
Download Presentation

Unix Comp-145

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unix Comp-145 Lecture 4: The Shell Command Structure Source: S. Das, “Your Unix: The ultimate Guide”, 2nd Edition, McGraw Hill, 2006 BROOKDALE COMMUNITY COLLEGE

  2. Objectives • Review of File Access Permissions/chmod • Understand the shell as a command processor • Learn about wild cards ( *, ?, [] ). • Understand how the UNIX pipe facility works. • Learn about I/O redirection BROOKDALE COMMUNITY COLLEGE

  3. Review of File Permissions • What file permissions result from the following commands? • chmod a=rwx <FILE> • Set read, write & execute access for all • chmod a=rw<FILE> • Set read, & write access for all • chmodo+x<FILE> • Add executable access for others • chmod u-rwx<FILE> • Remove all access for owner • chmod 777 <FILE> • Set read write & execute access for all • chmod 750 <FILE> • Set read, write & execute for user, read execute for group, no access others BROOKDALE COMMUNITY COLLEGE

  4. Shell as a command processor • Steps: • Issues prompt and waits for user input • Scans for meta-characters in command line and expands abbreviations • Passes command line to Kernel for processing • Shell waits for command to complete, normally locks terminal • Returns to step 1 BROOKDALE COMMUNITY COLLEGE

  5. Determining the Command to Run • When a command is invoked with a fully qualified pathname(e.g., /bin/echo), the shell runs program (e.g., echo) found in the specified location. BROOKDALE COMMUNITY COLLEGE

  6. Pattern Matching: Wild Cards • SubSetof meta-characters used in an expression to match multiple but similar filenames (see table 7.1 of text) • Shell creates list of filenames before allowing command to run. • Expansion can be prevented by quoting and escaping. • Feature also found in find and shell’s for and case constructs. • Filenames must not contain wild-card characters. BROOKDALE COMMUNITY COLLEGE

  7. Wild-cards Set for ls/mv/cp/rm, etc. * Any character or sequence of characters ? Any single character [ ] Any of the characters specified in the Brackets ! Not — Identifies a range of characters of numbers e.g., ls *[ab] ls *[5-7] BROOKDALE COMMUNITY COLLEGE

  8. Wild-cards Set (Cont’d) • *Any number of characters including none • ls *.lstLists all files with extension .lst. • ?A single character • rm ??*Removes all files comprising at least 2 characters. • [ch] A single character that is either a c or h • cp *.[ch]cprogsCopies all files with .c or .h extension. • [!ch]A single character that is not a c or h • rm *[!a-zA-Z]*Removes files not containing at least one letter. • ls .??*Lists all filenames beginning with a dot andcomprising at least two more characters. BROOKDALE COMMUNITY COLLEGE

  9. The Shell Meta-characters • Wild-card characters like the *, ?, etc. • Redirection characters like >, >>,<, etc. • The pipe character, | • Command substitution characters (` `) • echo The date today is `date` • Echo “There are `ls | wc –l` files in the current directory” • The$as a variable prefix, e.g., $PATH BROOKDALE COMMUNITY COLLEGE

  10. The Shell Metacharacters(Cont’d) cat chap*Shell expands * to match all filenames in the current directory that begin with chap. date > fooShell sees the > first, opens the file fooas a new file and connects the date output to it. date >> fooShell sees the >>first, opens the file foo in the current directory and appends the date output to it. who | sort Shell understands the strings on either side of the | as two separate programs and connects them. ls`cat foo`Shell first runs catand supplies the output as arguments tols. echo $HOMEEvaluates $HOME as a variable before running echo. BROOKDALE COMMUNITY COLLEGE

  11. Redirection Characters • The pipe character = Special operator redirects the output of one command into input for another command. • ls|wc –l • The “>” character = Redirects command output from standard e.g., the screen, to a file • ls|wc –l > wordCountOfFiles BROOKDALE COMMUNITY COLLEGE

  12. I/O Redirection Summary • The shell input redirection: • Standard input (the keyboard) - Default • Use < to change the default input to be a file • Use | to change the default input to be a command • A variable, e.g., $PRINTER • Shell redirection of output: • Use > to create it or >> to append to existing file • Use | to send output to another command BROOKDALE COMMUNITY COLLEGE

  13. Escaping (Using a \ before a character) • Reverses usual meaning of metacharacter following it. • (rm \*removes a file named *.) • Can also protect itself. (echo \\prints a \.) • Protects space and [Enter]. (cd My\ Documentswill work.) • Inconvenient to use when command line contains too many metacharactersthat need to be escaped. • Principle also used by commands in their expressions. • (grep “\.” foolooks for a dot in foo.) BROOKDALE COMMUNITY COLLEGE

  14. Quoting • Protects mostmetacharacters from interpretation by the shell, i.e., indicates to be treated literally. • echo “*” prints an asterisk (*) • More convenient than escaping when protecting a group of metacharacters. • Quoted string understood as a single argument by shell and C programs. (a.outfoo “My Documents” has 2 arguments and not 3.) • Double quotes and single quotes are not equivalent. (echo “$SHELL”not the same as echo ‘$SHELL’) • Quoting doesn’t protect the backward slash (\),escaping is also required. BROOKDALE COMMUNITY COLLEGE

  15. Single Quotes or Double Quotes? • Single quotes protect all characters except \ • echo ‘\’won’t work • Double quotes protect all characters except the \, $and` • echo “$”doesn’t print a $ • Single quotes protect the “ • Double quotes protect the ‘ • Double quotes permit variable evaluation and command substitution. BROOKDALE COMMUNITY COLLEGE

  16. The User Mask • Reassigns default file and directory Permissions. • Default permissions before applying mask are completely insecure: • 666 for files • 777 for directories • System-wide default changed by umask (a shell built-in). • umask statement placed in a startup script (typically, /etc/profile). BROOKDALE COMMUNITY COLLEGE

  17. File Systems • System of organizing files into multiple manageable units. • Each file system has a separate directory structure with at top. • For a file to be visible, its file system must be attached to the main file system. • Two files in two file systems may have the same inode number. • Not easy to understand whether a directory structure comprises multiple file systems. BROOKDALE COMMUNITY COLLEGE

  18. Determining the Command to Run (Cont’d) • When a command is invoked without a pathname, the shell first checks whether it is an alias or built-in: • If alias or built-in, the shell runs it withoutlooking in disk. • type, whatisand which= examples of built-in commands • If not, the shell looks at the PATH variable for directories where the command may reside. • $ echo $PATH where echo=shell, $PATH= input variable BROOKDALE COMMUNITY COLLEGE

  19. File Attributes Stored in INODE • Type:Whether ordinary, directory, device, etc. • Permissions:Determines who can read, write or execute a file. • Links:Number of names a file can have. A program can be designed to behave differently depending on the name by which it is invoked. • Owner: A file is owned by a user, by default its creator. The owner can change many file attributes and set the permissions. • Group Owner: The group which owns the file. The owner by default belongs to this group. • File Size: Number of bytes of data contained. • File Time Stamps: • Date and time of last modification • Date and time of last access BROOKDALE COMMUNITY COLLEGE

  20. Directory Permissions • Read permission: Whether filenames in directory can be listed by a program (like ls). • Write permission: Whether files and directories can be created in the directory. • Execute or search permission: Whether one can pass through directory to search for filenames. • Desirable permission setting: 755 BROOKDALE COMMUNITY COLLEGE

  21. An Ownership-Permissions Problem • Assumption: romeo and julietbelong to the users group. • Ownership and Permissions of File foo and its Directory • $ who am i • romeo • $ ls -l foo • -r-x-w-r-x 1 juliet users 7017 2004-11-14 13:53 foo • $ ls -ld . • drwxr-xr-x 21 romeo users 8192 2004-11-28 11:40 . • Note: foo is owned by juliet but directory is owned by romeo. • juliet: • can’t edit foo without changing the permissions. • can change permissions (as owner) and then edit foo. • can’t delete foo (directory write-protected for group). • romeo: • can edit or delete foo. • can’t change permissions of foo. • can’t display or copy foo. BROOKDALE COMMUNITY COLLEGE

  22. The INODE Revisited • System of organizing file attributes separately from content. • Identified by inode number but inode doesn’t contain this number. • Inodenumber displayed by ls -i • Bothinode and directoryentriesare looked up by inode number. • Possible to consume all inodeseven when there is adequate disk space. BROOKDALE COMMUNITY COLLEGE

  23. Making a Program Behave Differently • Using arguments and options. • Manipulating a configuration file read by a program on startup. • Modifiable text files by hand or via Shell Scripts • Using different names for the same file. BROOKDALE COMMUNITY COLLEGE

  24. Links or Hard Links • Mechanism by which a file is allowed to have multiple names. • Linked filenames share inode but have separate directory entries. • Each link increments link count in inode by 1 and adds an entry to the directory. BROOKDALE COMMUNITY COLLEGE

  25. Links or Hard Links (Cont’d) • File considered to be deleted and inode freed only when link count drops to 0. • Linked filenames equivalent in all respects. • The ln command creates links and rm removes them • Inode # reveals whether filenames are links or copies. • Two filenames in the same directory and having the same inode number MUST be linked to each other. BROOKDALE COMMUNITY COLLEGE

  26. Links or Hard Links (Cont’d) • QUESTION: • If two files in two different directories have the same inode number, have a link count of 2, and also have identical content, are they linked filenames? • ANSWER: • Not necessarily; they can be two separate files on two file systems having the same inode number by coincidence. They may also have their own separate hard links. It’s difficult to create this situation, but it’s not impossible. • But links can be in different directories, so how does one locate them? Use the -inum option of the find command that can track down a file by its inode number. BROOKDALE COMMUNITY COLLEGE

  27. Advantages of Hard Links • Backup: Prevention from accidental deletion. • Allows the same file to be executed as two similar but separate programs. • Takes care of old programs that accesses a file whose name or location has changed. BROOKDALE COMMUNITY COLLEGE

  28. Limitations of Hard Links • Can’t link directories. • Can’t link across file systems. BROOKDALE COMMUNITY COLLEGE

  29. Symbolic Links or Symlinks • Separate file type and having its own inode. • Contains the pathname of another file or directory. • Can link across file systems. • Link and file pointed to are not equivalent. • Pathname may be stored either in inode or in a separate file. BROOKDALE COMMUNITY COLLEGE

  30. Understanding Ownership and Group Ownership • Entryforromeo in /etc/passwd: • romeo:x:1003:101::/export/home/romeo:/usr/bin/ksh • Entryforromeo’sgroup (101) in /etc/group: • users::101: • A file has a UID (name and number). • Both UID parameters maintained in /etc/passwd (1st and 3rd field). • A file has a GID (name and number). • Both GID parameters maintained in /etc/group (1st and 3rd field). • The numeric GID also maintained in /etc/passwd. BROOKDALE COMMUNITY COLLEGE

  31. Ownership in SVR4 and BSD AttributeSVR4 BSD OwnershipCan be transferred Only by superuser but can be removed. Group Ownership Can be transferred Only to another group to removedbut can be modified. BROOKDALE COMMUNITY COLLEGE

  32. Backup Slides BROOKDALE COMMUNITY COLLEGE

  33. File Access Permissions -rwxrwxrwx - rwx rwx rwx File Type Owner Group World • = file • d = directory BROOKDALE COMMUNITY COLLEGE

  34. Owner The creator of the file Group A set of users grouped together The world Every other account not in the group Owner, Group, The World BROOKDALE COMMUNITY COLLEGE

  35. Read File can be read, but not modified Write Permission is granted to modify the file Execute Run directly as if the file is a program All programs should be executable (/bin) Read, Write, Execute BROOKDALE COMMUNITY COLLEGE

  36. Read Users can get a listing of that directory Write Users can create and remove files in that directory Execute Users can examine files in that directory What Permissions Mean On A Directory BROOKDALE COMMUNITY COLLEGE

  37. chmod<SETTINGS> <FILE> Changing Permissions With chmod u = user g = group o = other a = all • + (add) • (remove) • = (set) r = read w = write x = execute chmod a-x testFile BROOKDALE COMMUNITY COLLEGE

  38. Set read and write access for all? chmod a=rw<FILE> Add executable access for others? chmod o+x<FILE> Remove all access for owner? chmod u-rwx<FILE> Set read, write & execute access for all? chmod a=rwx <FILE> 777 permissions Octal Representation of -rwxrwxrwx Three binary digits or bits corresponds to one octal digit: Read = 4, Write = 2 Execute =1 i.e., rwx = 4+2+1 = 7 chmod 750 <file> same as chmod u=rwx,g=rx,o= <file> More Examples BROOKDALE COMMUNITY COLLEGE

  39. Most Unix hackers don’t use this form They prefer the more direct approach Set permissions for owner, group, and others all with one number Unfortunately, this approach requires a little bit of information AdvancedchmodUsage BROOKDALE COMMUNITY COLLEGE

More Related