350 likes | 367 Views
Learn about UNIX shell core functionalities, commands, and scripting from creating processes to shell variables. Understand built-in commands, wildcards, and file name expansion for efficient shell scripting.
E N D
UNIX ToolsG22.2245-001, Fall 2000 Danielle S. Lahmani email: lahmani@cs.nyu.edu Lecture 3 2000 Copyrigths Danielle S. Lahmani
Overview • Review of process creation • shell core functionality • /bin/sh • /bin/ksh • /bin/csh • project discussion 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"); } 2000 Copyrigths Danielle S. Lahmani
Fork operationreference Unix Network programming - W. R. Stevens 2000 Copyrigths Danielle S. Lahmani
(prog2 is cp in example) After exec of prog2 in child 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 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 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 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 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 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 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
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
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
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
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
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
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
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
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
The Bourne Shell: /bin/sh • Startup file: .profile • Variables: • Assignment: var = value; • Access: $var • Exporting variable: $export variable 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" …. 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 2000 Copyrigths Danielle S. Lahmani
/bin/sh: CONTROL STRUCTURES • Case . . . in . . . esac • For … do … done • If … then … fi • Until … do … done • While … done 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 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… 2000 Copyrigths Danielle S. Lahmani
/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
/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
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
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
/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
/bin/ksh (continued) • ARITHMETIC: Usinglet expression • TILDE SUBSTITUTION • ~ $HOME • ~user home directory of user • ~/pathname $HOME/pathname • ~+ $PWD • ~- $OLDPWD 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 } 2000 Copyrigths Danielle S. Lahmani
/bin/ksh: Functions (continued) • can use parameters • returning from a function • local variable using typeset • functions can be recursive 2000 Copyrigths Danielle S. Lahmani
/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
/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