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 3.

Similar presentations


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

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

2 2000 Copyrigths Danielle S. Lahmani Overview Review of process creation shell core functionality /bin/sh /bin/ksh /bin/csh project discussion

3 2000 Copyrigths Danielle S. Lahmani Example : Program that creates a new process to copy files Reference: M.Bach, "The Unix Operating system", p 11. main(argc,argv) int(argcl char *argv[]; {/* assumes 2 args, source and target files */ if ( fork() == 0) { /* child process */ execl("cp"."cp",argv[1],argv[2],0); } /* parent process */ wait(int *) 0); printf("copy done\n"); }

4 2000 Copyrigths Danielle S. Lahmani Fork operation reference Unix Network programming - W. R. Stevens

5 2000 Copyrigths Danielle S. Lahmani After exec of prog2 in child (prog2 is cp in example)

6 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

7 2000 Copyrigths Danielle S. Lahmani Complex commands Multiple commands –example: $ls ; pwd background processes –example: sleep 40& Command groupings –(cmd1; cmd2; cmd3) Conditional command execution

8 2000 Copyrigths Danielle S. Lahmani File name expansion Wildcards * matches any string of characters ?matches any single character [list] matches any character in list [lower-upper] matches any character in range lower-upper inclusive

9 2000 Copyrigths Danielle S. Lahmani Command substitution A command can be placed with grave accents ` ` to capture the output of command often used with shell variables

10 2000 Copyrigths Danielle S. Lahmani Shell Scripts A shell script is a regular text file that contains shell or UNIX commands Before running it, it must have execute permissions ( see chmod +x filename) Very useful for automating repetitive task and administrative tools and for storing commands for later execution

11 2000 Copyrigths Danielle S. Lahmani Shell Scripts (continued) When a script is run, kernel determines which shell it is written for by examining the first line of the script –If 1 st line is just #, then it is interpreted by a C shell –If 1 st line is of the form #!pathname, then the executable –Pathname is used to interpret the script –If neither rule 1 nor rule 2 applies, the script is interpreted by a Bourne shell.

12 2000 Copyrigths Danielle S. Lahmani Here Documents Shell provides alternative ways of supplying standard input to commands Shell allows in-line input redirection using << called here documents format command [arg(s)] << arbitrary-delimiter command input : arbitrary-delimiter arbitrary-delimiter should be a string that does not appear in text

13 2000 Copyrigths Danielle S. Lahmani Shell Variables Shell has several mechanisms for creating variables. A variable is a name Representing a string value –Shell variables can save time and reduce typing errors, variables Allow you to store and manipulate information two types : local and environmental –local are set by the user of by the shell itself –Positional parameters variables are normally set only on a command line

14 2000 Copyrigths Danielle S. Lahmani Environmental Variables NAMEMEANING $HOMEabsolute pathname of your home directory $PATHa list of directories to search for $MAILabsolute pathname to mailbox $USERyour user id $SHELLabsolute pathname of login shell $TERMtype of your terminal

15 2000 Copyrigths Danielle S. Lahmani Positional parameters when a shell procedure is invoked, the shell implicitly creates positional parameters. The name for a positional parameter is a number. Positional parameters are used mainly in scripts. –$0 is the argument in position zero on the command line –$1 is the first argument –$1.. $9 $n refers to the nth argument on the command line if applicable –$# the number of positional parameters, not counting 0 –$* the list of all arguments

16 2000 Copyrigths Danielle S. Lahmani QUOTING Quoting restores the literal meaning to characters that are processed specially by the shell. The literal quotes are not passed on to the command Single quotes ( ' ) inhibit wildcard replacement, variable substitution, and command substitution Double quotes ( " ) inhibit wildcard replacement only When quotes are nested, only the outer quotes have any effect

17 2000 Copyrigths Danielle S. Lahmani BUILT-IN commands commands that are internal to the shell Faster to execute and more efficient than other commands –Shell does not have to fork to execute the command –Trade-off: redirection of input/output not allowed for most of these

18 2000 Copyrigths Danielle S. Lahmani Built-in commands (continued) built-in commands common to the 3 shells: echoexec cdshift waitumask exiteval

19 2000 Copyrigths Danielle S. Lahmani Subshells When a parent shell forks a child to execute a command, the new child shell is sometimes called a subshell. This happens when: –a group command is executed ( $(cmd1; cmd2; cmd3) ) –a shell script is executed( $myscript ) –a background job is executed ( cmd1&) A shell inherits the parent's environment but not the parent's local variables.

20 2000 Copyrigths Danielle S. Lahmani The Bourne Shell: /bin/sh Startup file:.profile Variables: –Assignment: var = value; –Access: $var –Exporting variable: $export variable

21 2000 Copyrigths Danielle S. Lahmani /bin/sh:BUILT-IN VARIABLES $#number of cmd lines args $- options currently in effect $? exit value of last executed cmd $$process num of current process $! Proc num of last background proc $*all arguments on command line "$@"all arguments on command line individually quoted "$1" "$2" ….

22 2000 Copyrigths Danielle S. Lahmani /bin/sh: Arithmetic No arithmetic support in /bin/sh expr expression –Evaluates expression and sends the result to standard output –yield a numeric or string result test expression for conditional expression

23 2000 Copyrigths Danielle S. Lahmani /bin/sh: CONTROL STRUCTURES Case... in... esac For … do … done If … then … fi Until … do … done While … done

24 2000 Copyrigths Danielle S. Lahmani /bin/sh: trap command trap specifies command that should be executed when the shell receives a signal of a particular value. Trap [ [command] {signal}+] –If command is omitted, signals are ignored

25 2000 Copyrigths Danielle S. Lahmani /bin/sh: Other commands Debbuging options for scripts: –set -vx –-v : echo shell commands as they are read –-x : echo shell commands as they are executed sequenced commands –{ cmd1; cmd2; cmd3 …cmdn} executed by the parent, can redirect output, etc…

26 2000 Copyrigths Danielle S. Lahmani /bin/sh:Redirection using file Descriptors cmd >&n send cmd output to fd n cmd <&n take input for cmd from fd n cmd >&- close standard output cmd <&- close standard input

27 2000 Copyrigths Danielle S. Lahmani /bin/sh:multiple redirection cmd 2>filesend standard error to file standard output remains the same cmd > file 2>&1 send both standard error and standard output to file (cmd > file1) 2>file2send standard output to file1, send standard error to file2

28 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

29 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

30 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.

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

32 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 }

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

34 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 job

35 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.


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

Similar presentations


Ads by Google