SlideServe
Menu
Préférences
© SlideServe · v2026

Shell, Script Revisited

medea avatar
18 diapositives · 358 Views · Sep 13, 2014

제 20 강 :Shell, Script Revisited. Shell, Script Revisited. Newham & Rosenblatt, Learning the BASH shell,  O'Reilly Gail Anderson & Paul Anderson, The UNIX C Shell, Field Guide. Shell UNIX command interpreter UNIX Shell is just one of the user programs (a.out) Bash Shell features

Shell, Script Revisited - slide 1
01 / 18Shell, Script Revisited
1 / 17

Télécharger la présentation

Shell, Script Revisited
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. 제20강 :Shell, Script Revisited Shell, ScriptRevisited Newham & Rosenblatt, Learning the BASH shell,  O'Reilly Gail Anderson & Paul Anderson, The UNIX C Shell, Field Guide.

  2. Shell • UNIX command interpreter • UNIX Shell is just one of the user programs (a.out) • Bash Shell features • Read and interprete user’s command • Customize user’s system or environment (sh var) • Abbreviate long names, commands (alias) • Save old commands (history) • Provides job control (foreground/background) • High level programming using commands (script)

  3. Bash Setup Files (p. 58) • .bash_profile • Bash executes .bash_profile when user logs in • Changes and settings necessary when user logs in • .bash_logout • bash executes .bash_logout when user logs out • .bashrc • executes at beginning of execution by each shell • every time it executes a script • defines special bash characteristics • Setting up terminal stty erase ^h stty kill ^x

  4. Command Substitution • Using Backquote % d =`date` /* d is assigned the output of date*/ • back quote means “embedded command” • same as d = (Wed Jun 22 10:25:21 PST 1991) % echo $d Wed Jun 22 10:25:21 PST 1991 • Using $() % ls $(pwd) /* */ % vi $(ls *.c)

  5. Command Group • (command1; command2 ; command3) % pwd /usr/bob % (cd down; gcc work.c) % pwd /usr/bob • Parent shell creates child shell (sub-shell) • when it encounters ( ) • child shell parses command group, which may change its own new environment • new prompt • when both commands • are completed • you never said “cd ..” • but you are back to original directory

  6. struct user { int u_rsav[2]; /* save r5,r6 when exchanging stacks */ int u_fsav[25]; /* save fp registers */ char u_uid; /* effective user id */ int u_procp; /* pointer to proc structure */ char *u_base; /* base address for IO */ char *u_count; /* bytes remaining for IO */ char *u_offset[2]; /* offset in file for IO */ int *u_cdir; /* pointer to inode of current directory */ char u_dbuf[DIRSIZ]; /* current pathname component */ char *u_dirp; /* current pointer to inode */ struct { /* current directory entry */ int u_ino; char u_name[DIRSIZ]; } u_dent; int *u_pdir; /* inode of parent directory of dirp */ int u_ofile[NOFILE] /* pointers to file structures of open files */ int u_arg[5]; /* arguments to current system call */ int u_stime; /* this process system time */ u_utime; /* this process user time */ } u;

  7. How sub-shell is created parent shell $ sh $ date $ exit $ ls sub-shell OR $ sh< script OR $ (ls; who) $ date

  8. make - revisited Andrew Oram & Steve Talbott, Managing Projects with make, O’Reilly (p 59) a.o: a.c a.h cd down; gcc –o a.c a.h ar rcs libme.a a.o b.o Makefile: • Make treats eachcommand line as if it were executed in its own shell(\ is line continuation) cd down cd down \ cd down; gcc gcc (?) gcc (O) (O) Makefile same shell same shell different shells!

  9. Built-in Command • Command which is internal part of shell program • sh executes directly (not fork/execexternal cmd) • cd must be built-in command, otherwwise % pwd /* Suppose cd is an external process. */ /here /* Then sh fork/exec cd process*/ % cd /there /* New proc changes its own directory & terminate */ % pwd /* Back to parent sh process, who is still in /here */ /here • All commands that change environment of a process must change environment in current process’s context eg = setenv history …..

  10. I/O redirection • Diagnostic >& >>& % cc test.c |& more Pipes both std output & diagnostic % cat test >& save Redirect both standard output & diagnostic % (cat test> save1) >& save2 Separate std output and error file % cat test >>& save Append …

  11. Protection against mistakes in I/O redirection • > , >> could destroy accidently • by redirecting to existing file or • appending to non-existing file % set noclobberoffers protection % unset noclobber remove protection % set noclobber <set redirection protection> % who > memo memo : File exists % who >! memo overrides % ls >> test test : No such file or directory % cat memo >>! test force the output append % unset noclobber

  12. PID TTY STAT TIME COMMAND 62 001 R 0:01 du –s /etc > disk_storage 104 001 R 0:00 ls –l > directory.list 205 001 R 0:00 ps • Job control % du –s /etc > disk_storage & 6262 : process id % ls –l > directory.list & 104 % ps reports current process status % stop 62suspends process 62 % kill 62terminates process 62

  13. Conditional command execution % grep koh test.c > save &&lpr save If grep succeeds, then execute lpr % grep koh test.c > save || lpr save If grep fails, …

  14. In sum, sh is ... • sh is a language (very high level script program) • sh is interpreter (read & execute each line in script) • sh uses other components (which is a.out or other script) • sh glues them together • I/O redirection, • pipe, • IPC, • environment var, • control component execution: • conditional execution, • process control, ... date ls ONE=1 number=0 for filename in * do echo "$filename" | grep -q " " if [ $? -eq $ONE ] then let "number += 1" fi done

  15. In sum, sh is … (cont’d) • Convenient language • C like syntax • variables are weakly defined (no type definition, no init) • can combine many kinds of components • command, script (sh, awk, perl, java, …), daemon, … • slow(many context switches) but powerful & easy • adequate for • quick prototyping • system programming (high level)

  16. Many different kinds .. • Scripts on UNIX/Linux platform • Shell standard user command interface • Awk text processing • Tcl/tk X11 window programming • Scripts on Windows platform • Visual-Basic • Visual-C • Scripts common to Windows/UNIX(Linux) • Python • Perl text processing – CGI programming • Javascript Web page • Changing very rapidly • command + windows + web + …

  17. Script & specialty area • glue various components together sh Tcl/tk Python Perl --- scripts (sh language) text file run by interpreter commands httpd DB Win --- components (C, Java) (sort, date,..) binary run by hardware

  18. Why Script? • Easy to learn • days, weeks • Productivity • Glue various components (C, Java, command, DB, web ..) • Software reuse • Productivity: (60 times than C) (30% than OO) • Slow • 10 – 20 times (less problem as hardware improves) • Shell & C/Java comparison • Script: integration, quick prototyping • C, Java: component coding (library, process, …) for complex/performance-sensitive problems