Unix Shell Environments

Unix Shell Environments

Shell Characteristics Command-line interface between the Unix Shell Environments user and the operating system A program that automatically starts on login, waits for user to type in commands Class Meeting 6, Part I A command interpreter that uses a programming language Shell script is a text file containing logic for shell interpretation * Notes adapted by Doug Bowman from previous work by other members of the CS faculty at 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 history 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 > ls_help.txt Defaults to terminal echo $PWD > current_directory cat 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: wc — gives a count of words/lines/characters grep — searches for lines with a given pattern (regular mail [email protected] < message.txt expression) sort < friends > sorted_friends sort — sorts lines alphabetically or numerically cut — 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 | less sh (Bourne shell, original Unix shell) ls -la | wc ksh (Korn shell) man bash | grep "history" csh (C shell, developed at Berkeley) ps aux | grep user1 | wc -l tcsh who | 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 env 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 Environment Variable Examples Setting Environment Variables [cs2204@acorn bin]$ echo $PS1 Set variable with varname=value PS1=$USER@$HOSTNAME: [\u@\h \W]\$ 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 <tab> attempts to complete the alias shortcut=command current command or filename Examples push<tab> expands to alias pu=pushd pushd<space> alias po=popd pu<tab> gives the alternatives alias l="ls –F –C" pu pup pushd alias ll="ls –L –l –F" In /etc, entering ll modu<tab> gives alias up=“cd ..” modules modules.conf modules.devfs alias hide="chmod 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 fc –l <m> <n> to list previously Left- and right-arrows move 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 Kill 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 time 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/vi 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us