Unix-Linux Commands and Shell Programming
Unix-Linux Commands and Shell Programming
UNIX Overview
Why UNIX?
Control
Commands often provide complete access to the system and its devices Most devices can be manipulated just like files
Flexibility
Commands are just programs Commands have common interface to allow interoperation with other commands The UNIX shells provide the glue for this `
Reliability
Commands are typically lightweight since they typically do little more than invoke operating system calls Individual commands that are broken can easily be replaced
The omnipresent failsafe. Nowadays, turned off due to lack of adequate security.
Secure. Data is encrypted over the wire. What we use. Not always available outside CU due to different versions, implementations, platform availability.
ssh [user@]hostname
Log in!
3 tries to get valid username and password right
Logout!
exit CTRL-D
man will present the manual page of the specified entry using more or less.
In Linux, the default is less, but can be overridden less presents a screen-full at a time. spacebar moves forward, b moves backward, $ moves to end, q quits, ? helps.
Most commands have HTML references on the WWW. Dont panic. Just e-mail me or Dan.
cd
cd cd cd cd cd .. /home/tim/projects ~/projects ~tim/projects $HOME/projects
mkdir rmdir mv
mv oldfilename newfilename mv file1 file2 file3 newtargetdirectory
-- syntax like mv
cp r dir1 dir1copy
Unix: SH basics
Modifying environment variables
sh: PAGER=/usr/bin/less; export PAGER bash: export PAGER=/usr/bin/less tcsh: setenv PAGER /usr/bin/less
Any unquoted # is treated as the beginning of a comment until endof-line Every line is first parsed for shell metacharacters. These include characters that the shell will do something with and include:
#&><$%*[]?!`~;|, {}
Distinct commands may be separated by end-of-line, semicolon, or comma Environment variables are $EXPANDED Back-tick subshells are executed and `expanded` Pipelines are created | joining the output of | one program | with the next Any commands left over must be builtins or external commands. An error will fail the pipeline, but the script will continue!
>filename redirects just standard output Dont Clobber me! By default, > will overwrite existing files, but you can turn this off using shell settings and/or environment variables. Appendicitis! You can append to existing files this way:
- sh: >>filename >&1 - csh: >>&filename
Useful when a program does not already query the command line for files to read
program1 || program2
Program 2 will execute if and only if program1 exited with a non-0 status Example:
project1 || echo Project1 FAILED to complete!
csh example:
foreach VAR ( test1 test5 test7b finaltest ) runmycode $VAR >$VAR.out end
Regular Expressions
Powerful language for specifying strings of text to be searched and/or manipulated. Used by
grep sed awk perl Get Regular Expression and Print search files line by line Simple Editing tool, right from the command line Scripting language, executes program on matching lines Pathological Rubbish Lister. Powerful programming language
Note: These are not file-globs. The syntax is similar, but the semantics are slightly different! Cannot be used to match nested structures
Examples:
Match a line beginning with a space-padded line number and colon. ^[ \t]*[0-9][0-9]*: Match my name (various spellings) (Tim Shelling)|(TJS)|(T\. Shelling)|(Timothy J\. Shelling) Match if the line ends in a vowel or a number: [0-9aeiou]$ Match if the line begins with anything but a vowel or a number: ^[^0-9aeiou]
Archives