This page was generated from 09.commands.mth.

This page is part of the course work for CS360: Systems Tools course offered by the Computer Science Department at CalState, San Bernardino, California, USA. It was generated by Dr. Dick Botting.

Contents


    Commands Done Quicker

    Why

    Who wants to type lots of stuff...

    Sections


    (why): 9.01, 9.13
    (exam): 9.04, 9.12, 9.13, 9.16
    (PostIt Note):
    (useful): 9.02, 9.03, 9.04, 9.05, 9.07, 9.12, 9.13, 9.14, 9.15, 9.16, 9.17, 9.19, 9.20, 9.21, 9.22, 9.23, 9.24
    (perhaps): 9.06, 9.08, 9.09, 9.10, 9.11, 9.18, 9.25
    (skip): 9.26

    Facts

    Again... lots of useful tricks that you can use, but don't need for the final exam.... but make sure you know about:

    Even experts like me forget line and word erase control characters (09.02)

    Watch out for trying to create files by using wildcards.... See 1.16 again.

    09.05 - I've not used this very much. It should work on our workstations and servers. However I'd avoid it in scripts which may be executed by the original 'sh' Bourned Shell! Try these out! Use if you want. But not on the final exam.

    09.06 May be useful on our workstations, but not on a non-Linux server. As a rule I find editting previous commnads like this a bit difficult. Not on exam.

    09.07 New: I've never used these (I use commands to edit strings... more later on this. Will not be on the final exam.

    You can skip 09.08 to 09.10 if you wish, but I've known people to get very attached to this.

    In the Korn shell: if you define EDITOR and VISUAL [ 06.variables.html ] [ 06.variables.html ] Then the ESC character will let you edit your history using your favorite visual editor. For example, ESC k, shows the previous line and you can tap Return and re-execute it.

    The fantastic for loop.... but skip 09.11 (unless you like csh). The ksh/sh/bash format(9.12) is IMHO(In My Humble Opinion) easier to type and read:

     		for file in /usr/fran/report /usr/rob/file3 /usr/rob/file2
     		do cat -t -v $file | pg
     		done

    Study and experiment with the content of 09.12. I use it a lot.

    	for f in notes/*
    	do cat template >$f
     	done
    or
     	for n in 0 1 2 3 4
     	do
     	  for m in 0 1 2 3 4 5 6 7 8 9
    	  do
    		echo $n$m > notes/$n$m
    	  done
      done

    09:13: Vital information! Use it all the time...

    09:14: "Here documents" I use this every 2 minutes, 24 hours a day, and 7 days a week! Seriously: I have a daemon running to sort my EMail and part of the script looks like this

     		mailx <<++++++
     		s/XXXX mail/junk
     		d/junk
     		s/cs360 todo/cs360
     		s/cs360 cs360/received
     		d/cs360
    and so on until
     		++++++

    To see the full thing in its horror and glory, rlogin into Blaze and look inside

     		~dick/bin/sort.my.mail

    09.15: I like developing throw-away scripts. Especially as you can execute them without leaving the editor!

     		:!sh %
    executes the sh shell with the file name as an argumnet. For testing
     		:sh -x %

    The incredible back-quotation marks. Lets you compute a part of a command! (09.16).

    09.17.... shows you lists of files in a command.... suppose you had a list of all the people in this class in file "roster", what would the following do:

     	mailx `cat roster`

    09.18. Hmm new in bash. Not in final. But looks like I might use it!

    09.19. Type ahead

    09.20 Introduces my favorite technique "while read line; do something; done"

    09.21. When you don't have "xargs" you can get the same effect by a one line Korn shell loop. Replace

    	xargs command <from
    by
    	while read args; do command $args; done <from

    Skip(?) 09.22 and 09.23

    09.24 - I think the book may be a bit too clever here. I use the following.

     	ls -ltr|tail -20
    to get the last 20 files that were modified. I put it in a file
     		~dick/bin/latest
    and made it public and executable. Nice and useful. Or try
     ls -ltr|grep "Jan 16"
    for those modified on that day.

    On the other hand I regularly save a complete directory into a file:

     ls >dir
    and then edit it to get a list of files for future work, or even into commands to be obeyed. One case is when I transfer files from my home PC to here via a DOS disk. The dir file is edited to be the commands that reconstruct the UNIX files names from their DOS counterparts.... then I dump that file on the disk as well.

    Notice the tricks with grep/vi/sed/sort for generating a list of files that contain a text string....

    Skip 09.25... However somewhere I have a repeat n times script...


    (expect): 09.26. A new feature and tool that I have yet to master... Paul Tonning uses it a lot. It won't be on the final.

    Syntax

  1. erase_word::=CTRL/U.
  2. erase_line::=CTRL/X.
  3. see_line_so_far::=CTRL/R.
  4. CTRL::=indicates that the CTRL is to be held down while the key following "/" is tapped.

  5. Bourne_shell_for_loop::=for variable in list ; do commands; done.
  6. Bourne_shell_for_each_argument_loop::=for variable ; do commands; done.
  7. Bourne_shell_while_loop::=while commands ;do commands; done. Repeats the loop until one of the first sequence of commands fails.

  8. commands::= command;command;...
  9. list::= a space separated list of words and strings.
  10. variable::=any normal shell variable.

  11. enter_korn_shell_edit::=ESC

  12. For line E, here_document::= command <<E eol ... eol E eol, -- the lines between the command and the second E are input into the command after variables are substituted!.

  13. eol::=end of line.

  14. list_tail_of_file::=tail -number_of_lines file.
  15. list_files::=ls -ls_options.

  16. number_of_lines::=any decimal number.
  17. file::=a name of a file.
  18. ls_options::= one or more letters and digits.
  19. ls_option::= long | time | all | reversed | size | Flagged | Recursive | direct | ...,
    Net
    1. long::ls_option= l, provide more details on each item.
    2. time::ls_option= t, in order of time with latest first.
    3. all::ls_option= a, include files with name starting with a period.
    4. reversed::ls_option= r, reverse order of listing.
    5. size::ls_option= s, provide size of file in bytes.
    6. Flagged::ls_option= F, Compress detail down to a single character.
    7. Recursive::ls_option= l, list each directory, and its subdirectories etc.
    8. direct::ls_option= l, list info on given directory or file names and do not list content of the directories.

    (End of Net)

  20. search_for_a_pattern_in_some_files::= egrep pattern files...
  21. pattern::=a description of lines of interest, [ 26.patterns.html ] [ 26.patterns.html ]

  22. sort::command, the UNIX system sort - fast, general purpose, comprehensive with many posible options and features.
  23. sort_and_make_unique::=sort -u.

  24. global_substitute_in_vi::= :%s/pattern/replaced_by/.
  25. replaced_by::=a string.

  26. s::ex_command, substitution.

  27. ex_command::=in vi you can type a colon and then any command of the ex editor.

  28. ex::=the extended UNIX editor.
  29. vi::=the visual UNIX editor, actually a visual interface arround ex. [ 30.vi.html ] [ 30.vi.html ]

    Questions


      You create a new directory for a project. You want to create 5 files with names a,b,c,d, & e. You 'cd' into the new, empty directory. Your command is
       	vi [a-e]
      What happens?
    1. The shell reports an error message before calling vi ?
    2. The shell creates the files before calling vi ?
    3. The shell calls vi and passes it the argument '[a-e]', and vi creates the files. ?
    4. The shell calls vi and passes it the argument '[a-e]', and vi warns you that this file does not exist ?


      You create a new directory for a project. You want to create 5 files with names a,b,c,d, & e. Your command is
       	for f in a b c d e f;do echo >$f; done
      What happens
    1. The shell reports an error message ?
    2. The shell creates the files with a blank line in each file. ?
    3. echo complains that each of the files does not exist, in turn ?
    4. None of the above ?


      You create a new directory for a project. You want to create 5 files with names a,b,c,d, & e. Your command is
       	for f in a b c d e f  do echo >$f; done
      What happens
    1. The shell reports an error message ?
    2. The shell creates the files with a blank line in each ?
    3. Echo complains that each of the files does not exist, in turn ?
    4. None of the above ?


      What does the following korn/bourne shell command output?
       	for f in *.txt;  do echo $f
      		cat >>$f
       	done
    1. The shell reports an error message ?
    2. The shell obeys the statements between "do" and "done" for each file in the current working directory in turn ?
    3. Each file name is printed and then the user must supply data to be added to it. ?
    4. Both of the last two answers. ?


      What is output by this Korn shell command when environment variable V has value "date" on January the 15th 1996?

       	echo 'V' '$V' "V" "$V" V `$V`
       	V date V date V date
      ?
       	V $V V date V date
      ?
       	V $V V $V V date
      ?
      	V $V V date V Jan 15 1996....
      ?
      	V $V V $V V Jan 15 1996....
      ?
      	V date V date V Jan 15 1996....
      ?


      What is output by this shell command
       		ls -ltr | tail
    1. a. A list of 10 files that changed or created most recently ?
    2. b. A list of 10 files that have not been changed or created for the longest time ?
    3. c. A list of files with names containing the letters tail ?
    4. d. The same things as: ls -ltr >/tmp/mine; tail /tmp/mine ?
    5. e. Both a and d ?
    6. f. both b and d ?
    7. g. both c and d ?


      What is output by this shell command
       		ls -ltr | grep  tail
    1. a. A list of 10 files that changed or created most recently ?
    2. b. A list of 10 files that have not been changed or created for the longest time ?
    3. c. A list of files with names containing the word tail ?
    4. d. The same things as:
       		ls -ltr >/tmp/mine; grep tail /tmp/mine
      ?
    5. e. Both a and d ?
    6. f. both b and d ?
    7. g. all of a, c and d ?

    Submit Your Notes Here

    To earn credit for completing this part of the course you need to send me a short list of things you have learned. A simple way to do this is to follow this [click here [socket symbol] if you can fill this hole] link and fill in the form.

End