CSCI 330 UNIX and Network Programming Unit III

  • Slides: 30
Download presentation
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1

CSCI 330 UNIX and Network Programming Unit III Shell, Part 1

CSCI 330 - UNIX and Network Programming 2 UNIX Command Interpreters common term: shell

CSCI 330 - UNIX and Network Programming 2 UNIX Command Interpreters common term: shell • families: • Bourne shell • developed as part of original, commercial UNIX • C shell • developed as part of academic, free UNIX • standard: • every UNIX system has a “Bourne shell compatible” shell

CSCI 330 - UNIX and Network Programming Bourne shell family • sh: original Bourne

CSCI 330 - UNIX and Network Programming Bourne shell family • sh: original Bourne shell • written 1978 by Steve Bourne • part of commercial UNIX • ash: Almquist shell • BSD-licensed replacement of sh • bash: Bourne-again shell • GNU replacement of sh • dash: Debian Almquist shell • small scripting shell 3

CSCI 330 - UNIX and Network Programming bash shell basics • Customization • startup

CSCI 330 - UNIX and Network Programming bash shell basics • Customization • startup and initialization • variables, prompt and aliases • Command line behavior • history • sequence & substitution • redirections and pipe 4

CSCI 330 - UNIX and Network Programming Customization • via command line options •

CSCI 330 - UNIX and Network Programming Customization • via command line options • rarely done • instead: process initialization file ~/. profile if login session shell ~/. bashrc if invoked from command line • via variables • terminal settings: speed, size, color • path to find executables • custom prompt • command aliases 5

CSCI 330 - UNIX and Network Programming 6 Variables • shell remembers values in

CSCI 330 - UNIX and Network Programming 6 Variables • shell remembers values in variables • variable has name and type: string, number, array • to set string variable: Syntax: % varname=value • to display variable’s value % echo $varname

CSCI 330 - UNIX and Network Programming Variables Examples: % speed=fast % echo Today

CSCI 330 - UNIX and Network Programming Variables Examples: % speed=fast % echo Today we go $speed Today we go fast % speed="very fast" % echo Now we go $speed Now we go very fast 7

CSCI 330 - UNIX and Network Programming Variable Scope • valid for duration of

CSCI 330 - UNIX and Network Programming Variable Scope • valid for duration of shell invocation • variable can be exported into environment: inherited by commands and subshells Example: % export fast 8

CSCI 330 - UNIX and Network Programming some predefined variables Name Meaning HOME full

CSCI 330 - UNIX and Network Programming some predefined variables Name Meaning HOME full pathname of your home directory PATH list of directories to search for commands USER Your user name, also UID for user id SHELL full pathname of your login shell PWD Current work directory HOSTNAME current hostname of the system HISTSIZE Number of commands to remember PS 1 primary prompt (also PS 2, …) ? Return status of most recently executed command $ Process id of current process 9

CSCI 330 - UNIX and Network Programming 10 Example: PATH variable • PATH lists

CSCI 330 - UNIX and Network Programming 10 Example: PATH variable • PATH lists a set of directories • shell finds commands in these directories on Linux: % echo $PATH /usr/sbin: /usr/bin: /sbin: /usr/games % PATH=$PATH: ~/bin % echo $PATH /usr/sbin: /usr/bin: /sbin: /usr/games: /home/student/bin

CSCI 330 - UNIX and Network Programming bash shell prompt • can be set

CSCI 330 - UNIX and Network Programming bash shell prompt • can be set via “PS 1” shell variable Example: % PS 1="$USER > " student > • Secondary prompts: PS 2, PS 3, PS 4 11

CSCI 330 - UNIX and Network Programming bash shell prompt • special “PS 1”

CSCI 330 - UNIX and Network Programming bash shell prompt • special “PS 1” shell variable settings: w h u ! d t a current work directory hostname username history event number date time ring the “bell” Example: % PS 1="u@h-!: " ege@turing-22: 12

CSCI 330 - UNIX and Network Programming shell aliases • Allows you to assign

CSCI 330 - UNIX and Network Programming shell aliases • Allows you to assign a different name to a command • use alias like any other command • to check current aliases: % alias • to set alias: % alias ll="ls –al" • to remove alias: % unalias ll 13

CSCI 330 - UNIX and Network Programming to create and keep aliases • on

CSCI 330 - UNIX and Network Programming to create and keep aliases • on the command line • same scope as shell variables • via a text file • enter alias definitions into text file • execute text file via “source” or “. ” command • reads and executes content of file in current shell • make durable via the default startup files: • ~/. profile or ~/. bashrc 14

CSCI 330 - UNIX and Network Programming Command line behavior • history • sequence

CSCI 330 - UNIX and Network Programming Command line behavior • history • sequence • substitution • i/o redirection and pipe 15

CSCI 330 - UNIX and Network Programming 16 Shell History • record of previously

CSCI 330 - UNIX and Network Programming 16 Shell History • record of previously entered commands • can be: re-called, edited, re-executed • commands are saved • per session • per user HISTSIZE=500 HISTFILESIZE=100 • size of history can be set via shell variables

CSCI 330 - UNIX and Network Programming Shell History • Each command in history

CSCI 330 - UNIX and Network Programming Shell History • Each command in history has a sequential event number • to view the history buffer: Syntax: history [-c] [count] • If no options are supplied, list all Useful options: -c clear history 17

CSCI 330 - UNIX and Network Programming 18 Command line editing • UP ARROW

CSCI 330 - UNIX and Network Programming 18 Command line editing • UP ARROW • DOWN ARROW move back one command in history list • LEFT and RIGHT ARROW • BACKSPACE and DELETE • TAB move forward one command move into command remove information complete current command or file name

CSCI 330 - UNIX and Network Programming Command Sequence • allows series of commands

CSCI 330 - UNIX and Network Programming Command Sequence • allows series of commands all at once • commands are separated by a semicolon Example: % date; pwd; ls ; 19

CSCI 330 - UNIX and Network Programming Command Substitution • command surrounded by back

CSCI 330 - UNIX and Network Programming Command Substitution • command surrounded by back quotes ` is replaced by its standard output • newlines in the output are replaced by spaces Examples: % echo `date` % ls -l `which passwd` % var=`whoami`; echo $var 20

CSCI 330 - UNIX and Network Programming Command Substitution • second form of command

CSCI 330 - UNIX and Network Programming Command Substitution • second form of command substitution: $(command) Examples: % echo User $(whoami) is on $(hostname) User ege is on turing % echo Today is $(date) Today is Fri Jul 17 08: 06: 28 CDT 2012 21

CSCI 330 - UNIX and Network Programming Output Redirection (>) Syntax: command > file

CSCI 330 - UNIX and Network Programming Output Redirection (>) Syntax: command > file sends command output to file, instead of terminal Examples: % ls > listing % cat listing > filecopy Note: if “file” exists, it is overwritten 22

CSCI 330 - UNIX and Network Programming Input Redirection (<) Syntax: command < file

CSCI 330 - UNIX and Network Programming Input Redirection (<) Syntax: command < file command reads (takes input) from file, instead of keyboard Example: % tr [a-z] [A-Z] < listing 23

CSCI 330 - UNIX and Network Programming Examples: Output / Input • Redirecting input

CSCI 330 - UNIX and Network Programming Examples: Output / Input • Redirecting input and output: % tr [A-Z] [a-z] < r. in > r. out • Output of command becomes input to next: % ls > temp. txt; wc < temp. txt • Eliminate the middleman: pipe % ls | wc 24

CSCI 330 - UNIX and Network Programming Appending Output • Syntax: command >> file

CSCI 330 - UNIX and Network Programming Appending Output • Syntax: command >> file adds output of command at the end of file • If file does not exist, shell creates it • Examples: % date > usage-status % ls -l >> usage-status % du -s >> usage-status Build the file ‘usage-status’ from the output of ‘date’, ‘ls’, and ‘du’ 25

CSCI 330 - UNIX and Network Programming Here Document • read input for current

CSCI 330 - UNIX and Network Programming Here Document • read input for current source, uses “<<“ symbol Syntax: command << LABEL • reads following lines until line starting with “LABEL” Example: % > > > 2 wc -l << DONE line one line two DONE 26

CSCI 330 - UNIX and Network Programming File Descriptor • positive integer for every

CSCI 330 - UNIX and Network Programming File Descriptor • positive integer for every open file • process tracks its open files with this number 0 – standard input 1 – standard output 2 – standard error output • bash can use file descriptor to refer to a file 27

CSCI 330 - UNIX and Network Programming Redirection syntax • Output: > or 1>

CSCI 330 - UNIX and Network Programming Redirection syntax • Output: > or 1> filename 2> filename • Input: < or 0< • Combining outputs: 2>&1 or &> or Example: % cat hugo > /tmp/one 2>&1 or: % cat hugo &> /tmp/one >& 28

CSCI 330 - UNIX and Network Programming Summary: Redirections and Pipe Command Syntax Meaning

CSCI 330 - UNIX and Network Programming Summary: Redirections and Pipe Command Syntax Meaning command < file redirect input from file to command > file redirect output from command to file command >> file redirect output of command appends it to file command > file 2>&1 command &> file add error output to standard output, redirect both into file command 1 | command 2 take/pipe output of command 1 as input to command 2 command << LABEL take input from current source until LABEL line 29

CSCI 330 - UNIX and Network Programming Summary • features of the UNIX shell:

CSCI 330 - UNIX and Network Programming Summary • features of the UNIX shell: • customization • prompt, alias • command line behavior • history • sequence, substitution • redirections and pipe 30