520 likes | 676 Views
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.
E N D
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
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
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)
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
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
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
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
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.
The tcsh startup files • Only if tcsh is your login shell • .chdir • Re-builds the directory stack buffer • .logout • Performs termination processes at exit
Topics • Common bash and tc shell features • History • Alias • Job control • Filename Substitution • Directory Stack Manipulation
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
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.
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
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
Common bash and tc features • Special tcsh aliases • precmd • Executes just before shell displays prompt • shell • Name of shell to process scripts without #!
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.
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
Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Command-line Editing • Spelling Correction
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.
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
Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Spelling Correction
Word Completion • The process that tcsh uses to complete filenames, commands, and variable names.
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
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
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
Topics • Redirecting Standard Error • Word Completion • Filename Completion • Tilde Completion • Command and Variable Completion • Spelling Correction
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
Topics • Variables • Variable substitution • String variables • Arrays of String variables • Numeric variables • Shell variables • Expressions
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
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)
Variable substitution • $ - used to identify variables • \$ - no substitution show $ • “ – substitution will always occur • ‘ – substitution will never occur
String Variables • tcsh uses explicit declaration …]$ set name = fred …]$ echo $name fred …]$ name = georgename: Command not found.
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 )
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
Arrays of String Variables • To reference a range of elements use the variable name and [ n1-n2 ] …~]$ echo $colors[2-3]b c
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
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
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)
Topics • Control Structures • Interrupt handling • Built-ins
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
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
if … then … endif • If (expression) thencommandendif
if … then … else … endif • If (expression) thencommandelsecommandendif
if … then … else if … endif • if (expression) thencommandelse if (expression) thencommandelsecommandendif
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
while … end • while (expression) exec if expressioncommand is trueend • tcsh does not support until.
switch … case … endsw • switch ( test-string) case pattern:commandsbreaksw default:commandsendsw • • •
Topics • Control Structures • Interrupt handling • Built-ins