IT 244 – Chapter 5 The Shell: Command Line:

advertisement
IT 244 – Chapter 5
The Shell:

Command Line:
o Command [arg1 [arg2 … ]] <Return>
 Command name:
 Anything that is allowed as a file name
 Can be ‘–l’, but this of course is a problem, unless you
quote it correctly (‘-l’ or \-l). Same for any special char.
 Must be chmod’ed to be executable by the given
user/group/all
 Arguments
 Options
o Usually uni-character starting with a hyphen
 These can be grouped unless any takes an
argument
o Often full word starting with a double-hyphen
 These cannot be grouped
 Non-option
o Unless these are tied to a uni-character option,
convention has it that these come after options,
but many commands allow arguments to be
given in whatever order they come, except for
grouped arguments.
 Filename completion in shells
o Processing:
 The shell breaks a full command (command name, options, and
other arguments) on whitespace, into tokens.
 Usually this list is processed from left to right, but even
if that is the case, order may not matter.
 The shell starts processing the grouping of tokens by finding
the name of the command.
 Search against the $PATH to find it
 If ‘.’ Is not in $PATH, the command will not be found.
You must then precede the command name with ‘./’
 Once the command is found, and is found to be executable:
 Memory is set up for the command to run in, and it is
allocated to the command.
 The command-line arguments are passed to the
command process in the order in which they were given
on the command line.
 The shell goes into sleep mode until either:
o the process is completed or
o the process is sent to the background with Ctrl-Z.

When the command ends, it returns an exit status.
Usually, 0 means a good end, and anything else is a code
for the reason it ended badly.

Built-in commands
o Built-ins are commands that are part of the shell process
 When a built-in is run, a new process is not forked, so much
quicker

Path environment variable:
o Colon separated list of absolute and relative paths
o Temporarily change: PATH=$PATH:<and the new stuff>

Standard Input and Output
o Standard input – a place to get input from
o Standard output – a place to send output
o Standard error – not the same as standard output, but can be
redirected to go there.
o Both of these are given as devices, found in the /dev directory.
 Look at a ps or who output to see the tty. This is a file.
 There are also monitor, disk, tape, etc. device files
o Neither origin nor destination is known to the process. These are set
up by the shell according to the context of the run, ie:
 Command line no pipe
 Command line with pipe
 Started as a child process from another process
o The default standard in is the keyboard, and the default standard out
is the screen.
o If the shell sees that this process is not tied to a terminal, then it will
figure out whether this is command list with a pipe, and sends as in to
the next command, or redirected.

Redirecting output
o command [arguments] > filename
 NOTE: redirecting can destroy a file!!! Since it does not append
 set –o noclobber
o avoids overwrites (good for shell scripting)
o unset noclobber
 Override noclobber with | symbol as so:
o command [arguments] >| filename
o If you grep for something with standard error, you will not see the
standard error. You must use redirection as:
 command [arguments] 2>&1 >filename
 where 0 is standard in, 1 is standard out, 2 is standard
error.
o Appending to a file (>>)
 BE VERY CAREFUL – easy to use only one > sign
o Unwanted data (such as standard error when viewing)
 Redirect to /dev/null

Redirecting input
o command [arguments] < filename
o Used to read a file into standard input for a command
o Same as cat’ing the file and piping it to the command

Piping
o Tying the standard output of one command to the standard input of
another.
 As said before, to tied standard error also into standard input,
you have to redirect standard error to standard output
o To do the same thing with redirects, you would create an interim file
as output of one and read it in as input to another command. You then
have a file that needs to be deleted.
o Some commands, like tr, take their input from standard input only.
This is usually because they aid in processing information for
scripting.
o Some commands, like lpr, accept from either standard in or command
line.
o tee – A command that sends output to both standard output and file.
 -a – option to append
 -i – ignore SIGINT – means won’t respond to Ctrl-C.

Backgrounding and Foregrounding processes:
o Use the & symbol to background a process as you start it
o Use the Ctrl-Z sequence to background a running process
o Use the jobs command to see the list of backgrounded jobs
o Use the fg command, followed by the associated job number found by
running ‘jobs’ to foreground a particular job.

Cleaning up running processes:
o kill <option> <processid>
 -1 – hang up
 -2 – interrupt
 -3 – quit
 -6 – abort
 -9 – kill
 -14 – alarm clock
 -15 – software termination signal

Filename expansion
o Applies to files found in the folder referenced by the absolute or
relative path given (if none given, then ‘.’).
o Wildcards:
 * - any number of any type of characters
 ? – one of any type of character
 [] – lists possibilities for one match
 [abcd]
 [a-d]
 [a-zA-Z0-9]
 Character classes:
o [!<list>] or [^list] means NOT any of these
o [^<list>]
o A character class matches a ‘-‘ or ‘]’ by placing it
immediately before the close bracket.
 MUST give the ‘.’ before either to see hidden files.
 This is one instance where echo * and ls * will show the same
output, except for directories and not formatted the same way.
Download