Shell Script Shell Commands Placed in a File for Later Execution

Shell Script Shell Commands Placed in a File for Later Execution

<p>UNIX</p><p>Shell script – shell commands placed in a file for later execution</p><p>Files  Devices  Text Files  Program Files  Binary Files</p><p>I/O  device independent  input indirection <  output indirection >  cat</p><p>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 </p><p>Erase Keys Aborting Program Execution Key  Backspace  Delete  Ctrl-H  Ctrl-C  # Printing Line Deletion Keys  lp <dest> check  Ctrl-U  lpr <dest> check  @ Password Word Deletion Key  passwd  Ctrl-W Logging Out New Command Line  Ctrl-D  Ctrl-R redraws command line with  exit current  logout command (eliminates garbage on old line) Vi Editor</p><p> vi <file name> o if no file name is provided upon entry, vi will request a name at the termination of the editor o upon opening a file, vi is in Command Mode o Escape  “beep”</p><p> 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</p><p> Insert Mode  Command Mode o Escape</p><p> Command Mode  Line Mode o :</p><p> Command Mode Editing</p><p> o r replace single character under cursor o R replace multiple characters, starting at the current cursor position, <Esc> to exit o cw change the current word with new text, starting with the character under the cursor, <Esc> to exit o cNw change N words beginning with character under cursor, <Esc> to exit e.g., c5w changes 5 words o C change (replace) the characters in the current line, <Esc> to exit o cc change (replace) the entire current line, <Esc> to exit o Ncc or cNc change (replace) the next N lines, starting with the current line, <Esc> to exit e.g., 5cc &/or c5c changes the next 5 lines including the current line </p><p> 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</p><p> 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</p><p> 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 <file name> retrieve crashed file</p><p> Saving Files o :w<Return> write current contents to file named in original vi call o :w newfile<Return> write current contents to a new file named newfile o :w! oldfile<Return> write current contents over a pre-existing file, oldfile</p><p> Exiting Vi o :x<Return> quit vi, writing out modified file to file named in original invocation o :wq<Return> quit vi, writing out modified file to file named in original invocation o :q<Return> quit (or exit) vi prompting for file name o :q!<Return> quit vi even though latest changes have not been saved for this vi call</p><p> Command Mode Cursor Moving o j or <Return> or [down-arrow] move the cursor down one line o k or [up-arrow] move cursor up one line o h or <Backspace> or [left-arrow] move the cursor left one character o l or <Space> or [right-arrow] move the cursor right one character o 0, i.e., zero, move the cursor to start of current line o $ move the cursor to end of current line o w move the cursor to beginning of next word o b move the cursor back to beginning of preceding word o :0<Return> or 1G move the cursor to the first line in the file o :n<Return> or nG move cursor to line n, e.g., 5<Return> or 5G moves the curser to line 5 o :$<Return> or G move cursor to last line in file</p><p> 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</p><p> 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</p><p> 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</p><p>Unix Commands  cat entire screen delivered to target, e.g., screen  more <sp>  next page no EOF display; direct return to shell  pg <return>  next page EOF display K  shell  who or w displays list of users currently on the system  talk or write  mailx check – may be obsolete  finger displays list of all users of the system  mesg returns your current message status  mesg n blocks messages to your terminal  mesg y allows messages to pop up on your screen at will  man displays manual WARNING – may not be useful  man –k displays list of keywords  date  head <file name> displays first ten lines of file  head –n <file name> displays first n lines of file  sed –n <file name> displays first n lines of file  grep ‘search-string’ <file-name> searches <file-name> for occurrences of any string containing ‘search-string’ as a subsring  tail <file name> displays last ten lines of file  tail –n <file name> displays last n lines of file  sort <file-name> sorts the contents of <file-name> line-by-line  uniq <file-name> displays <file-name> skipping adjacent duplicate lines  sort <file-name> | uniq > newfile produces a file named newfile that contains no duplicate lines  whereis <tool name> not SVR4 path to <tool name>, location in man provides listing of multiple occurrences of <tool name>  which <tool name> not SVR4 returns <tool name> in the current path</p><p>Special Characters (Shell Usage Only) & ; | * ? ‘ “ ` [ ] ( ) $ < > { } ^ # / | % ! ~</p><p>Quoting Special Characters \”Hello\” treated as “Hello” \cntrl-h treated as cntrl-h \cntrl-u treated as cntrl-u</p><p>“**” Coffee treated as ** Coffee</p><p>File Structure Directories Root directory / Home directory . Relative pathnames working directory Parent directory .. Absolute pathnames root directory Working directory</p><p> pwd path to working directory  rm  cd return to your home directory  rmdir  ls  mkdir  ls -l  mv <file> <dir>  ls –a  mv <file1> <file2>  cp source-file destination-file Standard Directories / root – ancestor of all files in he filesystem /home one of several subdirectories of root that may contain users home directories /usr contains subdirectories that contain system information /usr/bin contains the standard Unix programs /bin same as usr/bin /usr/sbin system administration utilities /usr/ucb contains BSD utilities /usr/bsd contains BSD utilities /sbin system administration utilities /etc configuration & other system files /etc/passwd classical directory for password files <not currently used> /var subdirectories contain files whose contents vary as system runs temporary files, system logs, spooled files, etc. /dev contains peripheral device files /tmp contains temporary files</p><p>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</p><p> 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</p><p>Links</p><p> home</p><p> alex jenny</p><p>File a1 File a2 File j1</p><p> working directory alex  alex is creating a link to jenny  ln a2 /home/jenny/a2 creates link  remove link by removing the file Shells</p><p>Entering Command Line</p><p>Get Next Character</p><p> yes</p><p> cntrl-h Forget Previous Character in Buffer</p><p> no</p><p> yes</p><p> cntrl-u Forget Entire Buffer</p><p> no</p><p> no Store Character return in Buffer</p><p> yes</p><p>Pass buffer to shell for processing</p><p>Processing the Command line</p><p>Get First Word; Save as Command Name</p><p> no</p><p>Get Next Word; NEWLINE Save as Argument</p><p> yes</p><p>Execute Program Program Exists? no</p><p> yes</p><p>IssueIssue PromptPrompt Terminal File</p><p> 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</p><p> login  shell directs  standard output to the device file  monitor  standard input to the device file  keyboard</p><p> 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</p><p> 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</p><p> 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</p><p> 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 <pid#>  kills background job with selected pid number . $ tail –f file1 &  starts background job . $ps  produces a listing of all jobs currently running $ps PID TTY TIME COMMAND 324 tty06 0:09 sh 451 tty06 0:05 tail 453 tty06 0:02 sh $kill 451  kills job 451, i.e., tail</p><p>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!!</p><p>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!</p><p>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</p><p>Wildcards can be very useful in moving files from one directory to another</p><p> 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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us