<<

Characteristics

-line interface between the Shell Environments user and the  A program that automatically starts on login, waits for user to in commands Class Meeting 6, Part I  A command interpreter that uses a programming language  is a text containing logic for shell interpretation

* Notes adapted by Doug Bowman from previous work by other members of the CS faculty Virginia Tech (C) 2005 Doug Bowman and Virginia Tech CS Dept. 2

Shell Interactivity Shell Programming

 Command line parsing  Variables  I/O redirection, pipes and filters  Control structures  Environment  Loops and conditionals  Text completion  Function definition and invocation  Aliases  Shell scripts  Command line editing  Next lecture  Command  Configuration

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 3 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 4

Three standard files Redirecting stdout

 Each shell (and all other programs) automatically  Instead of sending stdout to the open three “files” when they start up terminal, you can redirect stdout to a  stdin — input character stream file using the > shell operator  Defaults to keyboard  >> shell operator is used to append  stdout — output character stream stdout to an existing file  Defaults to terminal  Examples:  stderr — receives error messages  man > ls_help.txt  Defaults to terminal  $ > current_directory  file1 >> file2

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 5 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 6

1 Redirecting stdin Pipes and Filters

 Instead of reading from a terminal, you  Pipe — a way to connect stdout of one command to stdin of another; use can tell a program to read from a file |  Filter — a program that takes input and transforms it using the < shell operator in some way  Examples:  — gives a count of words/lines/characters  — searches for lines with a given pattern (regular  [email protected] < message.txt expression)  sort < friends > sorted_friends  — sorts lines alphabetically or numerically  — select parts of each line to send to stdout

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 7 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 8

Examples Various Unix Shells

 ls -la |  sh (, original )  ls -la | wc  ksh (Korn shell)  man | grep "history"  csh (, developed at Berkeley)  aux | grep user1 | wc -l  | sort > current_users  bash (Bourne again Shell)  Default user shell in Linux  http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 9 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 10

Bourne Again Shell Environment Variables

 bash is the standard shell for this class  A set of variables the shell uses for  Superset of the Bourne shell (sh) certain operations  Borrows features from sh, csh, tcsh, and  Variables have a name and a value ksh  Current list can be displayed with the  Part of the GNU project command  Dispaly value of varname with echo $varname

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 11 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 12

2 Examples Setting Environment Variables

[cs2204@acorn bin]$ echo $PS1  Set variable with varname=value  PS1=$USER@$HOSTNAME: [\u@\h \]\$  Change default shell prompt  PS1="bash rules> "  PATH=$PATH:$HOME/bin [cs2204@acorn bin]$ echo $PATH  Append :$HOME/bin to PATH /usr/local/bin:/bin:/usr/X11R6/  PATH=$PATH:~:. bin:/usr/games:/home/courses/  Append :~:. to PATH  DATE=`date` or DATE=$(date) cs2204/bin  Capture output from date command

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 13 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 14

Text Completion Aliases

attempts to complete the  alias shortcut=command current command or filename  Examples  push expands to  alias pu=pushd pushd  alias po=popd  pu gives the alternatives  alias l="ls –F –C" pu pup pushd  alias ll="ls –L –l –F"  In /etc, entering ll modu gives  alias up=“ ..” modules modules.conf modules.devfs  alias hide=" og-rwx" [cs2204@blackberry etc]$ ll modules  alias unhide="chmod og+r"

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 15 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 16

Command History Editing on the Command Line

 Use history command to list  bash provides a number of line editing previously entered commands commands  Use –l to list previously  Left- and right-arrows insertion point typed commands from m through n  Default emacs-mode commands  Esc-b Move back one word  Use up and down cursor keys to scroll  Esc-f Move forward one word through history list  Ctrl-a Move to beginning of line  Ctrl-e Move to end of line  Ctrl-k text from cursor to end of line

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 17 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 18

3 Login Scripts Example .bash_profile (partial)

 You don’t want to set up your whole shell # .bash_profile environment each you log in # include .bashrc if it exists  Startup scripts executed at each login if [ -f ~/.bashrc ]; then . ~/.bashrc  /etc/profile fi  ~/.bash_profile  ~/.bash_login (if no .bash_profile) # Set variables for a warm fuzzy environment  ~/.profile (if neither are present)  ~/.bashrc export CVSROOT=~/.cvsroot export EDITOR=/bin/  Script executed upon logout export PAGER=/usr/bin/less  ~/.bash_logout

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 19 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 20

Example .bashrc (partial) Login and Other Shells

# .bashrc login shell # abbreviations for some common commands /etc/profile alias bye=logout ~/.bash_profile interactive shell ~/.bashrc alias h=history ~/.bashrc alias l='ls -F -C' alias ll='ls -L -l -F' interactive shell interactive shell alias po=popd ~/.bashrc alias pu=pushd ~/.bashrc

(C) 2005 Doug Bowman and Virginia Tech CS Dept. 21 (C) 2005 Doug Bowman and Virginia Tech CS Dept. 22

4