1 / 34

Flavors of Unix Shells

Flavors of Unix Shells. There are 2 main flavors of Unix Shells: Bourne (Standard Shell): sh , ksh , bash, zsh Faster Has a more consistent behavior C shell : csh , tcsh Easier to learn at first Has features that make it good working at the command prompt

taffy
Télécharger la présentation

Flavors of Unix Shells

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Flavors of Unix Shells There are 2 main flavors of Unix Shells: Bourne (Standard Shell): sh, ksh, bash, zsh Faster Has a more consistent behavior C shell : csh, tcsh Easier to learn at first Has features that make it good working at the command prompt But, as you get more advanced, you begin to encounter weird features

  2. Flavors of Unix Shells There are 2 main flavors of Unix Shells: Bourne (Standard Shell): sh, ksh, bash, zsh Faster Has a more consistent behavior C shell : csh, tcsh Easier to learn at first Has features that make it good working at the command prompt But, as you get more advanced, you begin to encounter weird features…

  3. An example of one of csh’s weird features (in comparison to bash) In csh, this is an error: grep "$1$" helloworld.c Variable name must contain alphanumeric characters Instead you need: grep"$1"'$' Interestingly (and illogically) the $ inside of a "" is sometimes OK: grep"Allice$1"

  4. An example of one of csh’s weird features (in comparison to bash) In csh, this is an error: grep "$1$" helloworld.c Variable name must contain alphanumeric characters Instead you need: grep"$1"'$' Interestingly (and illogically) the $ inside of a "" is sometimes OK: grep"Allice$1" This $ has special meaning to the grep command.

  5. An example of one of csh’s weird features (in comparison to bash) In csh, this is an error: grep "$1$" helloworld.c Variable name must contain alphanumeric characters The error message occurs because *this*is considered as a variable with no name. Instead you need: grep"$1"'$' Interestingly (and illogically) the $ inside of a "" is sometimes OK: grep"Allice$1"

  6. In csh, this is an error: grep "$1$" helloworld.c Variable name must contain alphanumeric characters The error message occurs because *this*is considered as a variable with no name. Instead you need: grep"$1"'$' helloworld.c Interestingly (and illogically) the $ inside of a "" is sometimes OK: grep"Allice$1" An example of one of csh’s weird features (in comparison to bash)

  7. In csh, this is an error: grep "$1$" helloworld.c Variable name must contain alphanumeric characters The error message occurs because *this* is considered as a variable with no name. Instead you need: grep"$1"'$' helloworld.c Interestingly (and illogically) the $ outside of the "" is also OK, if nothing comes after it: grep"Hello"$ helloworld.c An example of one of csh’s weird features (in comparison to bash) The illogicality here is that the "$" has a less-strict quoting than the $ does (in this case).

  8. csh vs. bash:set, :q %bash % A="*" % echo $A <lists all the files> % echo '$A' $A % echo "$A" * % echo $A:q *:q % % csh % set A = "*" % echo $A <lists all the files> % echo '$A' $A % echo "$A" * % echo $A:q * %

  9. tcsh vs. bash:for, $*, $@ % cat for_test.csh #!/bin/csh echo 'using $*' foreach arg ($*) echo "$arg" end echo 'using $@' foreach arg ($@) echo "$arg" end % ./for_test.csh 1 2 3 using $* 1 2 3 using $@ Illegal variable name. % cat for_test.bash #!/bin/bash echo "using \$* " for arg in "$*" do echo "$arg" done echo "using \$@ " for arg in "$@" do echo "$arg" done % ./for_test.bash 1 2 3 using $* 1 2 3 using $@ 1 2 3

  10. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does NOT use regular expressions. It uses wildcards. How would specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 10 10

  11. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does NOT use regular expressions. It uses wildcards. How would specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 11

  12. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does NOT use regular expressions. It uses wildcards. How would specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 12

  13. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does NOT use regular expressions. It uses wildcards. How would specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 13

  14. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does not use regular expressions. It uses wildcards. How would specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 14

  15. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does not use regular expressions. It uses wildcards. How do you specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 15

  16. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does not use regular expressions. It uses wildcards. How do you specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we do better? 16

  17. Regular ExpressionsAre we clear on the concept? What will this do: % grep 'AB*C' AB*C Answer: it will search for all patterns such as AC, ABC, ABBC, ABBBC, etc. Within files that have names such as: ABC, ABXC, ABBDC, etc.So the word STACK will match, if found in a file named ABFDSCFFGFGC. Are we clear on the concept? Grep uses regular expressions, but the second argument is never seen by grep; the UNIX shell converts it into a list of files before it initiates the grep program – and the UNIX shell does not use regular expressions. It uses wildcards. How do you specify the regular expression 'AB*C' using wildcards? Answer: *A*C* would be as close as you can get. Why can’t we get any closer that that? 17

  18. Searching for something in a file fgrep/grep: useful options Memorize all of the following: -i case-insensitive search -n show the line# along with the matched line -v invert match, e.g. find all lines that do NOT match -w match entire words, rather than substrings -o only print the matching pattern -A print a certain number of lines AFTER the matching line. (It is used with a number of lines, ie: -A4) -B print a certain number of lines BEFORE the matching line. -C print a certain number of lines of CONTEXT (before & after) -e use this when you want to search for one of several expressions --color print the matching pattern in red 18

  19. Searching for something in a filegrep: useful options % grep -inw--color "thou" OldEnglish 10: Thou mayst have thought that thou’st 11: property wast thine own, although thou Find “thou” in the file “shakespeare”, case-insensitive but only whole words and, when you display the answer, show line numbers and colorthe matching patterns. 19

  20. Searching for something in a filegrep: useful options % grep -o "[a-z]*in[a-z]*.." helloworld.c include < main() printf(" Only print the words which contain the substring “in” (and print two extra characters after each such word). 20

  21. Searching for something in a filegrep: useful options %grep-w --color -e 'three$' -e 'four$' lewis.txt very large house with a housekeeper called Mrs. Macready and three shall be only a statue of a Faun in her horrible house until the four time of those four thrones at CairParavel). Once you were all four else--namely a little dwarf who stood with his back to it about four there's sugar, and some matches. And if someone will get two or three "Four thrones in CairParavel," said the Witch. "How if only three hill and came straight across and stood before Aslan. The three flashing so quickly that they looked like three knives and three 21

  22. Let’s summarize what we have learned

  23. Viewing Files cat <filename> - display a file on screen cat –n <filename> - display with line numbers more <filename> - to see a screenful at a time less <filename> - a better version of more (Because, in life, less is often better than more.) head <filename> - display the first 10 lines of a file. head –n n <filename> - displays the first n lines. tail <filename> - display the last 10 lines of a file. tail –nn <filename> - displays the last n lines. 23

  24. Managing Files and Directories

  25. File Analysis Commands

  26. Other Basic Commands

  27. More Advanced Commands

  28. Regarding C-shell commands • $#argv • $argv[$#argv] • set X = $< • set X = word • set X = $3:q • set T • unset T • @ X = $2 + $Y • if () then else if () then else endif • if (-z X) then • if (-e X) then • while () • foreach ($*)

  29. Summary of C-Shell Variables User created variables $myvar, $file1, etc. Keyword shell variables $PATH, $prompt, $HOME, etc. Have special meaning to the shell Positional parameters $1, $2, etc. Need to use shift if there are more than 9 Special parameters $* - All arguments as a single string $# - The number of command-line arguments $#X - The number of elements in array X $< - A line typed from the keyboard (or redirected from a file) $? - The exit status of the last command $?X-Test to see if variable X exists

  30. By now, you know all of these shell symbols

  31. Here are some others that you don’t need to learn… Not for the midterm But just because we won’t learn what these symbols do is not to say that you won’t need to learn what they are: Because you need to remember that they have special shell meanings and so they must be quoted when you want to pass them as-is.

  32. Here are some others that you don’t need to learn… Not for the midterm • Actually, this is useful, just not useful for our class. You use it if: - You execute a command that opens a new • window (such as running an editor), but • you want to still type in the old window • The reason that we don’t use it is because • Cygwin won’t make new windows anyway But just because we won’t learn what these symbols do is not to say that you won’t need to learn what they are: Because you need to remember that they have special shell meanings and so they must be quoted when you want to pass them as-is.

  33. Here are some others that you don’t need to learn… Not for the midterm You type “history” to: See the old command. Then type “!#” to rerun that command number.It has another way use: “!gr” will rerun the most recent command that began with “gr” - But be careful with this feature, that you don’t accidentally rerun the wrong command, possibly overwriting some file, etc. A final note: In most cases, it is quicker and faster to just use the “up arrow” key on you keyboard to browse recent commands. But just because we won’t learn what these symbols do is not to say that you won’t need to learn what they are: Because you need to remember that they have special shell meanings and so they must be quoted when you want to pass them as-is.

  34. Not for the midterm Some special keys that you can use at the command prompt in tcsh(Same keys work in the emacs editor!) Ctrl-K = Cut everything on the command- line beyond the cursor Ctrl-Y = Paste onto the command line at the cursor position Ctrl-A = Move cursor to the front Ctrl-E = Move cursor to the end Ctrl-P = Set the current command-line to the previous command Ctrl-N = Set the current command-line to the next command TAB = Filename completion

More Related