Presentation is loading. Please wait.

Presentation is loading. Please wait.

2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani Lecture 4.

Similar presentations


Presentation on theme: "2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani Lecture 4."— Presentation transcript:

1 2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani email: lahmani@cs.nyu.edu Lecture 4

2 2000 Copyrigths Danielle S. Lahmani Overview shell core functionality /bin/sh /bin/ksh /bin/csh

3 2000 Copyrigths Danielle S. Lahmani Shell Core Features Simple and complex commands redirection of input/output pipes wildcards command substitution background processes shell variables here documents built-in cmds programming constructs

4 2000 Copyrigths Danielle S. Lahmani The Korn Shell: /bin/ksh Supports all features described in the Bourne shell (/bin/sh) Alias mechanism History mechanism for access of previous commands Functions Enhanced job control Arithmetic Tilde substitution

5 2000 Copyrigths Danielle S. Lahmani The Korn shell: /bin/ksh STARTUP FILES: –/etc/profile –$HOME/.profile ALIAS: –alias [-tx] [word[=string]] –alias -x : to export alias to child shell –unalias aliasname: to remove an alias

6 2000 Copyrigths Danielle S. Lahmani /bin/ksh: History Mechanism Numbered commands $ PS1='!!’ /* set prompt to contains a ! */ $HISTSIZE default is 128 using the built-in vi editor –declare VISUAL=vi or EDITOR=vi –to edit current line, press ESC key to enter the editor –vi cmds to edit line, when done, press ESC key again, –additional movement: cursor up (k or - ) cursor down (j or +) –additional searching /string or ?string : searches backward and forward through history, respectively.

7 2000 Copyrigths Danielle S. Lahmani /bin/ksh (continued) ARITHMETIC: Using let expression TILDE SUBSTITUTION –~$HOME –~userhome directory of user –~/pathname$HOME/pathname –~+$PWD –~-$OLDPWD

8 2000 Copyrigths Danielle S. Lahmani /bin/ksh: FUNCTIONS Allows functions that may be invoked as shell commands function name { list of commands } or name() { list of commands }

9 2000 Copyrigths Danielle S. Lahmani /bin/ksh: Functions (continued) can use parameters returning from a function local variable using typeset functions can be recursive

10 2000 Copyrigths Danielle S. Lahmani /bin/ksh: ENHANCED JOB CONTROL jobslist your jobs bgplaces a specified job in the background fgplaces a specified job in the foreground killsends an arbitrary signal to a process or job ^zto stop a foreground job stop to suspend a background job

11 2000 Copyrigths Danielle S. Lahmani /bin/ksh: coprocess PIPES |& operator supports a simple form of concurrent processing cmd |& cmd run as a background process whose standard input and output channels are connected to the original parent shell via a two way pipe.

12 2000 Copyrigths Danielle S. Lahmani The C Shell: /bin/csh Supports all features described in the Bourne shell simple and list variables Alias mechanism with arguments History mechanism for access of previous commands Enhanced job control Arithmetic directory stack

13 2000 Copyrigths Danielle S. Lahmani /bin/csh: STARTUP FILES $HOME/.cshrc /etc/.login $HOME/.login

14 2000 Copyrigths Danielle S. Lahmani /bin/csh: simple variables set { name [=word]} * access –$(name) –${name} –${?name}replaced by 1 if name is set, 0 otherwise

15 2000 Copyrigths Danielle S. Lahmani /bin/csh: list variables Variables to which you can assign a list of values set { name = ( {word}*) } * Access: $name[selector] ${name[selector]}selector can be a range $#name ${#name}number of elements in name

16 2000 Copyrigths Danielle S. Lahmani /bin/csh: predefined local variables $< the next line of standard input fully quoted $noclobberprevents existing files from being overridden by >, and non-existent files from being appended to by >> $noglobprevents wildcard expansion

17 2000 Copyrigths Danielle S. Lahmani /bin/csh: methods for accessing C shell arguments $argva list that contains all of the positional parameters, same as $* $argv[1]is equal to $1 $#argvnumber of arguments $argv[1-n] arguments 1 through n $argv[0] illegal, must use $0 $argv[$#argv] last argument

18 2000 Copyrigths Danielle S. Lahmani /bin/csh:file inquiry operator -option filename operatormeaning rread access wwrite access xexecute access eexistence oownership zzero length fplain file ddirectory

19 2000 Copyrigths Danielle S. Lahmani /bin/csh: ARITHMETIC EXPRESSION Assignment of results of an expression: Set variable to expression @ variable op expression @ variable[index] op expression example: @ x = 2 + 3 %echo $x 5

20 2000 Copyrigths Danielle S. Lahmani /bin/csh:ALIAS alias [word[string]] unalias to share an alias with other subshells, place it under.cshrc

21 2000 Copyrigths Danielle S. Lahmani ARGUMENTS IN ALIASES C shell assumes that all arguments come at the end of the alias definition, unless you specify otherwise –Example % alias cx chmod +x –% cx * # makes files in all current directory executable –Two common arguments designations to pass arguments to aliases

22 2000 Copyrigths Danielle S. Lahmani /bin/csh alias example reference: The Unix C shell field Guide G, Anderson P. Anderson find requires a single argument, we must escape ! to suppress interpretation from the C shell %alias loc 'find ~ -name \!^ - print' %loc ttydoc77 % alias ldir 'ls -l \!* | grep "^d"' % ldir../utils /usr/bin

23 2000 Copyrigths Danielle S. Lahmani /bin/csh: history mechanism For numbered commands –set prompt = '\! %’ storage of commands –set history = 40size of history file –set savehist=32save 32 commands across sessions

24 2000 Copyrigths Danielle S. Lahmani /bin/csh: hist mech (continued) command reexecution –!!reexecute previous command –!number –!prefix –!?substring command editing –% !even :s/pat1/pat2 substitute pat1 with pat2 –%^pat1^pat2reexecution of previous command substituting pat1 with pat2

25 2000 Copyrigths Danielle S. Lahmani ACCESS PORTION OF A FILENAME Modifierpart of fileportion of filename returned :hheadfilename minus trailing path :rrootfilename minus trailing *.suffix :eextensiontrailing.* suffix :ttailfilename minus leading dir path

26 2000 Copyrigths Danielle S. Lahmani /bin/csh: CONTROL STRUCTURES foreach ……. end if then else endif switch case … endsw while … end onintr [-l label] allows to specify a label to jump to when shell receives a SIGINT

27 2000 Copyrigths Danielle S. Lahmani /bin/csh: PIPING Process1 |& process2 Redirects standard output and standard error from process 1 to process 2

28 2000 Copyrigths Danielle S. Lahmani /bin/csh:MULTIPLE REDIRECTION Cmd >& filesend both standard output and standard error to file Cmd >&! file same as above, even in noclobber is set Cmd >>& file append standard error and standard output to end of file Cmd1 | & cmd2 pipe standard error together with standard output (cmd > file1) >& file2 send standard output to file1; send standard error to file2

29 2000 Copyrigths Danielle S. Lahmani /bin/csh: JOB CONTROL Same as ksh, plus stopsuspend background process suspendsuspend foreground process notifynotify when current job changes status

30 2000 Copyrigths Danielle S. Lahmani /bin/csh: DIRECTORY STACK pushd [+number|name] pushd pushes the directory onto the directory stack popd [+number] popdpops a directory from the directory stack dirs: lists the current directory stack.

31 2000 Copyrigths Danielle S. Lahmani Effective and efficient shell prog reference: Unix shell Tutorial G.Snyder J.R. Mashey Bell Labs Minimize number of processes generated Find out which cmds are built-in and which are not.  Number of data bytes accessed  Minimize Directory searches  Optimize Directory search order and the PATH variable  Minimize arithmetic and character processing  Avoid command substitution when you can  Move loop invariant outside of loop, especially for command substitution


Download ppt "2000 Copyrigths Danielle S. Lahmani UNIX Tools G22.2245-001, Fall 2000 Danielle S. Lahmani Lecture 4."

Similar presentations


Ads by Google