1 / 52

Chapter 09

Chapter 09 . The TC Shell. Topics. Background Shell Scripts Entering and Leaving The TC shell Startup files. Background. Originally developed at Berkeley. csh – C shell tcsh – Like bash, incorporates the best of other C shells. tcsh and bash are very similar… However.

ashby
Download Presentation

Chapter 09

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. Chapter 09 The TC Shell

  2. Topics • Background • Shell Scripts • Entering and Leaving The TC shell • Startup files

  3. Background • Originally developed at Berkeley. • csh – C shell • tcsh – Like bash, incorporates the best of other C shells. • tcsh and bash are very similar… However

  4. Background • tcsh – ignores spaces in assignment but bash will give you an error • set myvar = value (OK in tcsh) • set myvar=value (OK in bash) • Referencing an unset variable creates an error in tcsh but not in bash

  5. Shell Scripts • Shell scripts are run implicitly by the shell designated in the first line. • Scripts can be run by other shells if they are explicitly invoked. • tcsh myscript #!/bin/bash (this is ignored)

  6. Entering The TC shell • To enter the tcsh from the command line • tcsh • If you want tcsh or any other shell set as your permanent shell use • chsh

  7. Leaving the tcsh Shell • The ignoreeof variable effects the exit • If set CONTROL-D will issue a prompt on how to exit • The exit command will always work • logout will exit only from the login shell

  8. The tcsh startup files • On entry to tcsh executes • /etc/csh.cshrc • Sets prompt and file creation permissions • /etc/csh.login • Sets the PATH, HOSTNAME • Sets the delete key • Sets the user profile and executes scripts

  9. The tcsh startup files • .tcshrc or .cshrc • Establishes local shell variables • Executes for each new shell • Only if tcsh is your login shell • .history • Re-builds the history buffer

  10. The tcsh startup files • Only if tcsh is your login shell • .history • Re-builds the history buffer • .login • Executes once at the begining of a session • Used to set environmental variables, mail, terminal, etc.

  11. The tcsh startup files • Only if tcsh is your login shell • .chdir • Re-builds the directory stack buffer • .logout • Performs termination processes at exit

  12. Topics • Common bash and tc shell features • History • Alias • Job control • Filename Substitution • Directory Stack Manipulation

  13. Common bash and tc features • History • Event control works the same but has additional event modifiers • u, l, a • u – First lowercase letter set to upper case • l – First uppercase letter set to lower case • a – Apply globally to command line

  14. Common bash and tc features • History • set history=10 • Sets the number of commands to store • set savehist=5 • Sets the number of commands to save across sessions • set histlit • When set displays literal history without command line expansion.

  15. Common bash and tc features • Alias works the same but has syntax differences. • bash • alias ls=”ls –lf” • tcsh • alias ls ls –lf • Special tcsh aliases

  16. Common bash and tc features • Special tcsh aliases • beepcmd • Replaces that annoying beep! • cwdcmd • Executes when you change the working directory • periodic • Executes after a specified number of minutes

  17. Common bash and tc features • Special tcsh aliases • precmd • Executes just before shell displays prompt • shell • Name of shell to process scripts without #!

  18. Common bash and tc features • Job Control • Is very similar to bash • tcsh will display all process id’s used by a background job whereas bash displays only the last one.

  19. Common bash and tc features • Filename Substitution • Uses the same wildcards  ? * [ ] • Setting the noglob variables suppresses all filename substitution • Directory stack manipulation • Works the same way as bash

  20. Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Command-line Editing • Spelling Correction

  21. Redirecting standard error • tcsh combines both stdout and stderr through the use of the >& symbols • tcsh does not provide a simple way of redirecting just the error output but a good work around follows.

  22. Redirecting standard error …]$ cat xcat: No such file or directory …]$ cat yThis is what’s in y. …]$ (cat x y > stdfile) >& errfile …]$ cat stdfile This is what’s in y. …]$ cat errfile cat: No such file or directory

  23. Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Spelling Correction

  24. Word Completion • The process that tcsh uses to complete filenames, commands, and variable names.

  25. Filename Completion • Any filename argument will be completed by pressing the tab key. • If the argument is unique it is replaced • If not a short beep is heard. • If the filename is not unique • You may continue to type until it is. • Pressing Ctrl-D • This displays all matching filenames

  26. Tilde Completion • If the first character is a ~ the shell attempts to expand it to a user name when you enter a tab. If a directory by the username exists a / is appended. …]$ cd ~fe(tab) …]$ cd ~fester/ …]$ pwd /home/fester  tab causes expansion

  27. Command and Variable Completion • Works the same way as filename completion • It searches the $PATH variable for command names. • The context of the ambiguous element determines if it is a command or variable

  28. Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Spelling Correction

  29. Spelling Correction • tcsh will attempt to correct spelling errors in commands • You must set the correct shell variable • cmd – correct only the command • all – correct the entire command line • complete – correct like filename completion • Use META-s to correct command • Use META-$ to correct the line

  30. Topics • Variables • Variable substitution • String variables • Arrays of String variables • Numeric variables • Shell variables • Expressions

  31. Variables • tcsh stores all variables as strings • Variable names are limited to 30 chr. • Must be from A – Z, a – z, 0 – 9, or _ • Cannot start with a numeral

  32. Variables • Three builtins to declare display, & assign variable values • set – declares local non-numeric • @ - declares local numeric variables • setenv – declares environmental variables (similar to using export)

  33. Variable substitution • $ - used to identify variables • \$ - no substitution show $ • “ – substitution will always occur • ‘ – substitution will never occur

  34. String Variables • tcsh uses explicit declaration …]$ set name = fred …]$ echo $name fred …]$ name = georgename: Command not found.

  35. Arrays of String Variables • An array is a set of elements that can be accessed individually. • You must declare the array before you use it. • The actual number of elements are fixed at declaration …~]$ set colors = ( a b c d )

  36. Arrays of String Variables • To reference the entire array use just the name …~]$ echo $colorsa b c d • To reference individual elements use the name and […] …~]$ echo $colors[2]b

  37. Arrays of String Variables • To reference a range of elements use the variable name and [ n1-n2 ] …~]$ echo $colors[2-3]b c

  38. Numeric Variables • The @ builtin will assign values to numeric variables. • You can perform arithmetic operations on numeric variables by using the C++ operators • =, +=, -=, *=, /=, %= …~]$ @ mynum += 10

  39. Array of Numeric Variables • You must use the set builtin to declare a numeric array before you use the @ to assign array values. …~]$ set ages=(0 0 0 0 0) …~]$ @ ages[2]=15 …~]$ @ ages[3]=($ages[2] + 4) …~]$ echo $ages[3] 19

  40. Special Variable Forms • The number of elements in an array • $#variable-name • Returns 1 if variable is set or 0 it isn’t • $?variable-name • Reading user input using set • set input_line = “$<“ • Set input_word = $< (space delimited)

  41. Topics • Control Structures • Interrupt handling • Built-ins

  42. Control Structure Differences • if (expression) simple-command • if ( $#argv == 0 ) echo “No Arguments” No Arguments • if ( -d MyDir ) echo “ This is a directory” This is a directory • if ( -d MyDir ) filetest –A: MyDir Wed Nov 14 04:03:10 2001

  43. Control Structure Differences • Label: – Labels are identifiers that act as branch points in a script. • goto Label: –Transfers control directly to the label • onintr Label: – Transfers control directly to the label when interrupt key is pressed

  44. if … then … endif • If (expression) thencommandendif

  45. if … then … else … endif • If (expression) thencommandelsecommandendif

  46. if … then … else if … endif • if (expression) thencommandelse if (expression) thencommandelsecommandendif

  47. foreach … end • foreach loop-index (argument-list)commandend • Substitutes the argument-list in order to the loop-index • foreach x (1 a 2 b 3 c) echo $x end

  48. while … end • while (expression) exec if expressioncommand is trueend • tcsh does not support until.

  49. switch … case … endsw • switch ( test-string) case pattern:commandsbreaksw default:commandsendsw • • •

  50. Topics • Control Structures • Interrupt handling • Built-ins

More Related