+ All Categories
Home > Documents > Introduction to UNIX (3)

Introduction to UNIX (3)

Date post: 12-Jan-2016
Category:
Upload: armani
View: 25 times
Download: 0 times
Share this document with a friend
Description:
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, - PowerPoint PPT Presentation
22
Introduction to UNIX (3) • C Shells – Start-up – Variables – Alias – Script control structure
Transcript
  • Introduction to UNIX (3)C Shells Start-upVariablesAliasScript control structure

  • Start-UpWhen a C shell is invoked, first execute commands in $HOME/.cshrc if it existsIf the C shell is a login shell,Execute commands in global login initialization file (e.g., /.login) if it existsExecute commands in $HOME/.login if it existsThen it displays its prompt and wait for user commands

  • .login FileTypically contains commands that set environment vars like $TERM or $PATHThose that needs to be set once (that are inherited by other shells) or terminal setting

    set term = vt100set path = ( . /usr/bin /usr/local/bin )stty erase ^? kill ^U intr ^C eof ^D set history = 40set prompt = ! %

  • .cshrc FileTypically contains commands that set common aliases or anything that applies to the current shell

    alias emacs /usr/bin/emacsalias h historyalias ll ls -l alias ls ls -Falias rm rm -i

  • VariablesCreating/Assigning Simple Local variables set {name [= word]}*% set flag% echo $flag

    % set color = red% echo $colorred% set display a list of all local variablescdpath /home/smooncolor redterm vt100user smoon

  • VariablesCreating/Assigning List Local variables set {name = ([word]*)}*% set colors = ( red yellow green )% echo $colorred yellow green% echo $colors[1]red% echo $#colors3% set colors = ( $colors blue )% echo $colorsred yellow green blue

  • Predefined Local Variables $< the next line of standard input $ argv a list of parameters ($1 = $argv[1])$ cwd the current working directory$ home shells 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 = $;lsalias rm rm -Ialias rm mv \!* ~/tombalias h historyalias 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 shells .cshrc file

  • HistoryKeeps a record of commands you enteredMay be edited or reexecuted later using ! % set prompt = \! %1 % echo FOOFOO 2 % set history = 40 .. Remember the last 40 commands403 % alias h history4 % h1 echo FOO2 set history = 40 3. alias h history5 % !1echo FOOFOOTC Shell has a more powerful editing features (^p, ^n)

  • Control StructuresSuitable for high-level programmingforeach name ( wordList ) commandListend Iterates each time with different value for name% cat foreach.csh#foreach color (red yellow green) echo one color is $colorend% foreach.cshone color is redone color is yellowone color is green%

  • Control Structuresgoto name name:Jump unconditionally% cat goto.csh#echo gotta jumpgoto endOfScriptecho I will never echo thisendOfScript:echo the end% goto.cshgotta jumpthe end%

  • Control StructuresIf (expr) command if (expr1) then list1switch (expr) else if (expr2) then case pattern: list2 list else breaksw list3 endif

    % cat if.csh#echo -n enter a number: set number = $ ksh.txt ^Z[1] + stopped man ksh | ul -tdumb > ksh.txt% bg %1man 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 %maman ksh | ul -tdumb > ksh.txt &

  • Built-in Commandssource: execute a script w/o invoking a subshellNormally, 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 .loginThe directory stackpushd [+number|name], popd [+number|name], dirs% pwd/home/smoon% pushd // ~% pwd/% popd~% pwd/home/smoon


Recommended