CS 279 - Week 7 Lecture 2 - 2022-10-05
TODAY WE WILL
* announcements
* p.s. about exit statuses
* standard files and redirection
* initialization files!
* alias command
* prep for next class
* turn in your at-least-first-attempts at Homework 4
by 11:59 pm on Friday
=====
p.s. about exit statuses
=====
* we mentioned how exit status of 0 is intended
to be "successful completion", and non-zero
not-so-successful
* a bash shell -- interactive or in a shell script --
can see the exit status of the last command run
by looking at the value of of $?
=====
standard files and redirection
=====
* Some UNIX/Linux commands, such as cat,
will read their input from standard input
unless you specify files explicitly
and MANY write their output to standard output
(as we know!)
* We also know:
you can redirect standard output to a file
with >
jobs > curr_jobs.txt
and we can ask to append that redirected standard output
using >>
jobs >> curr_jobs.txt # has 2 jobs' output included
* you can redirect standard input to be from a file instead
of from the keyboard with < followed by a filename
./input-redir-ex < looky.txt
* standard error is another standard file!!
* by default, standard error is set to the screen
as standard output is
* you can redirect standard error using
2> (followed by the file to write that to)
cp * play 2> see-errors.txt
...and any errors, like trying to copy directory
play to itself, are redirected to see-errors.txt instead
of to the screen
(and 2>> does append!
cp * play 2>> see-errors.txt
)
* redirections have higher precedence than pipes
* FIRST the redirections are associated with commands,
THEN the commands and their redirections are passed through
the pipes
$ grep "value" * 2> errors.txt | sort > looky.txt
* filter: (in UNIX/Linux world): for a program that takes
standard input data, transforms it, and produces standard output
* filter programs work great with pipes!!
======
file descriptors
======
* each process has a numbered set of FILE DESCRIPTORS
associated with it
* when a program reads from or writes to a file,
it refers to that file by its descriptor
* three file descriptors are created when a process
starts up:
0 - standard input (abbeviated stdin)
1 - standard output (abbreviated stdout)
2 - standard error (abbreviated stderr)
^ yes, that's where the 2 in 2> i comes from...
* every file descriptor is associated with a file --
by default, file descriptors 0, 1, and 2 are associated
with /dev/tty, which is your terminal (and keyboard?)
unless you have modified them using redirection or pipes!
======
(shell) Initialization Files
======
* these can vary based on UNIX/Linux distribution...
(so, take this with a grain of salt!)
* there are system-level initialization files,
and each user can have their own personal initialization files
* FOR EXAMPLE,
/etc/profile
* starting an interactive shell, this will typically be invoked
/etc/bashrc
* LDP "Bash Beginners Guide" says this will tend to
contain Bash-specific configurations;
and nrs-projects' /etc/profile include:
"# System wide environment and startup programs, for login setup"
[go in /etc/profile,]
"# Functions and aliases go in /etc/bashrc"
~/.bash_profile
* this is a single user's Bash-specific configuration file
LDP Bash Beginners Guide:
...preferred configuration file for an individual's environment?
...BUT frequently uses the source command to run .bashrc!
~/.bash_login
LDP Bash Beginners Guide:
* contains settings normally executed only when you
log into a shell
~/.bashrc
LDP Bash Beginners Guide:
* this will be a sought when a non-login shell is
started
* I've seen the advice to put aliases and functions
here...?
* and the other .bash_* files often source this,
so both login and non-long see it
if [ -f ~/.bashrc ]
then
source ~/.bashrc
fi
* FUN FACT: in the Bourne shell (and subsequent
shells) . starting a command means the same as source...!
=====
looking in additional directories for a command: adding to PATH
=====
* the PATH environment variable specifies the directories to search to find
command executables
* you can ADD directories to this, such as a personal bin directory or the
current working directory
* the pathnames to be checked are SEPARATED by COLONS
* ADD to this, do not completely reset it; you do not want to "lose" access
to regular UNIX/Linux commands!
* for example, to add a bin directory in your home directory to your
PATH (noting that HOME contains your home directory's path):
export PATH=$PATH:$HOME/bin
* for example, to add both a bin directory in your home directory and the
current working directory to your path:
export PATH=$PATH:$HOME/bin:.
=====
alias command
=====
LDP Bash Beginners Guide:
* allows a string to be substituted for a word when it is
used as the first word of a simple command
alias desired_alias="cmd it is an alias for"
* users frequently put these in their .bashrc files in
their home directory