------------------------ Week 6 Notes for NET2003 ------------------------ -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Keep up on your readings (Course Outline: average 5 hours/week homework) The Search Path environment variable $PATH : What is a shell for? how does it find commands? - it uses the search $PATH environment variable (inherited by children) The $PATH variable contains the list of directories (files don't work) searched by the shell when it tries to find a command name - PATH must be a list of directories (show file names) - directory pathnames are separated by colons (":") - only command names without slashes are looked up in $PATH - a command name containing a slash is NOT looked for in $PATH $ ./a.out # uses the a.out in "."; does not search $PATH $ /bin/ls # executes /bin/ls; does not search $PATH $ ../foo # executes the foo in the parent dir; no $PATH used - slashes in a command name mean no PATH lookup; direct try to execute path Examples: $ PATH=/bin:/usr/bin ; ls - shell tries first directory in PATH: /bin - shell looks for /bin/ls - this exists, so it is executed $ PATH=/bin:/usr/bin ; gcc - shell tries first directory in PATH: /bin - shell looks for /bin/gcc - this is not found - shell tries next directory in PATH: /usr/bin - shell looks for /usr/bin/gcc - this exists, so it is executed $ PATH=/bin:/usr/bin ; nosuch - shell tries first directory in PATH: /bin - shell looks for /bin/nosuch - this is not found - shell tries next directory in PATH: /usr/bin - shell looks for /usr/bin/nosuch - this is not found - shell issues message "nosuch: Command not found" $ PATH=/bin:/usr/bin ; /usr/games/fortune - command name contains slashes, $PATH is NOT used - shell executes /usr/games/fortune directly - $PATH is NOT used $ PATH=/bin:/usr/bin ; ./foo - command name contains slashes, $PATH is NOT used - shell executes ./foo directly - $PATH is NOT used Your shell has some built in commands, e.g. echo, cd, umask, pwd - built-in commands are not looked up in $PATH $ PATH=/junkxxxx ; pwd # works because pwd is built-in to shell More examples: $ PATH=/bin:/usr/bin ; ls # works $ PATH=/junkxxxx ; ls # fails (PATH must contain directories) $ PATH=/bin/ls ; ls # fails (PATH must contain directories) New commands: which - tell which $PATH directory contains a command whereis - locate commands in "standard" directories (ignores $PATH) (also locates man pages for you, if any) Note that "whereis" may tell you that a command exists in some standard directory, but when you try to execute the command it may not be found, if the standard directory is not one of your PATH directories. The shell only looks in PATH, not in any "standard" places. Put PATH into all your shell scripts and export PATH If you don't set the PATH in your script, the script will inherit the PATH from the person or program that executes your script. The inherited PATH may or may not contain the correct directories needed to find the commands used by your script - your script may fail. Choose PATH to include the system directories that contain the commands your script needs. Directories /bin and /usr/bin are almost always necessary. System scripts may need /sbin and /usr/sbin. GUI programs will need the X11 directories. Choose appropriately for the script. Unix file system Permissions: Notes: umask.txt Umask and Permissions unix_permissions.txt Unix Permissions Unix is multi-user; people own files. Files/directories can have exactly one owner and be in exactly one group. You can set permissions for three classes: User Group Others Yes, you can create a file and make it readable by everyone else but you if you remove your read permissions from the file. You can also create a file that you cannot write. For example: #!/bin/sh -u umask 222 date >out wc out rm out New commands: chmod chown chgrp umask id groups $ chown idallen file1 file2 $ chgrp daemon file3 file4 $ groups idallen Permissions have a different meaning for a directory: "r" - can see the names (only) in the directory "x" - can use the names (only) in the directory to access the data What permissions for "ls ."? Note "chmod -w" uses umask to know which of UGO to affect! - scripts should always specify explicitly, e.g.: chmod go-w Default permissions for new inodes (files/directories/etc.) are controlled by the umask of the process (an integer): Look at the umask as octal permissions that TURN OFF what you can do: New Files get 666 (rwx) *minus* the bits set in the umask New Directories get 777 (rwx) *minus* the bits set in the umask If you experiment with umask at the shell prompt, remember to reset umask to 022 when you are done! Don't create 000 files and directories! Four shell script basics: interpreter, PATH, umask, collate - we need to control what a shell script inherits from its parent process! - review shell script execution: #!/bin/sh -u - always set 1) kernel line 2) PATH 3) umask 4) collate order - each process has its own pwd, umask, PATH, and environment - setting these values in the script does not affect the parent - these values are inherited by child processes Using "source" statements in scripts $ source foo $ . foo # <- dot is an alias for "source" - similar to #include statements - the *current* shell reads the file - no new shell is started - used extensively in start-up scripts to define functions, e.g. see: /etc/rc.d/init.d/httpd /etc/rc.d/init.d/sshd Shell script functions - functions pass and keep their arguments in $1, $2, etc. - $#, $*, and $@ behave as expected (applying to function arguments) - functions are not passed to child processes unless exported #!/bin/sh -u Errmsg () { echo 1>&2 "$0: Error $*" } if [ ! -w /etc/passwd ] ; then Errmsg "/etc/passwd is not writable" fi Shell "switch" statement case "$string" in start ) echo do something ;; stop ) echo do something else ;; *) echo default case ;; esac - uses GLOB pattern matching against the switch string - first match wins - only the first match is executed - the GLOB pattern "*" always matches - must be the last case The "nc" TCP/IP "Swiss Army Knife" program See week07notes.txt