Top Banner
CSCI 330 THE UNIX SYSTEM Bash shell
26

CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

CSCI 330THE UNIX SYSTEM

Bash shell

Page 2: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

BASH SHELL SPECIFICS

Shell login and logout files Shell variables Prompt History IO redirection

2

CS

CI 330 - T

he Unix S

ystem

Page 3: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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

3

CS

CI 330 - T

he UN

IX S

ystem

Page 4: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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_logout4

CS

CI 330 - T

he Unix S

ystem

executed for login shell

executed for non-login shell

Page 5: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

PREDEFINED SHELL VARIABLES

Shell Variable

Description

PWD The most recent current working directory.

OLDPWD The previous working directory.

BASH The full path name used of the bash shell.

RANDOM Generates a random integer between 0 and 32,767

HOSTNAME The current hostname of the system.

PATH A list of directories to search of commands.

HOME The home directory of the current user.

PS1 The primary prompt (also PS2, PS3, PS4).

CS

CI 330 - T

he Unix S

ystem

5

Page 6: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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”

6

CS

CI 330 - T

he Unix S

ystem

Page 7: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

NUMERIC VARIABLES

Syntax:

let varname=value

can be used for simple arithmetic:

let count=1

let count=$count+20

let count+=1

7

CS

CI 330 - T

he Unix S

ystem

Page 8: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

ARRAY VARIABLES

Syntax:

varname=(list of words)

accessed via index:

${varname[index]}

${varname[0]} first word in array${varname[*]} all words in array

8

CS

CI 330 - T

he Unix S

ystem

Page 9: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

USING ARRAY VARIABLES

Examples:

% ml=(mary ann bruce linda dara)

% echo ${ml[*]}

mary ann bruce linda dara

% echo ${ml[2]}

bruce

% echo ${#ml}

4

9

CS

CI 330 - T

he UN

IX S

ystem

Page 10: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

EXPORTING VARIABLES

Environment variable is created by exporting shell variable

Syntax: export varname(s)

declare –x varname(s)

10

CS

CI 330 - T

he Unix S

ystem

Page 11: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

VARIABLES COMMANDS

To delete both local and environment variables

unset varname

To prohibit changereadonly varname

list all shell variables (including exported)set

11

CS

CI 330 - T

he Unix S

ystem

Page 12: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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 fileset +o noclobber

12

CS

CI 330 - T

he Unix S

ystem

Page 13: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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

13

CS

CI 330 - T

he Unix S

ystem

Page 14: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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”

14

CS

CI 330 - T

he Unix S

ystem

Page 15: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

BASH SHELL PROMPT

can be set via “PS1” shell variable

Example: % PS1="$USER > "

z036473 >

Secondary prompts:PS2, PS3, PS4

15

CS

CI 330 - T

he UN

IX S

ystem

Page 16: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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: 16

CS

CI 330 - T

he UN

IX S

ystem

Page 17: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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

17

CS

CI 330 - T

he Unix S

ystem

Page 18: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

I/O REDIRECTION

18

CS

CI 330 - T

he Unix S

ystem

Command Syntax Short Description

cmd > file Send output of cmd to file

cmd >> file Append output of cmd to file

cmd < file Take input from file

cmd << text Read stdin up to a line identical to text a.k.a “here command”

Page 19: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

FILE DESCRIPTOR

19

CS

CI 330 - T

he Unix S

ystem

positive integer for every open file process tracks its open files with this

number

0 – standard input1 – standard output2 – standard error output

bash uses file descriptor to refer to a file

Page 20: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

REDIRECTION SYNTAX

Output:> or 1> filename 2> filename

Input:< or 0<

Combining outputs:2>&1

Example: % cat hugo > /tmp/one 2>&1

20

CS

CI 330 - T

he Unix S

ystem

Page 21: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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 (“)

21

CS

CI 330 - T

he Unix S

ystem

Page 22: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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\>

22

CS

CI 330 - T

he Unix S

ystem

Page 23: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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"23

CS

CI 330 - T

he Unix S

ystem

Page 24: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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.0024

CS

CI 330 - T

he Unix S

ystem

Page 25: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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 25

CS

CI 330 - T

he Unix S

ystem

Page 26: CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -

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

26

CS

CI 330 - T

he Unix S

ystem