Top Banner
03/17/22 Introduction to the Shell – Session 2 1 .0.0.3 – Introduction to the Shell 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
24

2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

Dec 29, 2015

Download

Documents

Michael Smith
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 1

2.0.0.3 – Introduction to the Shell

2.0.0.3.2Introduction 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

Page 2: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 2

2.0.0.3 – Introduction to the Shell

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

Page 3: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 3

2.0.0.3 – Introduction to the Shell

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

Group usersEvery user at GSC belongs to the group “users”, and this is the default group ownership for any file created.

GroupUser

Page 4: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 4

2.0.0.3 – Introduction to the Shell

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

Page 5: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 5

2.0.0.3 – Introduction to the Shell

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

Page 6: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 6

2.0.0.3 – Introduction to the Shell

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

Page 7: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 7

2.0.0.3 – Introduction to the Shell

Changing Permissions: chmod

· If you own a file, change permissions using

· Specify:

chmod

chmod [ugoa][+-=][rwx]

Who to modify permissions for:u = userg = groupo = 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 = readw = writex = execute

Other options:-R apply recursively

Page 8: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 8

2.0.0.3 – Introduction to the Shell

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-xchmod a+x

Page 9: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 9

2.0.0.3 – Introduction to the Shell

Changing Permissions: chmod and umask

· You can also use a set of numbers to set permissions

eg. chmod 644 = -rw-r--r--

· 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)

Page 10: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 10

2.0.0.3 – Introduction to the Shell

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

Page 11: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 11

2.0.0.3 – Introduction to the Shell

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

Page 12: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 12

2.0.0.3 – Introduction to the Shell

Setting the Prompt: PS1

· 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

· Get colourful! To specify colour, bracket the colour code between a “\e[“ (or “\033[“) and an “m”

30 Black31 Red32 Green33 Yellow34 Dark blue35 Purple36 Light blue37 White

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.

Page 13: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 13

2.0.0.3 – Introduction to the Shell

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 “./”

Page 14: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 14

2.0.0.3 – Introduction to the Shell

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!

Page 15: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 15

2.0.0.3 – Introduction to the Shell

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….

Page 16: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 16

2.0.0.3 – Introduction to the Shell

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

Page 17: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 17

2.0.0.3 – Introduction to the Shell

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

Page 18: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 18

2.0.0.3 – Introduction to the Shell

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

Page 19: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 19

2.0.0.3 – Introduction to the Shell

Redirecting Input, Output, and Error: >, <, >>, 2>, $>

· 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 >>

/dev/nullA 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.

stdout

append stdout

stderr

Page 20: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 20

2.0.0.3 – Introduction to the Shell

Example: Perl output

· It is good practice to separate “real” output from error messages when running Perl scripts

Page 21: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 21

2.0.0.3 – Introduction to the Shell

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)

Page 22: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 22

2.0.0.3 – Introduction to the Shell

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 helpMany 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.

Page 23: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 23

2.0.0.3 – Introduction to the Shell

2.0.0.3.2Introduction 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

Page 24: 2.0.0.3 – Introduction to the Shell 10/1/2015 Introduction to the Shell – Session 2 1 2.0.0.3.2 Introduction to the Shell – Session 2 · Permissions · Users.

04/19/23 Introduction to the Shell – Session 2 24

2.0.0.3 – Introduction to the Shell

2.0.0.3.2Further 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