1 / 26

CSCI 330 The UNIX System

CSCI 330 The UNIX System. Bash shell. Bash Shell Specifics. Shell login and logout files Shell variables Prompt History IO redirection. Invoking bash. On the command line: % sh % bash as login shell specified in /etc/passwd with file as argument file is sh-script % sh scriptfile .

lynsey
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 Bash shell

  2. CSCI 330 - The Unix System Bash Shell Specifics • Shell login and logout files • Shell variables • Prompt • History • IO redirection

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

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

  5. CSCI 330 - The Unix System Predefined Shell Variables

  6. CSCI 330 - The Unix System User-defined Shell Variables • Syntax: varname=value Example: rate=7.65 echo “Rate today is: $rate” • use double quotes if the value of a variable contains white spaces Example: name=“Thomas William Flowers”

  7. CSCI 330 - The Unix System Numeric variables • Syntax: let varname=value • can be used for simple arithmetic: let count=1 let count=$count+20 let count+=1

  8. CSCI 330 - The Unix System Array variables • Syntax: varname=(list of words) • accessed via index: ${varname[index]} ${varname[0]} first word in array ${varname[*]} all words in array

  9. CSCI 330 - The UNIX System Using array variables Examples: % ml=(mary ann bruce linda dara) % echo ${ml[*]} mary ann bruce linda dara % echo ${ml[2]} bruce % echo ${#ml} 4

  10. CSCI 330 - The Unix System Exporting Variables • Environment variable is created by exporting shell variable • Syntax: export varname(s) declare –x varname(s)

  11. CSCI 330 - The Unix System Variables commands • To delete both local and environment variables unset varname • To prohibit change readonly varname • list all shell variables (including exported) set

  12. USING “SET” TO CHANGE OPTIONS • “set” is a builtin command of bash • “set +o” can be used to change an option • To keep I/O redirection from overwriting a file set +o noclobber CSCI 330 - The Unix System 12

  13. CSCI 330 - The Unix System variable manipulation • use portion of a variable’s value via: ${name:offset:length} • name – the name of the variable • offset – beginning position of the value • length – the number of positions of the value Example: % SSN="123456789" % password=${SSN:5:4} % echo $password % 6789

  14. CSCI 330 - The Unix System Special variable uses • ${#variable} number of characters in variable’s value • ${variable:-value} if variable is undefined use “value” instead • ${variable:=value} if variable is undefined use “value” instead, and set variable’s value • ${varname:?message} if variable is undefined display error “message”

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

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

  17. CSCI 330 - The Unix System Command History List • View or re-issue previously executed commands • Size of history can be set via shell variables: HISTSIZE=500 HISTFILESIZE=100 • Command line editing via keys: • 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

  18. CSCI 330 - The Unix System I/O Redirection

  19. CSCI 330 - The Unix System 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 File Descriptor

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

  21. CSCI 330 - The Unix System Quoting • Quoting allows you to distinguish between the literal value of a symbol and the symbols used as code • You need a way to distinguish between the literal symbol and the symbol’s use as a metacharacter or wild card characters • To do this you must use of the following symbols: • Backslash (\) • Single quote (‘) • Double quote (“)

  22. CSCI 330 - The Unix System A Backslash (\) • A backslash is also called the escape character • It allows you to preserve only the character immediately following it • For example: to create a file named “tools>”, enter: % touch tools\>

  23. CSCI 330 - The Unix System The Single Quote (‘) • A single quote is used to protect the literal meaning of metacharacters. • It protects all characters within the single quotes • The only character it cannot protect is itself • A single quote cannot occur with other single quotes even if preceded by a backslash • Examples: % echo 'Joe said 'Have fun'' Joe said Have fun % echo 'Joe said "Have fun"' Joe said "Have fun"

  24. CSCI 330 - The Unix System A Double Quote (“) • Double quotes protect all symbols and characters within the double quotes. • Double quotes will not protect these literal symbols: $ (dollar sign), ! (history event), and \ (backslash). • Examples: % echo "I've gone fishing'" I've gone fishing' % echo 'Jake won $500.00' Jake won $500.00 % echo "You've" earned '$5.00' You've earned $5.00

  25. CSCI 330 - The Unix System Command Substitution • Used to substitute the output of a command in place of the command itself • Two forms of command substitution: $(command) `command` Examples: % echo "User $(whoami) is on $(hostname)" User ege is on lx % echo "Today is" `date` Today is Sun Jul 17 08:06:28 CDT 2007

  26. CSCI 330 - The Unix System Using the “eval” Command • Evaluates a command line, performs all shell substitutions, and then executes the command line • Used when normal parsing of the command line is not enough • Results in a second round of variable substitution • Same capability as in C-shell

More Related