1 / 24

2.0.0.3.2 Introduction to the Shell – Session 2

2.0.0.3.2 Introduction to the Shell – Session 2. Permissions Users and groups Who can do what and how to control it Customizing your environment Changing the shell prompt Aliases Environment variables Saving customizations Pipes and redirection Controlling input and output

ailani
Télécharger la présentation

2.0.0.3.2 Introduction to the Shell – Session 2

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. 2.0.0.3.2 Introduction to the Shell – Session 2 • Permissions • Users and groups • Who can do what and how to control it • Customizing your environment • Changing the shell prompt • Aliases • Environment variables • Saving customizations • Pipes and redirection • Controlling input and output • Running a series of commands • Running programs on the command line Introduction to the Shell – Session 2

  2. UNIX Users and Groups • Every file/directory belongs to a user and a group • People logged on to UNIX systems are users • Check who you are logged on as with • Change who you are logged on as with whoami su Introduction to the Shell – Session 2

  3. UNIX Users and Groups • Check which groups you belong to with • See what user and group a file belongs to with ls –l or stat (as in 2.0.0.3.1) groups User Group Group users Every user at GSC belongs to the group “users”, and this is the default group ownership for any file created. Introduction to the Shell – Session 2

  4. UNIX Users and Groups: chgrp • Users (except root) cannot change which user owns a file • Change the group that a file (or directory) belongs to with -R: change recursively (all subdirectories and files) chgrp Introduction to the Shell – Session 2

  5. The Permissions System: reading, writing, and executing • Permissions refer to the type of access that users have to files • Allows files to be private, public, read-only, etc. • There are three types of access possible: • Read (ability to see the contents of a file, or list the contents of a directory) • Write (ability to modify or delete a file, or create files in a directory) • Execute (ability to run the file as a program – eg. Perl scripts need to be executable – or enter a directory) • Permissions are set individually for: • User • Group • All users Introduction to the Shell – Session 2

  6. The Permissions System: reading, writing, and executing • Read, write, and execute permissions for user, group, and all users are specified as “r”, “w” and “x” (if the permission is given) or “-” (if the permission is denied) For all users For group For user Introduction to the Shell – Session 2

  7. Changing Permissions: chmod • If you own a file, change permissions using • Specify: chmod chmod [ugoa][+-=][rwx] Other options: -R apply recursively Who to modify permissions for: u = user g = group o = other (all users) a = all (u,g, and o) Whether to give or take permission: + = give - = take away = = these are the only permissions What permission to modify: r = read w = write x = execute Introduction to the Shell – Session 2

  8. Changing Permissions: chmod examples • Make a file private (only you can see it): -rw------- chmod og-rw or chmod og= • Make a file public (everyone can modify it): -rw-rw-rw- chmod a+rw • Make a directory private (only you can view it’s contents): drwx------ chmod og-rwx or chmod og= • Make a directory usable by anyone (anyone can add files to it): drwxrwxrwx chmod a+rwx • Make a file executable: -rwxr-xr-x chmod a+x Introduction to the Shell – Session 2

  9. Changing Permissions: chmod and umask • You can also use a set of numbers to set permissions • Specify default permissions for all newly created files with umask http://www.cs.ualberta.ca/doc/UNIX/novice-unix-doc/permissions.html eg. umask 022 = -rw-r--r– (files) -rwxr-xr-x (directories) eg. chmod 644 = -rw-r--r-- Introduction to the Shell – Session 2

  10. Other file attributes: chattr • In addition to specifying permissions, other file attributes can be set with • eg. set the file so it cannot be deleted, so it’s access time is not updated, or so that it is stored in compressed form. chattr Introduction to the Shell – Session 2

  11. Customizing your Environment: Environment variables • Environment variables store information about how your shell should look and behave, where to find things, particular program settings, etc. They are used by programs running in the shell. • See what environment variables are currently set with • Set environment variables with <VARIABLE>=“new value” • Delete environment variables with printenv unset Introduction to the Shell – Session 2

  12. Setting the Prompt: PS1 • Get colourful! To specify colour, bracket the colour code between a “\e[“ (or “\033[“) and an “m” 30 Black 31 Red 32 Green 33 Yellow 34 Dark blue 35 Purple 36 Light blue 37 White • The environment variable PS1 stores what is displayed at the prompt • Use special characters: \d date \t and \@ time \h hostname \u username \w current directory Word wrapping isn’t working! When modifying colours in PS1, surround the color specifications with “\[“ and “\]” to tell the shell that text doesn’t take up any space on the prompt. Introduction to the Shell – Session 2

  13. Specifying Where to Look for Programs: PATH • The PATH variable stores a list of directories where the shell and other processes should look for programs • Each entry is separated by “:” • Start your path with “.” to run scripts without specifying “./” Introduction to the Shell – Session 2

  14. Specifying Where to Look for Directories: CDPATH • CDPATH is like PATH, but instead of specifying where to look for programs, it specifies where to look for directories • Be careful, it can be dangerous…. always put “.” first! Introduction to the Shell – Session 2

  15. Other Environment Variables • HISTSIZE specifies the number of commands to keep in the history • PRINTER specifies the printer name • PWD stores the current directory (used by “pwd”) • OLDPWD stores the previous directory (used by “cd –”) • LS_COLORS specifies colours to display different file/directory types as (used by “ls –color”) • Various programs require other environment variables • BLAST • SRS • Java • Perl • Etc…. Introduction to the Shell – Session 2

  16. Defining Commands: aliases • An alias is a short form of a command that can save on typing • View and assign aliases with • To use the “normal” command, not the alias, use \<command> eg. “\ls” lists with no colour etc. alias Introduction to the Shell – Session 2

  17. Customizing your Environment: Configuration at startup • Customizations are lost when you logout • To save them, put them in one of your configuration files: • .bash_profile (/home/<user>/.bash_profile) is run every time you log on • .bashrc (/home/<user>/.bashrc) is run every time you open a shell other than the login shell • “export” the variables so you can see and access them Oops! Careful…. If you do something wrong (like make text white on white) it can be difficult to fix • Test everything before putting it in .bashrc or .bash_profile • Keep an extra terminal window that was opened BEFORE you made the changes, so you can use that terminal to fix mistakes Introduction to the Shell – Session 2

  18. Input, Output, and Error • Data can be transferred (eg. between commands) in the shell using three streams: • Standard input (STDIN in Perl): Input to a process, by default from the keyboard • Standard output (STDOUT in Perl): Output from a process, by default to the terminal window • Standard error (STDERR in Perl): Another stream of output from a process, by default to the terminal window • eg. • cat takes a file or text from the terminal as standard input, writes the file contents to standard output (the terminal) • Error messages from commands are written to standard error • Perl scripts by default write to standard output Introduction to the Shell – Session 2

  19. Redirecting Input, Output, and Error: >, <, >>, 2>, $> append stdout • Redirect standard input to a file with < • Redirect standard output to a file with > or 1> • Redirect standard error to a file with 2> • Redirect BOTH standard output and error to a file with $> • Redirect standard output to append to a file with >> stdout stderr /dev/null A useful “file” to redirect to is /dev/null: anything redirected here just disappears. Good for programs that produce a lot of verbose messages that you don’t need to see or keep. Introduction to the Shell – Session 2

  20. Example: Perl output • It is good practice to separate “real” output from error messages when running Perl scripts Introduction to the Shell – Session 2

  21. Running a Series of Commands: pipes • To put the standard output of one command directly into the standard input of another, use a pipe “|” (on the keyboard above the “\”) • You can do this as many times as you like on one line – especially useful for command line tools like grep, cut, sort, wc, etc. (see session 2.0.0.3.3) Introduction to the Shell – Session 2

  22. Running Programs on the Command Line • With correctly set environment variables and ability to redirect output, you will find it much easier to run command-line programs (eg. BLAST) • To check where a program is installed, use • If the command is in your PATH, the shell will tell you where it is • Important to check when there is more than one version of the program installed (eg. Perl at GSC!) which Getting help Many programs will give short “help” messages when run without any parameters (eg. perl, blastall). Otherwise, try option –h or --help. To check the program version, try --version. Introduction to the Shell – Session 2

  23. 2.0.0.3.2 Introduction to the Shell – Session 2 • Now you know…. • How to see and set permissions • How to customize your environment, and save customizations for future sessions • How to control input and output, and run a series of commands • Next you’ll learn…. • Job control • Command line goodies (really useful tools) • Command line Perl Introduction to the Shell – Session 2

  24. 2.0.0.3.2 Further Readings • Permissions • Linux Cookbook Ch. 7 • Learning the UNIX Operating System Ch. 3.3 • Customizing the shell • Linux Cookbook Ch. 4.6 • Learning the UNIX Operating System Ch. 3.6 • PS1: http://www-106.ibm.com/developerworks/linux/library/l-tip-prompt/ • Redirecting input and output • Linux Cookbook Ch. 4.2 • Learning the UNIX Operating System Ch. 5 Introduction to the Shell – Session 2

More Related