Popular in Course
Popular in ComputerScienence
This 40 page Class Notes was uploaded by Mallie Crist on Monday September 21, 2015. The Class Notes belongs to CSC 3320 at Georgia State University taught by Staff in Fall. Since its upload, it has received 49 views. For similar materials see /class/209896/csc-3320-georgia-state-university in ComputerScienence at Georgia State University.
Reviews for SYSTEM
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 09/21/15
Ch 6 C Shell C shell supports all the core features discussed in Ch 3 plus the following several ways to set and access variables builtin programming language conditional branching looping etc command customization using aliases access to previous commands using history mechanism advanced job control several new builtin and enhancements to existing commands Startup The C shell is located in bincsh usually it is a C program When started as a login shell the startup sequence is as follows Execute commands in HOME cshrc if it exists Then execute commands in global login initialization file etclogin if it exists Then execute commands in HOMElogin A sample CShI C le The cshrc file usually contains commands that set aliases or anything else that applies only to the current shell rc stands for run commands alias h history alias ll ls l alias ls ls F alias rm rm i The login file usually contains commands to set the terminal set environment variables etc echo n quotEnter your terminal type default is vt100 quot set termtype lt set term vt100 if quotermtypequot quotquot set term quottermtypequot unset termtype set path bin usrbin usrlocalbin stty erase quot quot kill quot Uquot intr quot Cquot eof quot Dquot crt crterase set cdpath quot set history 40 set notify set prompt quot Z quot set savhist 32 set name value if name is not specified null string is assigned value is omitted all variables are listed ifnww value could be a list within parentheses Some examples set flag set color red set name quotGraham Glassquot Two new access methods 1 name 2 name set verb sing echo I like verbing I like singing use this if name immediately followed by string replaced by 1 if name is set and 0 otherwise The following is a script illustrating the set flag abc if f1ag then echo flag is set endif 2 access method List Variables set colors red green blue echo colors1 red echo colors23 green blue echo colors red green blue echo colors 3 1 Building lists set colors4 yellow Not ok since 4 does not exists set colors colors pink echo colors red green blue pink set colors4 yellow set colors colors black forgot echo colors sets only first value ok since 4 exists red set s1 a b c set s2 b c f set s3 s1 82 echo s3 a b c b c f lt argv cwd home ignoreeof noclobber noglob path prompt savehist shell status ISorne Prede ned kxz var esl The next line of standard input A list containing all of the positional parameters argv1 1 etc Current working directory Shell s home directory Prevents shell from terminating when CTRLD is pressed Prevents existing files from being overwritten by gt and nonexisting files from being appended using gtgt Prevents wildcard expansion Used to locate executables list of directories extracted from PATH environment variable The shell prompt Number of commands to save in the history file Full path name of the login shell exit code of the last command bincsh echo n quotplease enter your name quot set name lt echo ho name your current directory is ch Environment Variables setenv name word X setenv TERM vt100 no list values allowed predefined environment variable LOGNAME shell owner s user id lString Arithmetic and Fileoriented Expressions String expressions s1 s1 s1 s1 Note if s2 true s2 true s2 Like s2 Like if if either side for comparison exactly equal unequal except rhs may contain wildcards except rhs may contain wildcards is a list the first item is used bincsh echo n quotdo you like the C shell quot set reply lt if reply quotyesquot then echo you entered yes else if reply quot y then echo I assume you mean yes else echo not yes endif Arithmetic expressions P 189 Table lists all operators similar to C language amp ampamp I II lt gt ltlt gtgt operations must be surrounded by so that they are not misinterpreted as shell operators ex if a gt b II a lt c then Do not use the set command to assign an expression to a variable Instead use the command as follows by itself lists all shell variables a22 echo a 4 Z a 1 echo a 5 Fileoriented Expressions option fileName Option I CONS H39aN Meaning Shell has read permission for fileName Shell has write permission for fileName Shell has execute permission for fileName fileName exists fileName is owned by the same user as that of the shell process fileName exists and is 0 bytes in size fileName is a regular file not dir special fileName is a directory The following is a script called 63 csh bincsh echo n quotEnter name of file you wish to erase quot set fileName lt if w fileName then echo you do not have permission to erase the file else rm fileName endif X 63 csh Enter name of file you wish to erase you do not have permission to erase the file lFiIename completionl set filec after setting filec you do not have to type the entire file names instead you may type esc key after initial few letters if a unique match exists the file name is automatically completed otherwise type to view all matching files In tcsh the shell displays all matching files when esc is pressed alias word string unalias pattern Some useful aliases alias ls ls F alias rm rm i alias h history alias c clear alias ls l ls l alias ll ls l alias dir ls History Mechanism The C shell keeps a record of the commands entered from the keyboard These can be recalled edited and executed at a later stage The meta character gives access to the history of commands Numbered commands set prompt Z set prompt to include commandevent number set history 40 remember last 40 commands set savehist 32 save last 32 commands between sessions alias h history h is an alias for history which lists the history Command Reexecution replaced with text of last command num replaced with text of command numbered num prefix replaced with the most recent command which started with prefix substring replaced with text of the most recent command containing substring Accessing pieces of a previous command These modifiers can be placed immediately after event specifier 0 first token number number1st token startend start1 through end1 tokens 39 first token colon optional z last token colon optional second through last token colon optional 48 echo I like horseback riding 49 Z O 1 2 4 echo I like riding I like riding 50 echo 481 echo I like horseback riding I like horseback riding 20 Accessing portions of filenames If the token extracted is a file name the various parts of the filename can be extracted as follows let filename be usrincludestdio h h head usrinclude r root usrincludestdio e extension h t tail stdioh 53 ls usrincludestdio h usrincludestdioh 54 X echo 53 1h usrinclude History substitution eventssssttt 21 Control Structures If a control structure is entered on the keyboard on several lines the C shell prompts with a for each subsequent line foreachend foreach name wordList commandlist breakcontinue can be used as commands end example foreach color red blue green echo color end 22 goto name example echo gotta jump goto endOfScript echo I will never echo this endOfScript echo the end 23 if then 8188 endif if expr command X if 5 gt 3 echo five is greater than 3 if exprl then commandsl else if expr2 then commands2 else commandsS endif bincsh echo n l39enter a number set number lt if number lt 0 then echo negative else if number 0 then echo zero else echo positive endif 24 onintr label bincsh onintr controlC while 1 echo infinite loop sleep 2 end controlC echo control C detected repeat expr command o repeat 2 echo Hi there switch case endsw switch expr case patterni commandsl breaksw case pattern2 case pattern3 commandsZ breaksw default defaultCommands endsw 26 bincsh echo menu test program set stop 0 while stop 0 cat ltlt ENDDFMENU 1 print the date 23 print the current working directory 2 exit ENDDFMENU echo echo n your choice set reply lt switch reply case quot1quot date breaksw case quot2quot case quot3quot pwd breaksw case quot4quot set stop 1 breaksw default echo illegal choice breaksw endsw end while expr commandList end bincsh set x 1 while x lt 1 set y 1 while y lt 1 v x echo n v y end II ll x end 60 lt X multi csh 4 1 2 3 2 4 6 3 6 9 4 8 12 41 set outer loop value outer loop set inner loop value inner loop calculate entry display entry update inner loop counter newline update outer loop counter 28 bincsh 92591 set fileList junk script author Graham Glass Initialize variables lSample Project junkcshl a list of all specified files set listFlag 0 set to 1 if l option is specified set purgeFlag 0 1 if p option is specified set fileFlag 0 1 if at least one file is specified set junk quotjunk the junk directory 29 Parse command line foreach arg switch arg case quotpquot set purgeFlag 1 breaksw case l39lquot set listFlag 1 breaksw case echo arg is an illegal option goto error breaksw default set fileFlag 1 set fileList fileList arg append to list breaksw endsw end 30 Check for too many options total 1istFlag purgeF1ag fi1eF1ag if tota1 1 goto error If junk directory doesn t exist create it if e junk then mkdir junk endif Process options if 1istF1ag then 1s 1gF junk list junk directory exit 0 endif if purgeF1ag then rm junk remove contents of junk directory exit 0 endif if fileFlag then mv fileList junk move files to junk directory exit 0 endif exit 0 Display error message and quit error cat ltlt ENDDFTEXT Dear USER the usage of junk is as follows junk p means l39purge all filesquot junk l means l39list junked filesquot junk ltlist of filesgt to junk them ENDDFTEXT exit 1 32 Enhancements Command reexecution A shortcut sss ttt would replace sss in the previous command with ttt Metacharacters X cp usrincludestdiosignalh copies two files in one command 33 Filename Substitution Disabling filename substitution set noglob echo a 3 Nomatch situations echo a p p1c p2c echo a b echo no match set nonomatch echo a b a b causes wildcard to be disabled if no match 34 Protecting files from accidental overwrites X set noclobber X cat ac gt bc errors file exists Redirecting the standard error channel use gtamp and gtgtamp X cc ac gtamp errors error messages sent to file To send std output and error along pipeline use amp Jok3ontrol jobs l lists all jobs background processes currently active specifying a job Zinteger using job number PID Zprefix jobs beginning with prefix last referenced job XX same as second to last referenced job bg Zjob places job in the background used with suspended jobs if no job specified last referenced job is used fg xjob brings job to foreground if no job specified last referenced job is used 36 X stop Zjob suspends job if no job specified last referenced job is used suspend suspends the shell that invokes the command useful only when shell was invoked as a child process nice integer command sets the run level priority for the command larger the number lower the priority default 4 negative numbers can be used by super user only Terminating the login shell X set ignoreeof prevents D logout exit logout file is executed by login shell when it terminates 38 Some builtin commands X chdir works same as cd glob works same as echo except it NULL terminates its output useful in C programs X source file executes commands stored in file Directory Stack pushd dir same as cd except current dir is pushed on stack popd read details 39 Hash table of executables in PATH directories to speed up Whenever a new executable is added to a directory in PATH use rehash to reconstruct hash table unhash disables hash table slows search process 40