Download presentation
Presentation is loading. Please wait.
Published byWillis Black Modified over 8 years ago
2
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell
3
2 © 2012 John Urrutia. All rights reserved. Topics Background Shell Scripts Entering and Leaving The TC shell Startup files
4
3 © 2012 John Urrutia. All rights reserved. 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
5
4 © 2012 John Urrutia. All rights reserved. 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
6
5 © 2012 John Urrutia. All rights reserved. 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)
7
6 © 2012 John Urrutia. All rights reserved. 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
8
7 © 2012 John Urrutia. All rights reserved. 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
9
8 © 2012 John Urrutia. All rights reserved. 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
10
9 © 2012 John Urrutia. All rights reserved. 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
11
10 © 2012 John Urrutia. All rights reserved. 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
12
11 © 2012 John Urrutia. All rights reserved. Only if tcsh is your login shell .chdir Re-builds the directory stack buffer .logout Performs termination processes at exit The tcsh startup files
13
12 © 2012 John Urrutia. All rights reserved. Topics Common bash and tc shell features History Alias Job control Filename Substitution Directory Stack Manipulation
14
13 © 2012 John Urrutia. All rights reserved. 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
15
14 © 2012 John Urrutia. All rights reserved. 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.
16
15 © 2012 John Urrutia. All rights reserved. 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
17
16 © 2012 John Urrutia. All rights reserved. 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
18
17 © 2012 John Urrutia. All rights reserved. Common bash and tc features Special tcsh aliases precmd Executes just before shell displays prompt shell Name of shell to process scripts without #!
19
18 © 2012 John Urrutia. All rights reserved. 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.
20
19 © 2012 John Urrutia. All rights reserved. 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
21
20 © 2012 John Urrutia. All rights reserved. Topics Redirecting Standard Error Word Completion Filename Completion Tilde Completion Command and Variable Completion Command-line Editing Spelling Correction
22
21 © 2012 John Urrutia. All rights reserved. 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.
23
22 © 2012 John Urrutia. All rights reserved. Redirecting standard error …]$ cat x cat: No such file or directory …]$ cat y This 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
24
23 © 2012 John Urrutia. All rights reserved. Topics Redirecting Standard Error Word Completion Filename Completion Tilde Completion Command and Variable Completion Spelling Correction
25
24 © 2012 John Urrutia. All rights reserved. Word Completion The process that tcsh uses to complete filenames, commands, and variable names.
26
25 © 2012 John Urrutia. All rights reserved. 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
27
26 © 2012 John Urrutia. All rights reserved. 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
28
27 © 2012 John Urrutia. All rights reserved. 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
29
28 © 2012 John Urrutia. All rights reserved. Topics Redirecting Standard Error Word Completion Filename Completion Tilde Completion Command and Variable Completion Spelling Correction
30
29 © 2012 John Urrutia. All rights reserved. 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
31
30 © 2012 John Urrutia. All rights reserved. Topics Variables Variable substitution String variables Arrays of String variables Numeric variables Shell variables Expressions
32
31 © 2012 John Urrutia. All rights reserved. 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
33
32 © 2012 John Urrutia. All rights reserved. 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)
34
33 © 2012 John Urrutia. All rights reserved. Variable substitution $ - used to identify variables \$ - no substitution show $ “ – substitution will always occur ‘ – substitution will never occur
35
34 © 2012 John Urrutia. All rights reserved. String Variables tcsh uses explicit declaration …]$ set name = fred …]$ echo $name fred …]$ name = george name: Command not found.
36
35 © 2012 John Urrutia. All rights reserved. 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 )
37
36 © 2012 John Urrutia. All rights reserved. Arrays of String Variables To reference the entire array use just the name …~]$ echo $colors a b c d To reference individual elements use the name and […] …~]$ echo $colors[2] b
38
37 © 2012 John Urrutia. All rights reserved. Arrays of String Variables To reference a range of elements use the variable name and [ n1-n2 ] …~]$ echo $colors[2-3] b c
39
38 © 2012 John Urrutia. All rights reserved. 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
40
39 © 2012 John Urrutia. All rights reserved. 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
41
40 © 2012 John Urrutia. All rights reserved. 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)
42
41 © 2012 John Urrutia. All rights reserved. Topics Control Structures Interrupt handling Built-ins
43
42 © 2012 John Urrutia. All rights reserved. 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
44
43 © 2012 John Urrutia. All rights reserved. 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
45
44 © 2012 John Urrutia. All rights reserved. if … then … endif If ( expression ) then command endif
46
45 © 2012 John Urrutia. All rights reserved. If ( expression ) then command else command endif if … then … else … endif
47
46 © 2012 John Urrutia. All rights reserved. if ( expression ) then command else if ( expression ) then command else command endif if … then … else if … endif
48
47 © 2012 John Urrutia. All rights reserved. foreach loop-index ( argument-list ) command end Substitutes the argument-list in order to the loop-index foreach x (1 a 2 b 3 c) echo $x end foreach … end
49
48 © 2012 John Urrutia. All rights reserved. while … end while ( expression ) exec if expression command is true end tcsh does not support until.
50
49 © 2012 John Urrutia. All rights reserved. switch … case … endsw switch ( test-string ) case pattern : commands breaksw default: commands endsw
51
50 © 2012 John Urrutia. All rights reserved. Topics Control Structures Interrupt handling Built-ins
52
51 © 2012 John Urrutia. All rights reserved. Builtins Tcsh provides a set of “internal” utilities called builtins. Builtins run under the same PID as the shell The builtins command will display all tcsh builtin commands
53
52 © 2012 John Urrutia. All rights reserved. The tcsh Builtins : @aliasallocbg bindkeybreakbreakswbuiltinscase cdchdircompletecontinuedefault dirsecho echotc elseend endifendswevalexecexit fgfiletestforeach glob goto hashstathistoryhupifjobs killlimitlogloginlogout ls-fnicenohupnotifyonintr popdprintenvpushdrehash repeat sched setsetenvsettcsetty shift source stop suspend switch telltctimeumaskunalias uncompleteunhashunlimitunsetunsetenv waitwherewhichwhile
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.