1 / 35

UNIX Tools G22.2245-001, Fall 2000

UNIX Tools G22.2245-001, Fall 2000. Danielle S. Lahmani email: lahmani@cs.nyu.edu Lecture 3. Overview. Review of process creation shell core functionality /bin/sh /bin/ksh /bin/csh project discussion. Example : Program that creates a new process to copy files.

reece-moon
Download Presentation

UNIX Tools G22.2245-001, Fall 2000

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. UNIX ToolsG22.2245-001, Fall 2000 Danielle S. Lahmani email: lahmani@cs.nyu.edu Lecture 3 2000 Copyrigths Danielle S. Lahmani

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

  3. 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"); } 2000 Copyrigths Danielle S. Lahmani

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

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

  6. 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 2000 Copyrigths Danielle S. Lahmani

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

  8. 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 2000 Copyrigths Danielle S. Lahmani

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

  10. 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 2000 Copyrigths Danielle S. Lahmani

  11. 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 1st line is just #, then it is interpreted by a C shell • If 1st 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. 2000 Copyrigths Danielle S. Lahmani

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

  13. 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 2000 Copyrigths Danielle S. Lahmani

  14. Environmental Variables NAME MEANING $HOME absolute pathname of your home directory $PATH a list of directories to search for $MAIL absolute pathname to mailbox $USER your user id $SHELL absolute pathname of login shell $TERM type of your terminal 2000 Copyrigths Danielle S. Lahmani

  15. 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 2000 Copyrigths Danielle S. Lahmani

  16. 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 2000 Copyrigths Danielle S. Lahmani

  17. 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 2000 Copyrigths Danielle S. Lahmani

  18. Built-in commands (continued) • built-in commands common to the 3 shells: echo exec cd shift wait umask exit eval 2000 Copyrigths Danielle S. Lahmani

  19. 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. 2000 Copyrigths Danielle S. Lahmani

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

  21. /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" …. 2000 Copyrigths Danielle S. Lahmani

  22. /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 2000 Copyrigths Danielle S. Lahmani

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

  24. /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 2000 Copyrigths Danielle S. Lahmani

  25. /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…  2000 Copyrigths Danielle S. Lahmani

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

  27. /bin/sh:multiple redirection • cmd 2>file send 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>file2 send standard output to file1, send standard error to file2 2000 Copyrigths Danielle S. Lahmani

  28. 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 2000 Copyrigths Danielle S. Lahmani

  29. 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 2000 Copyrigths Danielle S. Lahmani

  30. /bin/ksh: History Mechanism • Numbered commands $ PS1='! $’ /* set prompt to contains a ! */ $HISTSIZE default is 128 • using the built-in vi editor • declare VISUAL=vior 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. 2000 Copyrigths Danielle S. Lahmani

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

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

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

  34. /bin/ksh: ENHANCED JOB CONTROL • jobs list your jobs • bg places a specified job in the background • fg places a specified job in the foreground • kill sends an arbitrary signal to a process or job • ^z to stop a job 2000 Copyrigths Danielle S. Lahmani

  35. /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. 2000 Copyrigths Danielle S. Lahmani

More Related