Shell Script Shell Commands Placed in a File for Later Execution
Total Page:16
File Type:pdf, Size:1020Kb
UNIX
Shell script – shell commands placed in a file for later execution
Files Devices Text Files Program Files Binary Files
I/O device independent input indirection < output indirection > cat
Interprocess Communication pipe -- used to transmit data from one file to another filter – used to select data items from the output of one pipe’s data flow for retransmission through another pipe
Erase Keys Aborting Program Execution Key Backspace Delete Ctrl-H Ctrl-C # Printing Line Deletion Keys lp
vi
Command Mode Insert Mode :set showmode mode display on : set nu line numbers on o i insert text before cursor :set nonu line numbers off o I insert text at beginning of current line o a append text after cursor o A append text to end of current line o open and put text in a new line below current line o open and put text in a new line above current line
Insert Mode Command Mode o Escape
Command Mode Line Mode o :
Command Mode Editing
o r replace single character under cursor o R replace multiple characters, starting at the current cursor position,
o x delete the single character under the cursor o Nx delete N characters, starting with character under cursor, e.g., 5x deletes 5 characters o dw delete the single word beginning with character under cursor o dNw delete N words beginning with character under cursor; e.g., d5w deletes 5 words o D delete the remainder of the line, starting with current cursor position o dd delete entire current line o Ndd or dNd delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines
o yy copy (yank, cut) the current line into the buffer o Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer o p put (paste) the line(s) in the buffer into the text after the current line o P put (paste) the line(s) in the buffer into the text before the current line
o u undo last action (toggle switch) o U undo all changes made to the current line o . repeat last text change at current position File Recovery vi –r
Saving Files o :w
Exiting Vi o :x
Command Mode Cursor Moving o j or
o ctrl-f scrolls down one screen o ctrl-b scrolls up one screen o ctrl-u scrolls up a half a screen o ctrl-d scrolls down a half a screen
Command Mode Screen Manipulation o Ctrl-f move forward one screen o Ctrl-b move backward one screen o Ctrl-d move down (forward) one half screen o Ctrl-u move up (back) one half screen o Ctrl-l redraws the screen o Ctrl-r redraws the screen, removing deleted lines
Line Numbers o :.= returns line number of current line at bottom of screen o := returns the total number of lines at bottom of screen o Ctrl-g provides the current line number, along with the total number of lines, in the file at the bottom of the screen o :set number sets line numbers o :set nonumber eliminates line numbers
Unix Commands cat entire screen delivered to target, e.g., screen more
Special Characters (Shell Usage Only) & ; | * ? ‘ “ ` [ ] ( ) $ < > { } ^ # / | % ! ~
Quoting Special Characters \”Hello\” treated as “Hello” \cntrl-h treated as cntrl-h \cntrl-u treated as cntrl-u
“**” Coffee treated as ** Coffee
File Structure Directories Root directory / Home directory . Relative pathnames working directory Parent directory .. Absolute pathnames root directory Working directory
pwd path to working directory rm cd return to your home directory rmdir ls mkdir ls -l mv
Access Permissions file-types o - file o d directory o l hard link -rwx r- - r- - 1 cputnam c322 6753 May 5 12:06 syllabus o s symbolic link drwx ------cputnam 413 May 5 12:15 grades permissions owner group universe r w xr w x r w x link-count owner-name group-name univ-name file-size creation/modification date & time file-name
chmod 777 syllabus changes permissions to rwx for group and universe, i.e., 111 111 111 binary 7 7 7 hexadecimal chmod 744 syllabus changes permissions to r - - for group and universe, i.e., 111 100 100 binary 7 4 4 hexadecimal
Links
home
alex jenny
File a1 File a2 File j1
working directory alex alex is creating a link to jenny ln a2 /home/jenny/a2 creates link remove link by removing the file Shells
Entering Command Line
Get Next Character
yes
cntrl-h Forget Previous Character in Buffer
no
yes
cntrl-u Forget Entire Buffer
no
no Store Character return in Buffer
yes
Pass buffer to shell for processing
Processing the Command line
Get First Word; Save as Command Name
no
Get Next Word; NEWLINE Save as Argument
yes
Execute Program Program Exists? no
yes
IssueIssue PromptPrompt Terminal File
device file -- /dev directory terminal file name – who listing – name following login name who am i cputnam ttyp19 May 5 19:31:16 writing to terminal displays on monitor reading from terminal reads input from keyboard
login shell directs standard output to the device file monitor standard input to the device file keyboard
cat file1 – executed with the argument file1 writes file1 to the standard output cat – executed without arguments reads from the standard input writes to the standard output until cntrl-d is entered EOF signal sent to cat
redirection standard input cat file1 < file0 standard output cat file1 > file2 cat file1 file2 file3 > file4 copies the contents of files 1, 2 & 3 to file4; the original contents of file4 are destroyed appending cat file1 >> file0 appends contents of file1 to the tail of file0
pipes connect stdout of first command to srdin of second command can be used on any command that accepts input from the command line or from stdin o cat file1 | tr . ; > file2 cat send contents of file1 to trace, i.e., tr, which substitutes “;” for each “.” encountered; the result is redirected to file2; file1 remains untouched. ls –l | lp long listing of the current directory is sent to the printer who | grep ‘cputnam’ list of currently active accounts is sent to the grep utility which filters out all accounts which do not contain the string “cputnam”; the strings containing “cputnam” are sent to stdout who | sort | lp The stdout of who is sent to the stdin of sort; the stdout of sort is sent to the stdin of lp
tee splits the output into two streams o one is sent to the stdout $who | tee who6.5.2010 | grep ‘cputnam’ sends stdout stream of who to file who6.5.2010 sends stdout stream of who to grep which filters out all entries not containing the string “cputnam” o the other is sent to a designated file Background Programs $ ls –l | lp & lp will run in the background, i.e., stdio will immediately be returned to the terminal lp messages will be sent to the stdout, i.e., the terminal, except for a messy screen, the messages will not corrupt the files that are currently being modified in the foreground $ ls –l | lp > lp.out 2> lp.err & same as before, but stdout is redirected to lp.out and stderr is redirected to lp.err; nothing will interrupt the foreground session except a request by lp for input killing a background job o kill 0 kills all background jobs o kill
Filename Spawning Special Characters (metacharacters) (wildcards) If the special character appears in an argument list, shell expands the argument into a list of filenames to be processed by that command Question Mark “?” “?” can be replaced by any single character o $rm memo? deletes any file whose name is five characters in length and which contains the string ”memo” as the first four characters of its name does not delete “memo” or “memo57” o $rm m?mo deletes file such as memo, mamo, momo, mumo, mbmo, etc. Askerisk “*” “*” can be replaced by any number of characters, including nothing o $rm memo* deletes all files which contain the string “memo” as the first four characters of the name o $rm * DO NOT DO THIS!!
If you feel the need to use wildcards in a deletion command, use upmost care as many a programmer has accidently deleted important files by a moment’s lapse in judgment or a slight pause in mental acuity! In other words, DON’T DO IT!
The one place where it is useful, and not necessarily destructive, is in deleting an entire program, e.g., $rm fib* removes fib, fib.c & fib.o
Wildcards can be very useful in moving files from one directory to another
Brackets “[ ]” o define a character class of all the characters listed inside the brackets o each character in the class is substituted, one at a time, in place of the brackets o the resulting list of file names are passed one-by-one to the command for processing o $cat file[0 1 2 3 4 ] >> file5 appends file0, file1, file2, file3 & file4 to file5 in the order listed