1 / 22

Introduction to UNIX (3)

Introduction to UNIX (3). C Shells Start-up Variables Alias Script control structure. Start-Up. When a C shell is invoked, first execute commands in $HOME/.cshrc if it exists If the C shell is a login shell,

armani
Download Presentation

Introduction to UNIX (3)

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. Introduction to UNIX (3) • C Shells • Start-up • Variables • Alias • Script control structure

  2. Start-Up • When a C shell is invoked, first execute commands in $HOME/.cshrc if it exists • If the C shell is a login shell, • Execute commands in global login initialization file (e.g., /.login) if it exists • Execute commands in $HOME/.login if it exists • Then it displays its prompt and wait for user commands

  3. .login File • Typically contains commands that set environment vars like $TERM or $PATH • Those that needs to be set once (that are inherited by other shells) or terminal setting set term = vt100 set path = ( . /usr/bin /usr/local/bin ) stty erase “^?” kill “^U” intr “^C” eof “^D” set history = 40 set prompt = “! % “

  4. .cshrc File • Typically contains commands that set common aliases or anything that applies to the current shell alias emacs /usr/bin/emacs alias h history alias ll ls -l alias ls ls -F alias rm rm -i

  5. Variables • Creating/Assigning Simple Local variables set {name [= word]}* % set flag % echo $flag % set color = red % echo $color red % set ………… display a list of all local variables cdpath /home/smoon color red … term vt100 user smoon

  6. Variables • Creating/Assigning List Local variables set {name = ([word]*)}* % set colors = ( red yellow green ) % echo $color red yellow green % echo $colors[1] red % echo $#colors 3 % set colors = ( $colors blue ) % echo $colors red yellow green blue

  7. Predefined Local Variables $< the next line of standard input $ argv a list of parameters ($1 = $argv[1]) $ cwd the current working directory $ home shell’s home directory $ noclobber prevents existing files from being overriden by > $ noglob prevents wildcard expandsion $ path used by shell for locating executable files $ prompt shell prompt % cat var.csh # echo -n “please enter your name: “ set name = $< echo hi $name, your current directory is $cwd % var.csh please enter your name: Moon hi Moon, your current directory is /home/smoon

  8. Environment Variables • Creating/Assigning environment variables setenv name word % setenv TERM vt100 % echo $TERM vt100 • Predefined environment variables • $LOGNAME the shell owner’s user ID

  9. Expressions • Supports string, arithmetic, file-oriented • String operators: ==, !=, =~, !~ % cat expr1.csh # echo -n “Do you like C shell?” set reply = $< #get a line of input if ($reply == “yes”) echo you entered yes • Arithmetic operators: -, !, *, /, %, <<, … % cat expr2.csh # set a = 3 set b = 5 if ($a > 2 && $b > 4) echo expression seems to be working

  10. Assigning expression result • You cannot use the set command; use @ • @ variable = expression % set a = 2 * 2 setL Syntax error % @ a = 2 * 2 % echo $a 4 % @ a = $a + $a % echo $a 8 • File-oriented: -option fileName • e: exists, r: has read permission, f: regular file, d: directory, o: owned by the same user …..

  11. Aliases • Customize your own commands via alias alias [word [string]] % alias dir `ls -aF` % dir a.c b.c cc.c UNIX1.ppt UNIX2.ppt % alias dir ls -aF % alias rm `rm -I` % rm a.c delete a.c? No % unalias rm …. Removes all aliases of rm

  12. Useful Aliases • Many people keep the following in .cshrc alias cd ‘cd \!*; set prompt = “$cwd \!>”;ls’ alias rm ‘rm -I’ alias rm ‘mv \!* ~/tomb’ alias h history alias ll ‘ls -l’ % alias cd ‘cd \!*; set prompt = “$cwd \!>”;ls’ % cd . A.c b.c cc.c UNIX1.ppt UNIX2.ppt /home/smoon 2> • To make an alias available to a subshell, place it in the shell’s “.cshrc” file

  13. History • Keeps a record of commands you entered • May be edited or reexecuted later using ! % set prompt = ‘\! %’ 1 % echo FOO FOO 2 % set history = 40 …….. Remember the last 40 commands 40 3 % alias h history 4 % h 1 echo FOO 2 set history = 40 3. alias h history 5 % !1 echo FOO FOO • TC Shell has a more powerful editing features (^p, ^n)

  14. Control Structures • Suitable for high-level programming foreach name ( wordList ) commandList end • Iterates each time with different value for name % cat foreach.csh # foreach color (red yellow green) echo one color is $color end % foreach.csh one color is red one color is yellow one color is green %

  15. Control Structures goto name name: • Jump unconditionally % cat goto.csh # echo gotta jump goto endOfScript echo I will never echo this endOfScript: echo the end % goto.csh gotta jump the end %

  16. Control Structures If (expr) command if (expr1) then list1 switch (expr) else if (expr2) then case pattern: list2 list else breaksw list3 … endif % cat if.csh # echo -n ‘enter a number: ‘ set number = $< if ($number < 0) then echo negative else if ($number = 0) then echo zero else echo positive endif

  17. Control Structures onintr label % cat onintr.csh # onintr controlC while (1) echo infinite loop sleep 2 end controlC: echo control C detected % onintr.csh infinite loop infinite loop ^C control C detected %

  18. Control Structures repeat expr command % repeat 2 echo hi there hi there hi there % while ( expr ) commandlist end % cat multi.csh # set x = 1 while ($x <= $1) echo $x @ x ++ end

  19. Example - junk utility Junk -lp {fileName}* • Replacement of rm; it moves the files into ~/.junk, not removing them; if no .junk, create one. -l lists .junk; -p purges .junk #! /bin/csh case “-l”: set fileList = () # list all specified files set listFlag = 1 set listFlag = 0 # set to 1 if “-l” option breaksw set purgeFlag = 0 # 1 if “-p” option case “-*: set fileFlag = 0 # 1 if at least one file is there echo $arg is an illegal option set junk = ~/.junk goto error breaksw foreach arg ($*) default: switch ($arg) set fileFlag = 1 case “-p”: set fileList = ($fileList $arg) set purgeFlag=1 breaksw breaksw endsw end

  20. Example - junk utility @ total = $listFlag+$purgeFlag+$fileFlag if( $total != 1 ) goto error if( !( -e $junk) ) then if( $filrFlag ) then ‘mkdir’ $junk ‘mv’ $fileList $junk endif exit 0 endif if( $listFlag ) then exit 0 ‘ls’ -lgF $junk error: exit 0 cat << ENDOFTEXT endif Dear $USER, the usage of junk is: junk -p means “purge all files” if( $purgeFlag ) then junk -l means “list junked files” ‘rm’ $junk/* jubk < list of files > exit 0 ENDOFTEXT endif exit 1

  21. Job Control • jobs: displays all of the shell’s jobs • bg %num: resume the specified job in background % man ksh | ul -tdumb > ksh.txt ^Z [1] + stopped man ksh | ul -tdumb > ksh.txt % bg %1 man ksh | ul -tdumb > ksh.txt & • fg %num: resume the specified job in foreground % sleep 1000 & % man ksh | ul -tdumb > ksh.txt & % jobs [2] [1] % fg %ma man ksh | ul -tdumb > ksh.txt &

  22. Built-in Commands • source: execute a script w/o invoking a subshell • Normally, aliases in the script cannot affect the parent shell % vi .login .. Assume we do some editing; normally we have to re-login to see the effect % source .login • The directory stack • pushd [+number|name], popd [+number|name], dirs % pwd /home/smoon % pushd / / ~ % pwd / % popd ~ % pwd /home/smoon

More Related