Bourne tutorial



Yüklə 478,18 Kb.
Pdf görüntüsü
səhifə5/20
tarix08.10.2017
ölçüsü478,18 Kb.
#3721
1   2   3   4   5   6   7   8   9   ...   20

Mixing quotation marks

Having two types of quotation marks simplifies many problems, as long as you remember how

meta-characters behave. You will find that the easiest way to escape a quotation mark is to use the other form

of quotation marks.

echo "Don't forget!"

echo 'Warning! Missing keyword: "end"'

Quotes within quotes - take two

Earlier I showed how to include a quote within quotes of the same kind. As you recall, you cannot place a

single quote within a string terminated by single quotes. The easiest solution is to use the other type of

quotation marks. But there are times when this is not possible. There is a way to do this, but it is not obvious

to many people, especially those with a lot of experience in computer languages. Most languages, you see, use

special characters at the beginning and end of the string, and has an escape to insert special characters in the

middle of the string. The quotation marks in the Bourne shell are not used to define a string. There are used to

disable or enable interpretation of meta-characters. You should understand the following are equivalent:

echo abcd

echo 'abcd'

echo ab'c'd

echo a"b"cd

echo 'a'"b"'c'"d"

The last example protects each of the four letters from special interpretation, and switches between strong

and weak quotation marks for each letter. Letters do not need to be quoted, but I wanted a simple example. If

I wanted to include a single quote in the middle of a string delineated by a single quote marks, I'd switch to

the different form of quotes when that particular character is encountered. That is, I'd use the form

'string1'"string2"'string3'

where string2 is a single quote character. Here is the real example:

$ echo 'Strong quotes use '"'"' and weak quotes use "'

Strong quotes use ' and weak quotes use "

It is confusing, but if you start at the beginning, and following through, you will see how it works.

Placing variables within strings

Change the quoting mid-stream is also very useful when you are inserting a variable in the middle of a string.

You could use weak quotes:

echo "My home directory is $HOME, and my account is $USER"

You will find that this form is also useful:

echo 'My home directory is '$HOME', and my account is '$USER

Bourne Shell Tutorial

http://www.grymoire.com/Unix/Sh.html

12 of 66

11/21/2011 12:03 PM




When you write your first multi-line awk or sed script, and discover you want to pass the value of a variable

to the middle of the script, the second form solves this problem easily.

Variables

The Bourne shell has a very simple syntax for variables:

variable=value

The characters used for variable names is limited to letters, numbers and the underscore character. It cannot

start with a number. Unlike the C shell, spaces are important when defining Bourne shell variables.

Whitespace (spaces, tabs or newlines) terminate the value. If you want whitespace in a variable, it must be

quoted:

question='What is the filename? '



Multiple assignments can be placed on one line:

A=1 B=2 C=3 D=4

Do not put a space after the equals sign. This terminates the value. The command

a=date


sets the variable "a" to be equal to "date," but the command

a= date


sets "a" to be the empty string, and executes the date command. The "date" command? Yes. Which

introduces...

A subtle point

Notice how two commands are executed on one line: the variable is changed, and the "date" program is

executed. It is not obvious that this is valid. The manual page doesn't mention this. Even stranger is some

commands can be executed, while others cannot. The "date" command is an external program. That is, the

command is not built into the shell, but is an external executable. Other commands are internal command,

built into the shell. "Echo" and "export" are shell built-in commands, and can follow the variable assignment.

You might see an environment variable defined like this:

VAR=/usr/lib; export VAR

But the following works just as well:

VAR=/usr/lib export VAR

Some of the built--in commands cannot be on the same line, like "for" or "if." The "echo" command does, but

it may not do what you think. Don't believe me? I'll give you an example, and you have to guess what the

results will be. I suspect that that 99.9999% of you would guess wrong. Put on your thinking caps. You'll need

it. UPDATE - this information gives different results than when I wrote it in 1996. See below

Bourne Shell Tutorial

http://www.grymoire.com/Unix/Sh.html

13 of 66

11/21/2011 12:03 PM




Ready?

What does the following Bourne shell commands do?

a=one; echo $a

a=two echo $a

a=three echo $a >$a

I have to be honest. I failed the test myself. Well, I got partial credit. But I wrote the quiz. The first line is

simple: "one" is output to the screen. The second line behaves differently. The value of variable "a" is set to

"two," but the echo command outputs "one." Remember, the shell reads the lines, expands metacharacters,

and then passes it to the programs. The shell treats built-in commands like external commands, and expands

the meta-characters before executing the built-in commands. Therefore the second line is effectively

a=two echo one

and then the command is executed, which changes the value of the variable after it is used.

Ready for a curve ball? What does the last line do? It creates a file. The file contains the word "two." For

$64,000 and a trip to Silicon Valley, what is the name of the file that is created? For those to thought the

answer is "two," I'm terrible sorry, you didn't win the grand price. We do have a nice home version of this

game, and a year's supply of toothpaste. The correct answer is "three." In other words

echo $a >$a

is interpreted as

echo second >third

I am not fooling you. The variable "$a" has two different values on the same line! The C shell doesn't do this,

by the way.

2011 Update - I just tried this on several systems to see what happens. On an old Sun system, it behaved as I

noted. Note that this was the Bourne shell, and not Bash.

I tried it using the Bash shell on a 2.2 Linux system. The third line created a file called "three" but it

contained the string "one"!.

I also tried it on a Ubuntu 10.04 system, and line three generated the error

bash: $a: ambiguous redirect

Just consider this an example where the behavior is unpredictable. Now let's continue in the tutorial.]

The Bourne shell evaluates metacharacters twice: one for the commands and arguments, and a second time

for file redirection. Perhaps Mr. Bourne designed the shell to behave this way because he felt

a=output >$a

ought to use the new value of the variable, and not the value before the line was executed, which might be

undefined, and would certainly ave undesirable results. Although the real reason is that the above command

treats the "a" variable like an environment variable, and sets the variable, marks it for export, and then

Bourne Shell Tutorial

http://www.grymoire.com/Unix/Sh.html

14 of 66

11/21/2011 12:03 PM




Yüklə 478,18 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   20




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə