agenda the bourne shell – part ii special characters ambiguous file reference variable names and...
TRANSCRIPT
Agenda
The Bourne Shell – Part II
• Special Characters
• Ambiguous File Reference
• Variable Names and Values
• User Created Variables
• Read-only Variables (Positional Parameters)
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
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: > < * ? ‘ “ \ $ ; / `
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…
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
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)
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)
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
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)
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
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>
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>
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!!)
User-Created Variables
To remove a previously stored variable, assign a “null” value to the variable or use unset command
Example: name= unset name
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...
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
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
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)
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)
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…