1 / 28

Introduction to UNIX (2)

Introduction to UNIX (2). The UNIX Shells Shell functionality Selecting a shell Redirection/Substitution/Sequences Scripts Variables Process control Core built-in commands. Shell Functionality. A shell is an interface to the raw OS

ozzie
Download Presentation

Introduction to UNIX (2)

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. Introduction to UNIX (2) • The UNIX Shells • Shell functionality • Selecting a shell • Redirection/Substitution/Sequences • Scripts • Variables • Process control • Core built-in commands

  2. Shell Functionality • A shell is an interface to the raw OS • Built-in commands, scripts, variables, pipe, background job, wildcards, subshells, I/O redirection, substitution, sequences, ... • Selecting a shell • Bourne shell: $ prompt, /bin/sh • Korn shell: $ prompt, /bin/ksh • C shell: % prompt, /bin/csh • Command for changing a shell: chsh • Checking your current shell: echo $SHELL

  3. Shell Operations • Shell operation when you log-in • Reads a special startup file located in your home dir that contains some initialization information • .cshrc file (C shell), .profile (Bourne & Korn shell) • Displays a prompt and waits for a user command • A command longer than a line is possible using \ • If user types a Control-D or some exit command, the shell is terminated • Command: executable files vs. shell commands • e.g. ls is an executable located in /bin/ls • e.g, cd or echo are shell built-in commands

  4. Meta Characters • Some characters specially processed > : Output redirection; write standard output to a file >> : Appends standard output to a file < : Input redirection; read standard input from a file * : File substitution wildcard; matches 0 or more chars ? : File substitution wildcard; matches any single char [..]: File substitution wildcard; matches any char in [..] | : Pipe; send a process output to input of another one ; : Used for sequencing commands $ : Expands the value of a variable `command`: Command substitution; replaced by output

  5. More Meta Characters || : Conditional execution; execute if prev. one fails && : Conditional execution; execute if prev. one succeed (..) : Groups commands & : Run a command in the background # : For comment use \ : Prevents special interpretation of the next character • When shell scans given command, it first processes all meta characters in it specially, then it executes the command % echo hi > file % cat file hi % echo hi \> file % cat file hi > file %_

  6. I/O Redirection (>, >>, <) • A process has 3 standard I/O/E channels • Standard input, output, error • By default, they are keyboard, screen, screen • Output redirection • Store standard output of a process to a file command > filename or command >> filename • Store standard error of a process to a file command 2> filename • Input redirection • Use a file as standard input of a process command < filename or command << filename

  7. WildCards (*, ?, [..]) % ls a.c b.c cc.c unix1.ppt unix2.ppt % ls *.ppt unix1.ppt unix2.ppt % ls ?.c a.c b.c % ls [ac]* a.c cc.c % ls [A-Za-z]* a.c b.c cc.c unix1.ppt unix2.ppt

  8. Pipes (|) % ls a.c b.c cc.c unix1.ppt unix2.ppt % ls | wc -w 5 % ls | tee ls.capture | wc -w 5 % cat ls.capture a.c b.c cc.c unix1.ppt unix2.ppt

  9. Command Substitution (`) • A command surrounded by ` is executed and its standrad output is inserted in the command’s place % echo the date today is `date` the date today is Mon May 30 13:40:55 PST 2000 % who smoon ttyp0 May 30 13:00 (masslab:0.0) jaemok ttyp1 May 30 11:00 (maia) % echo there are `who | wc -l` users on the system there are 2 users on the system

  10. Command Sequences (;) • Commands separated by ; will be executed in sequence % date; ls ; pwd Mon May 30 13:40:55 PST 2000 a.c b.c cc.c unix1.ppt unix2.ppt /home/smoon % date > date.txt ; ls ; pwd > pwd.txt a.c b.c cc.c date.txt unix1.ppt unix2.ppt

  11. Conditional Sequences (&&, ||) • Every UNIX process has an exit value • Zero (0) means a success while non-zero (1) means a failure • A && B: B is executed only if A returns zero • A || B: B is executed only if A returns non-zero % cc myprog.c && a.out % cc myprog.c || echo compilation failed

  12. Grouping Commands () • Commands grouped with () are executed by a child shell which shares the same standard I/O/E channels % date; ls ; pwd > out.txt Mon May 30 13:40:55 PST 2000 a.c b.c cc.c unix1.ppt unix2.ppt % cat out.txt /home/smoon % (date ; ls ; pwd) > out.txt

  13. Background Processing (&) • A command ended with & is executed by a child shell as a background process which runs concurrently with the parent shell and does not take conrol of keyboard • Useful for concurrent tasks (but windowing obviated it) % find . -name a.c -print ./wild/a.c ./reverse.tmp/a.c % find . -name a.c -print & 27174 % pwd ./wild/a.c /home/smoon % ./reverse.tmp/a.c % find . -name a.c -print > find.txt &

  14. Shell Programs: Scripts • A series of shell commands saved in a file for later exec. • The file should have a execute permission (using chmod) • To run the script, simply type the file name as regular command • The first line determines which shell to use for execution • If it is just #, then the same shell you executed the script • If it is #! pathName, then pathName shell, otherwise Bourne shell % cat > script.csh #!/bin/csh echo -n the date today is # -n in csh omits new line date exit 0 ^D % chmod +x script.csh % script.csh the date today is Mon May 30 13:40:55 PST 2000

  15. Subshells • Your parent shell may creates a subshell for some tasks • When executing a grouped shell or a script; parent shell sleeps • When executing a background job, parent shell continues exec. • Subshell’s working dir does not affect that of parent’s % pwd /home/smoon % (cd / ; pwd) / % pwd /home/smoon • Each shell has two kinds data spaces • Environment space and local-variable space • A child shell inherits parent’s environment space and a clean local-variable space Parent Shell Environment Child Shell Local Environment Local

  16. Variables • Environment variable & local variable • Hold data in a string format • Every shell has predefined environment vars $Home: full path name of your home directory $PATH: list of directories to search for commands $MAIL: full path name of your mailbox $USER: your user name $SHELL: full path name of your login shell $TERM: type of your terminal % echo HOME = $HOME, PATH = $PATH HOME = /home/smoon, PATH = /bin:/usr/bin:/usr/sbin • Other environment vars and local vars may be created as needed (useful for writing scripts)

  17. Environment vs Local Vars • Bourne shell example $ firstname=Soo-Mook $ lastname=Moon $ echo $firstname $lastname Soo-Mook Moon $ export lastname $ sh .. Start a child shell; parent sleeps $ echo $firstname $lastname Moon $ ^D .. Terminate child; parent awakens $ echo $firstname $lastname Soo-Mook Moon $ _

  18. Special Built-In Variables $$: Process ID of the shell, $0: Name of the shell script $1..$9: $n is n-th command line arguments, $*: List of all $ cat script.sh echo the name of this script is $0 echo the first argument is $1 echo a list all arguments is $* echo this script places the date into a temporary file called $1.$$ date > $1.$$ ls $1.$$ rm $1.$$ $ script.sh a b c echo the name of this script is script.sh echo the first argument is a echo a list all arguments is a b c this script places the date into a temporary file called a.32751 a.32751

  19. Quoting • Inhibit wildcards, variable-substitution, command -substitution using ‘ (all) or “ (wildcards only) $ echo 3 * 4 = 12 3 a.c b.c cc.c 4 = 12 $ echo “3 * 4 = 12” 3 * 4 = 12 $ echo ‘my name is $name - date is `date`’ my name is $name - date is `date` $ echo “my name is $name - date is `date`” my name is Moon - date is Mon May 30 13:40:55 PST 2000

  20. Here Documents (<<) • % command << word • Copies its standard input up to, but not including the line starting with word into a shell buffer, then execute command using the contents of the buffer as input $ cat here.sh mail $1 << ENDOFTEXT Dear $1, Hi! - $USER ENDOFTEXT echo mail sent to $1 $ here.sh smoon mail sent to smoon $_

  21. Job Control • Obtain a listing of your current processes and contol their behavior - ps, kill, wait, sleep, .. $ (sleep 10; echo done) & 27387 $ ps PID TTY TIME CMD 27355 pts/3 0:00 -sh ___ the login shell 27387 pts/3 0:00 -sh ___ the subshell 27388 pts/3 0:00 sleep 10 ___ the sleep 27389 pts/3 0:00 ps ___ the ps command itself $ done

  22. PS Meaning • S: process status • (R: runnable, S: sleeping, T: suspended, Z: zombie) • UID: effective user ID of the process • PID: ID of the process • PPID: ID of the parent process • TIME: amount of CPU time used • CMD: command • C: percentage of CPU time used by the process • PRI: priority of the process • SZ: size of the process in KB • TTY: the controlling terminal

  23. Signalling Processes: Kill • Wish to terminate a process before it completes • kill [-signalID] {pid}+ :sends a signal with code signalID to numbered process (by default, a TERM signal, but if it won’t work, send a KILL (-9) signal) $ (sleep 10; echo done) & 27387 $ kill 27387 $ ps PID TTY TIME CMD 27355 pts/3 0:00 -sh ___ the login shell 27389 pts/3 0:00 ps ___ the ps command itself $ kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH LOST USR1 USR2

  24. Wait for Child Process: wait • A process may wait for one or more of its child processes to terminate by executing wait • Useful for advanced scripts $ (sleep 30; echo done 1) & 27387 $ (sleep 30; echo done 2) & 27390 $ echo done 3 ; wait echo done 4 done 3 done 1 done 2 done 4 $

  25. Finding a Command: $PATH • When a shell processes a command, it checks • If the command is a built-in; if so, executes it directly $ echo done 1 done 1 • If not, checks if it starts with /; if so, it is the absolute pathname of the command, so execute the program $ /bin/ls a.c b.c cc.c • If not, the shell searches directories whose names are stored in PATH environment variable, from left to right • If there is a match for the command, then execute the file • Otherwise (or if PATH is empty & it is not in pwd), an error $ echo $PATH /bin:/usr/bin:/usr/sbin $ ls a.c b.c cc.c $ foo foo: not found

  26. Overload Standard Utilities • Users often creates a bin subdirectory in their home and place it before /bin in their PATH setting • Can overload standard utilities with their own ones $ mkdir bin $ cd bin $ cat > ls echo my ls ^D $ chmod +x ls $ echo $PATH /bin:/usr/bin:/usr/sbin $ echo $HOME /home/smoon $ PATH=/home/smoon/bin:$PATH $ ls my ls • Only this shell (and its subshells) is affected by this change of the PATH variable

  27. Other Built-in Commands • evalcommand • Executes the output of command; useful for processing the output of utilities that generate shell commands (e.g., tset) $ echo x=5 x=5 $ eval `echo x=5` $ echo $x 5 • execcommand • Causes shell’s image to be replaced by command; if command is successful, the shell who executes exec dies $ exec date Mon May 30 13:50 PST 2000 login:

  28. Other Built-in Commands • umask [octal value] • When shell performs >, it creates a file with O666 permission but it is exclusive-or with umask setting for final permission $ date > date.txt $ ls -l date.txt -rw-r--r-- 1 smoon 10 May 30 13:50 date.txt $ umask 22 $ umask 0 $ date > date.txt $ ls -l date.txt -rw-rw-rw- 1 smoon 10 May 30 13:50 date.txt • Default umask value is O22, but you can change it with umask command, which is usually done in the shell start file (e.g., .cshrc)

More Related