Shell Script Shell Commands Placed in a File for Later Execution

Total Page:16

File Type:pdf, Size:1020Kb

Shell Script Shell Commands Placed in a File for Later Execution

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 check  Ctrl-U  lpr 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

 vi 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”

 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, to exit o cw change the current word with new text, starting with the character under the cursor, to exit o cNw change N words beginning with character under cursor, to exit e.g., c5w changes 5 words o C change (replace) the characters in the current line, to exit o cc change (replace) the entire current line, to exit o Ncc or cNc change (replace) the next N lines, starting with the current line, to exit e.g., 5cc &/or c5c changes the next 5 lines including the current line

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 retrieve crashed file

 Saving Files o :w write current contents to file named in original vi call o :w newfile write current contents to a new file named newfile o :w! oldfile write current contents over a pre-existing file, oldfile

 Exiting Vi o :x quit vi, writing out modified file to file named in original invocation o :wq quit vi, writing out modified file to file named in original invocation o :q quit (or exit) vi prompting for file name o :q! quit vi even though latest changes have not been saved for this vi call

 Command Mode Cursor Moving o j or or [down-arrow] move the cursor down one line o k or [up-arrow] move cursor up one line o h or or [left-arrow] move the cursor left one character o l or 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 or 1G move the cursor to the first line in the file o :n or nG move cursor to line n, e.g., 5 or 5G moves the curser to line 5 o :$ or G move cursor to last line in file

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  next page no EOF display; direct return to shell  pg  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 displays first ten lines of file  head –n displays first n lines of file  sed –n displays first n lines of file  grep ‘search-string’ searches for occurrences of any string containing ‘search-string’ as a subsring  tail displays last ten lines of file  tail –n displays last n lines of file  sort sorts the contents of line-by-line  uniq displays skipping adjacent duplicate lines  sort | uniq > newfile produces a file named newfile that contains no duplicate lines  whereis not SVR4 path to , location in man provides listing of multiple occurrences of  which not SVR4 returns in the current path

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

 ls –a  mv  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 /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

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  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

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

Recommended publications