1 / 47

CSCI 330 The UNIX System

CSCI 330 The UNIX System. Unit VII Bash Shell Basics. UNIX Command Interpreters. common term: shell families: Bourne shell developed as part of original, commercial UNIX C shell developed as part of free, academic UNIX standard: every UNIX system has a “Bourne shell compatible” shell.

quinta
Download Presentation

CSCI 330 The UNIX System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 330The UNIX System Unit VII Bash Shell Basics

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

  3. 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: DebianAlmquist shell • scripting shell in Ubuntu • ksh: Korn shell, Bell Labs improved sh • zsh: superset of all others • busybox: all-in-one utility for small systems CSCI 330 - The UNIX System

  4. C shell family • csh • written in 1979 by Bill Joy • part of BSD UNIX • tcsh: TENEX csh • part of TENEX operating system • was original shell for Mac OS X CSCI 330 - The UNIX System

  5. bash: Bourne-again Shell • default command interpreter for GNU/Linux • implements Bourne shell syntax • Bourne shell scripts run without modification • many improvements over Bourne shell • command line usability • many built-in commands • improved I/O redirection CSCI 330 - The UNIX System

  6. bash Shell Basics • Invocation • Customization • variables • prompt and aliases • Startup and initialization • Command line behavior • history • quoting • redirections and pipe • background • directory stacks • eval command CSCI 330 - The UNIX System

  7. Invoking bash • as login shell • specified in /etc/passwd • on the command line: % bash • with file as argument • file is shell script % bash scriptfile CSCI 330 - The UNIX System

  8. Customizations • 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 • set path to find executables • define custom prompt • define command aliases CSCI 330 - The UNIX System

  9. Variables • shell remembers values in variables • 2 types of variables • environment • valid for complete login session • shell • valid for each shell invocation • to display variable’s value % echo $variable CSCI 330 - The UNIX System

  10. Predefined Environment Variables “printenv” displays all environment variables CSCI 330 - The UNIX System

  11. Predefined Shell Variables CSCI 330 - The UNIX System

  12. Setting shell variables Syntax: varname=value Example: speed=fast echo "Today we go: $speed" Note: no spaces CSCI 330 - The UNIX System

  13. Example: PATH variable • PATH lists a set of directories • shell finds commands in these directories • on Ubuntu: % echo $PATH /usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games % PATH=$PATH:~/bin % echo $PATH /usr/local/bin:/usr/sbin:/usr/bin: /sbin:/bin:/usr/games:/home/student/bin CSCI 330 - The UNIX System

  14. bash Shell Prompt • can be set via “PS1” shell variable Example: % PS1="$USER > " z036473 > • Secondary prompts: PS2, PS3, PS4 CSCI 330 - The UNIX System

  15. bash Shell Prompt • special “PS1” shell variable settings: \w current work directory \h hostname \u username \! history event number \d date \t time \a ring the “bell” Example: % PS1="\u@\h-\!: " ege@turing-22: CSCI 330 - The UNIX System

  16. Shell Aliases • Allows you to assign a name, or alias to command(s) • use alias like any other command • can rename existing commands • to check current aliases: % alias • to set alias: % alias ll="ls –al" • to remove alias: % unaliasll CSCI 330 - The UNIX System

  17. To Create an Alias • via the default startup files: • ~/.profile or ~/.bashrc • 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 CSCI 330 - The UNIX System

  18. Startup & Shutdown Files • ~/.profile • /etc/profile • ~/.bash_profile • ~/.bash_login • ~/.bashrc • /etc/bash.bashrc • options: • --norc don’t run initialization files • -l run as login shell • ~/.bash_logout executed for login shell executed for non-login shell CSCI 330 - The UNIX System

  19. Customizations • startup • terminal settings • set path to find executables • define custom prompt • define command aliases • shutdown • save command history • remove temporary files CSCI 330 - The UNIX System

  20. Command line behavior • history • sequence • substitution • quoting • background • i/o redirection and pipe CSCI 330 - The UNIX System

  21. Shell History • shell keeps a record of previously entered commands so that they can later be: • re-executed • edited • Commands are saved • per session • per user • Size of history can be set via shell variables: HISTSIZE=500 HISTFILESIZE=100 CSCI 330 - The UNIX System

  22. Shell History • Each previously run command gets 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 CSCI 330 - The UNIX System

  23. Shell History • You can re-execute history events: • By the event number % !5 • By the number relative to current event % !-3 • By the text it contains % !ls CSCI 330 - The UNIX System

  24. Command line editing • UP ARROW: move back one command in history list • DOWN ARROW: move forward one command • LEFT and RIGHT ARROW: move into command • BACKSPACE and DELETE: Remove information • TAB: complete current command or file name CSCI 330 - The UNIX System

  25. Command Sequence • allows series of commands all at once • commands are separated by a semicolon ; Example: % date; pwd; ls CSCI 330 - The UNIX System

  26. 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 CSCI 330 - The UNIX System

  27. 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 2009 CSCI 330 - The UNIX System

  28. Quoting • allows to distinguish between the literal value of a symbol and the symbols used as code • used to distinguish between the literal symbol and the symbol’s use as a metacharacter or wild card characters • done via the following symbols: • Backslash (\) • Single quote (‘) • Double quote (“) CSCI 330 - The UNIX System

  29. Backslash (\) • also called the escape character • preserve the character immediately following it • For example: to create a file named “tools>”, enter: % touch tools\> CSCI 330 - The UNIX System

  30. Single Quote (‘) • protects the literal meaning of metacharacters • protects all characters within the single quotes • exception: it cannot protect itself Examples: % echo 'Joe said "Have fun"' Joe said "Have fun" % echo 'Joe said 'Have fun'' Joe said Have fun CSCI 330 - The UNIX System

  31. Double Quote (“) • protects all symbols and characters within the double quotes, expect for: • $ (dollar sign) • ` (back quote) • \ (backslash) Examples: % echo "I've gone fishing" I've gone fishing % echo "your home directory is $HOME" your home directory is /home/ege CSCI 330 - The UNIX System

  32. Quoting Examples: % echo “Hello Ray" Hello Ray % echo “Hello $USER" Hello a132436 % echo “It is now `date`” It is now Mon Feb 25 10:24:08 CST 2008 % echo “you owe me \$500” you owe me $500 CSCI 330 - The UNIX System

  33. Output Redirection (>) Syntax: command > file Sends output of command to file, instead of to terminal Examples: % ls > listing % cat file > filecopy CSCI 330 - The UNIX System

  34. Input Redirection (<) Syntax: command < file Command will read (take input) from file, instead of from terminal Example: % tr [a-z] [A-Z] < listing CSCI 330 - The UNIX System

  35. 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 CSCI 330 - The UNIX System

  36. Build the file ‘usage-status’ from the output of the ‘date’, ‘ls’, and ‘du’ commands 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 CSCI 330 - The UNIX System

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

  38. 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 uses file descriptor to refer to a file CSCI 330 - The UNIX System

  39. Redirection syntax • Output: >or1> filename 2> filename • Input: <or0< • Combining outputs: 2>&1 or &> or >& Example: % cat hugo > /tmp/one 2>&1 or: cat hugo &> /tmp/one CSCI 330 - The UNIX System

  40. Summary: Redirections and Pipe CSCI 330 - The UNIX System

  41. Shell Job Control • Foreground job: • a job that has our immediate attention • user has to wait for job to complete • Background job: • a job that the user does not wait for • it runs independently of user interaction Examples: • compiling long programs • solving complex equations • searching for particular kinds of files • backing up large number of files CSCI 330 - The UNIX System

  42. Shell Background Jobs • to execute command in the background, put & after it Example: % date; cd projects; cc gets.c –o gets & [1] 26432 % CSCI 330 - The UNIX System PID Job number

  43. Managing jobs • Unix shells allow users to: • make jobs execute in the background, • move jobs from background to foreground • display jobs • command “jobs” lists your active jobs • each job has number, used with “%” to refer to job • move job to foreground • fg %1 • once a job finishes it will display exit status CSCI 330 - The UNIX System

  44. Directory Stacks • To keep track of multiple directories using stack structure • To maintain a directory stack, we use the following three commands: • pushd • popd • dirs CSCI 330 - The UNIX System

  45. The commands: pushd • pushd directory to push directory on top of stack • pushd (with no argument) to travel between two top stack entries • popd (with no argument) to remove the top directory and changes to the next stack entry • dirs: to display all directory entriesin stack CSCI 330 - The UNIX System

  46. “eval” Command Synatx:eval [ arg … ] • arguments are read and concatenated into a single command, command is then executed • performs all shell substitutions, and then executes the command line Example: command="ls" options="-al" eval $command $options CSCI 330 - The UNIX System

  47. Summary • common features of UNIX command interpreters • next: shell script programming CSCI 330 - The UNIX System

More Related