agenda the bourne shell – part ii special characters ambiguous file reference variable names and...

20
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional Parameters)

Upload: franklin-houston

Post on 27-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Agenda

The Bourne Shell – Part II

• Special Characters

• Ambiguous File Reference

• Variable Names and Values

• User Created Variables

• Read-only Variables (Positional Parameters)

Page 2: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Special Characters

• A special character is used to give special meaning to a file or string when used with a UNIX command or when executing shell scripts. For example redirection (>, >>, <)

• Special characters are used for:– Wildcard searches

– Redirecting standard output

– Displaying variable contents

Page 3: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Special Characters

• You should avoid using special symbols when creating files. You should also be aware that special symbols when used in shell scripts can cause problems (shell may interpret them differently than user)

Examples: > < * ? ‘ “ \ $ ; / `

Page 4: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Quoting text When creating shell scripts, you may want to make reference to

text that includes a special character. To make the shell “ignore” the meaning of the special character you use quotes.

Types of Quotes:

Single Quotation marks: echo ‘*’ Double Quotation marks: echo “*” Backslash: echo \*

Note: A backslash must appear for every occurrence of a special symbol – we will discuss more about quoting text later in this course…

Page 5: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Ambiguous File References(Wildcard Searches)

• You may need to access or process a file, but forget what the exact spelling (syntax) of the filename.

• You may also want to run a shell script that accesses many files that share a similar characteristic (eg all files with extension “txt”). Files that match a pattern (as opposed to being referenced by an exact filename) is called an ambiguous file reference

Page 6: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

The * character

This wildcard character is used to represent any character. It is useful when searching for file names sharing common characteristics.

For Example: ls file* (list all files beginning with “file”) ls –a .* (list all hidden files)

Page 7: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

The ? character

The wildcard character ? will match only a single character.

ls file? (list all files with only 1 character after “file”)

ls file?? (list all files with only 2 characters after “file”)

ls ??45? (list all files containing 5 characters that contain a 4 in 3rd position and a 5 in the 4th position)

Page 8: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Character Class [ ]

Used to match a particular group or “class” of a single character. Similar to using “?” but the user can determine what “?” is allowed or not allowed to represent.

Examples

ls file[abc].txt displays filea.txt, fileb.txt, filec.txt ls file[1-9].txt displays a range file1.txt to file9.txt

cat file[!abd].doc displays all other files thanfilea.doc, fileb.doc or filed.doc

Page 9: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Variable Names ($)

The shell has the ability for users to assign values to a variable.

There are basically two general categories of variables in UNIX:

– Shell Variables (Assigned by the Shell)

– User-Created Variables (Assigned by the user)

Page 10: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Shell Variables

There are two categories of shell variables:– Keyword Variables

• Variables that have been assigned an easy to remember word that are commonly used

• Examples include PATH, VISUAL, PS1, PS2, etc…

– Read-only Shell Variables(Positional Parameters)

• Variables that are assigned by a command (set) as opposed to assigning by using the equal sign (=)

• Examples include $0, $1, $2, $3, … $9

Page 11: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

User-Created Variables

• A user-created variable must begin with a letter.

• You must not include white-space on either side of the equals sign– For example: cat >test.file name=Murray <CTRL><D>

Page 12: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

User-Created Variables

In order to access or display the value of the variable, a dollar sign $ must appear before the variable name - otherwise, it would be interpreted as text.– For Example:

cat > test2.file

name=Murray

echo $name

<CTRL><D>

Page 13: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

User-Created Variables

You can use single or double quotes to assign an entire string of text– For Example: name=‘Murray Saul’ name=“Murray Saul”

Note: Single quotes (‘ ’) and backslash (\) will turn-off meaning of special characters including the variable name eg. echo ‘$name’ (Double quotes will allow variable to display value!!)

Page 14: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

User-Created Variables

To remove a previously stored variable, assign a “null” value to the variable or use unset command

Example: name= unset name

Page 15: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Reading & Storing InputFrom the User

The read command is used to pause and prompt the user to enter a line of text.

Example: echo “Enter your full name \c” read fullname echo $fullname

\c within quotes prompts user for data on same line as message...

Page 16: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Read-Only Shell Variables(Positional Parameters)

• Read-Only shell variables have the feature to store commands and relating arguments as well as command output.

• You can use the set command to assign values to these read-only shell variables.

• You can also create shell scripts and have arguments after script name automatically stored in read-only shell variables

Page 17: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Read-Only Shell Variables

• Read only variables range from $0 to $9

• $0 is name of command used to call script

• $1 to $9 are first nine positional parameters, or command line arguments

• Read-Only Shell Variables need to be set

• For Example– set name1 name2 name3

$0 = set$1 = name1$2 = name2$3 = name3

Page 18: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Examples of Read-Only Variables

• Try to determine output from the following commands:

set date echo $0 $1 $2

set whoami echo $1 $2

set name1 name2 name3 echo $*

set `date` echo $0 $1 $2 $3

$* matches all variables

Can use Read-Only Variables to read output from executed command (must use ` symbol around command)

Page 19: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Exporting a Variable

• As mentioned in a previous lesson, UNIX runs a series of operations or “processes”.

• Processes can start other operations or “child processes”

• Unfortunately, variables are usually only stored in the current or “local” process.

• Therefore, the export command is used to transfer or “export” the variable to the child processes (if they are required)

Page 20: Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional

Exporting a Variable

• Usage: export variable_name

– During the weekend, check your .profile to see if there are any variables exported - we will discuss this next week…