Bourne tutorial



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

SHACCT - Accounting file

This variable defines the accounting file, used by the acctcom and acctcms commands.

MAILPATH - searchpath for mail folders

The "MAILPATH" variable lists colon-separated filenames. You can add a "%" after the filename, and

specify a special prompt for each mailbox.

In addition, several environment variables are specified by the login process. "TERM" defines the terminal

type, and "USER" or "LOGNAME" defines your user ID. "SHELL" defines your default shell, and "TZ"

specifies your time zone. Check the manual pages, and test your own environment to find out for sure. The

external program "env" prints all current environment variables.

Bourne Shell Variables - Alternate Formats

Earlier, I discussed simple variables in the Bourne shell. Now is the time to go into more detail. Suppose you

wanted to append a string to a variable. That is, suppose you had a variable "X" with the value of "Accounts,"

but you wanted to add a string like ".old," or "_new" making "Accounts.old" or "Accounts_new," perhaps in

an attempt to rename a file. The first one is easy. The second requires a special action. In the first case, just

add the string

mv $X $X.old

The second example, however, does not work:

mv $X $X_new # WRONG!

The reason? Well, the underscore character is a valid character in a variable name. Therefore the second

example evaluates two variables, "X" and "X_new." If the second one is undefined, the variable will have a

value of nothing, and the shell will convert it to

mv Accounts

The mv command will take the offered arguments, and complain, as it always wants two or more variables. A

similar problem will occur if you wish to add a letter or number to the value of a variable.

Using quoting and shell variables

There are several solutions. The first is to use shell quoting. Remember, quoting starts and stops the shell from

treating the enclosed string from interpretation. All that is needed is to have a quote condition start or stop

between the two strings passed to the shell. Place the variable in one string, and the constant in the other. If

the variable "x" has the value "home," and you want to add "run" to the end, all of the following

combinations are equal to "homerun:"

$x"run"

$x'run'


Bourne Shell Tutorial

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

18 of 66

11/21/2011 12:03 PM




$xrun

$x''run


$x""run

"$x"run


Using curly braces with variables

There is another solution, using curly braces:

${x}run

This is a common convention in UNIX programs. The C shell also uses the same feature. The UNIX make



utility uses this in makefiles, and requires braces for all variable references longer than a single letter. (Make

uses either curly braces or parenthesis).

This form for variables is very useful. You could standardize on it as a convention. But the real use comes

from four variations of this basic form, briefly described below:

+-------------------------------------------------------------+

|Form


    Meaning

      |


+-------------------------------------------------------------+

|${variable?word}   Complain if undefined

      |

|${variable-word}   Use new value if undefined



      |

|${variable+word}   Opposite of the above

      |

|${variable=word}   Use new value if undefined, and redefine. |



+-------------------------------------------------------------+

Why are these forms useful? If you write shell scripts, it is good practice to gracefully handle unusual

conditions. What happens if the variable "d" is not defined - and you use the command below?

d=`expr $d + 1`

You get "expr: syntax error"

The way to fix this is to have it give an error if "d" is not defined.

d=`expr "${d?'not defined'}" + 1`

The "?" generates an error: "sh: d: not defined"

If instead, you wanted it to silently use zero, use

d=`expr "${d-0}" + 1`

This uses "0" if "d" is undefined.

If you wish to set the value if it's undefined, use "="

echo $z

echo ${z=23}



echo $z

The first echo outputs a blank line. The next 2 "echo" commands output "23."

Note that you can't use

new=`expr "${old=0}" + 1`

to change the value of "old" because the expr command is run as a subshell script, and changing the value of

Bourne Shell Tutorial

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

19 of 66


11/21/2011 12:03 PM


"old" in that shell doesn;t change the value in the parent shell.

I've seen many scripts fail with strange messages if certain variables aren't defined. Preventing this is very

easy, once you master these four methods of referring a Bourne shell variable. Let me describe these in more

detail.


${variable?value} - Complain if undefined

The first variation is used when something unusual happens. I think of it as the "Huh???" option, and the

question mark acts as the mnemonic for this action. As an example, assume the following script is executed:

#!/bin/sh

cat ${HOME}/Welcome

But suppose the environment variable "HOME" is not set. Without the question mark, you might get a strange

error. In this case, the program cat would complain, saying file "/Welcome" does not exist. Change the script

to be


#!/bin/sh

cat ${HOME?}/Welcome

and execute it, and you will get the following message instead:

script: HOME: parameter null or not set

As you can see, changing all variables of the form "$variable" to "${variable?}" provides a simple method to

improve the error reporting. Better still is a message that tells the user how to fix the problem. This is done by

specifying a word after the question mark. Word? Yes, the manual pages says a word. In a typical UNIX-like

way, that word is very important. You can place a single word after the question mark. But only one word.

Perfect for one-word insults to those who forget to set variables:

cat ${HOME?Dummy}/Welcome

This is a perfect error message if you wish to develop a reputation. Some programmers, however, prefer to

keep their jobs and friends. If you fall into that category, you may prefer to give an error message that tells the

user how to fix the problem. How can you do that with a single word?

Remember my discussion earlier on quoting? And how the shell will consider a whitespace to be the end of

the word unless quoted? The solution should be obvious. Just quote the string, which makes the results one

word:


cat ${HOME?"Please define HOME, and try again"}/Welcome

Simple, yet this makes a shell script more user-friendly.

${variable-default} - Use default if undefined

The next variation doesn't generate an error. It simply provides a variable that didn't have a value. Here is an

example, with user commands in boldface:

$ echo Y is $Y

Bourne Shell Tutorial

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

20 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   10   ...   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ə