unix shells
play

UNIX Shells CIS 218 Shells A shell is a command line interpreter - PowerPoint PPT Presentation

UNIX Shells CIS 218 Shells A shell is a command line interpreter that is the interface between the user and the OS. It is also a programming language. Shells may be used interactively or non -interactively. In interactive mode, they


  1. UNIX Shells CIS 218

  2. Shells • A shell is a command line interpreter that is the interface between the user and the OS. It is also a programming language. • Shells may be used interactively or non -interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file. • The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. • The shell: – analyzes each command – performs substitutions – determines what actions are to be performed – performs the actions • Shells also provide a small set of built -in commands ( builtins ) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec) cannot be implemented outside of the shell because they directly manipulate the shell itself. Builtin commands do not generate separate processes.

  3. Which Shell? • sh – Bourne shell – Most common, other shells are a superset • csh or tcsh – “C” shell – C-like syntax – Used by UNIX programmers • bash – “Bourne again shell” default shell on Linux – Based on sh, newer version has some csh features. • korn – written by David Korn – Based on sh – combines features of bash and csh. Default shell on Solaris • PERL – Program Extract and Reporting Language – complex sysadmin scripting language with “C” language features pre-dates Internet but used for web programming. • PYTHON – “BASH” like” scripting language used for sysadmin, less so for for web programming.

  4. Common shell facilities • Input-output redirection (STDIN, STDOUT,STDERR): prog < infile > outfile Also << (here document) and >> (append) • Pipeline “|” commands send the output (STDOUT) from one command to STDIN on a subsequent command. • Semicolon ; allows multiple commands on one line. | pipe character is also a command separator. Parenthese () group commands as separate processes withina process tree. • Job control - A job is a program whose execution has been initiated by the user. - Foreground job: a program which has control of the terminal - Background job: runs concurrently with the parent shell and does not take control of the keyboard. - Start a job in the background by appending &. “kill < pid >” terminates background process. - Place a foreground task by using “Ctrl Z” to put to sleep, bg command to place in background. - jobs command list background tasks. Can bring background job to foreground with “ fg [job#]“

  5. Redirection Redirection • Use > to redirect standard output to a ‘file’: standard output file command standard input

  6. Redirection output • $ cat > sample.txt This text is being entered at the keyboard. Cat is copying it to a file. Press control-D to indicate the end of file. $ control-D $ cat file 1 .c file 2 .c file 3 .c > all-files.c

  7. Redirection • • Use > to redirect standard output to a ‘file’: standard output file command standard input

  8. Redirect input • $ cat < supply_orders 2000 sheets letterhead ordered: 10 / 7 / 97 1 box masking tape ordered: 10/8/97 $ $ cat supply_orders $ mail ad@ratree.psu.ac.th < letter.txt

  9. Redirecting append • Use >> to append: $ date > whoson $ cat whoson Fri May 29 09 : 24 : 19 GMT 2000 $ who >> whoson $ cat whoson Fri May 29 09 : 24 : 19 GMT 2000 jenny tty 02 May 29 07: 21 ad tty 06 May 28 11:01 $

  10. Redirection • • Use a pipe to connect standard output of one command to standard input of another: command 1 command 2 standard standard input output

  11. Redirection using pipe • Use the ‘|’ operator between commands: $ command 1 | command 2 • Same as: $ command 1 > temp $ command 2 < temp $ rm temp

  12. Filters • • A filter is a command that modifies its standard input, putting the changes onto its standard output: $ who | sort | lpr $ ps | grep ad

  13. Tee command • • Passes its input through to standard output unchanged. Also saves input into a file: file command 1 command 2 tee standard standard input output

  14. Tee command • • $ who | tee who.out | grep ad ad tty 06 May 23 10:31 • $ cat who.out jenny tty 02 May 21 15: 29 ad tty 06 May 23 10: 31 scott tty 03 May 23 09:02

  15. File Name Expansion • * stands in for 0 or more (any) characters • ? stands in for exactly one (any) character • [..] stands for one character contained in the set of .. sh[1-6] stands in for one of 1, 2, 3, 4, 5, 6 • [^..] stands for one character NOT contained in the set of .. [^oa] stands in for any char except o or a • ~/ stands in for your home directory • Examples - ls *.c - rm file[1-6].? - cd ~/bin - ls ~krueger - ls *.[^oa]

  16. Shell Commands • You can run any program in a shell by calling it as you would on the command line. • When you run a shell program or binary like grep or ls in a shell program, a new (child) process is created – e.g. “spawned” or “forked”. UNLESS, the program is run with the “here” specification – e.g. . Command. (Note the “.” before the command. • Built-in UNIX commands spawn where no new process is created. (in addition to shell script commands). These include the below commands as well as shell logic constructs. set read exit test shift wait

  17. Variables • Variables are placeholders, named locations in memory, where values are stored. They are assigned via the left/right rule. Name only on left when assigning a value: [set] name=value Dollar sign “$” in fromt of variable name when referenecing that value: echo $name • Whitespace or IFS separate text into words or tokens (see Quoting) – [set] name=value – assignment (no spaces around =) – [set] name= nulls variable, variable exists, no value – $name – replaced by value of name, – ${name} – replaced by value of name, {} delineates varaiable name from adjacent test – unset name - to remove variable • Single value: bindir="/usr /bin“ • List of values (separated by spaces): searchdirs="~/tests $HOME/test2 ."

  18. String Quoting • Scripting languages are all about replacing text or strings. Usually with some form or “quoting”. • Escape “ \ ” quotes the following single character. • Double quotes “” inhibit wildcard replacement only. • Single quotes ‘’ inhibit wildcard replacement, variable substitution and command substitution. • Back quotes `comand`cause command substitution: command output assigned to a variable. Alternate syntax is $(command). • Quoting is used to remove the special meaning of certain (meta)characters or words to the shell. • Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion. • There are four quoting mechanisms: the escape character, single quotes, double quotes and back quotes.

  19. String Quoting • Escape Character: A non- quoted backslash ‘ \ ’ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). • Single Quotes: Enclosing characters in single quotes (‘’’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. • Double Quotes: Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘‘’, ‘ \ ’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘‘’ retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘‘’, ‘"’, ‘ \ ’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed. The special parameters ‘*’ and ‘@’ have special meaning when in double quotes, • Command substitution: `` encloses a command and allows re-assignment of command output to a variable. Example: DATE=`date`; echo $DATE

  20. ANSI-C Characters (man echo) • \a - alert (bell) • \b - backspace • \e - an escape character (not ANSI C) • \f - form feed • \n - newline • \r - carriage return • \t - horizontal tab • \v -vertical tab • \\ - backslash • \' -single quote • \ nnn - the eight-bit character whose value is the octal value nnn (one to three digits) • \x HH -the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) • \c x - a control- x character

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend