Bourne tutorial



Yüklə 478,18 Kb.
Pdf görüntüsü
səhifə12/20
tarix08.10.2017
ölçüsü478,18 Kb.
#3721
1   ...   8   9   10   11   12   13   14   15   ...   20

As you can see, the comments are echoed with the verbose flag. Also, each line is echoed before the variables

and the commands in backquotes are evaluated. Also note the "x" command echoes the assignment to

variables a and b on two lines, while the verbose flag echoed one line. Perhaps the best way to understand the

difference is the verbose flag echoes the line before the shell does anything with it, while the "x" flag causes

the shell to echo each command. Think of it as a case of Before and After.

Combining flags

You can combine the flags if you wish. Execute a script with

sh -x -v script

or more briefly

sh -xv script

Inside, you can use any of these commands

set -x -v

set -xv

set +x +v



set +xv

The first line of a script has an exception. You can use the format

#!/bin/sh -xv

but the following will not work:

#!/bin/sh -x -v

UNIX systems only pass the first argument to the interpreter. In the example above, the shell never sees the

"-v" option.

U - unset variables

Another useful flag for debugging is the "u" flag. Previously, I mentioned how the variable form "${x:?}"

reports an error if the variable is null or not set. Well, instead of changing every variable to this form, just use

the "-u" flag, which will report an error for any unset variable.

N - Bourne Shell non-execute flag

A simple way to check a complex shell script is the "-n" option. If set, the shell will read the script, and parse

the commands, but not execute them. If you wanted to check for syntax errors, but not execute the script, use

this command.

E - Bourne Shell exit flag

Bourne Shell Tutorial

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

32 of 66

11/21/2011 12:03 PM




I haven't discussed the exit status much. Every external program or shell script exits with a status. A zero

status is normal. Any positive value is usually an error. I normally check the status when I need to, and ignore

it when I don't care. You can ignore errors by simply not looking at the error status, which is the "$?" variable

I mentioned last time. (If the program prints error messages, you have to redirect the messages elsewhere).

Still, you may have a case where the script isn't working the way you expect. The "-e" variable can be used

for this: if any error occurs, the shell script will immediately exit. This can also be used to make sure that any

errors are known and anticipated. This would be very important if you wanted to modify some information,

but only if no errors have happened. You wouldn't want to corrupt some important database, would you?

Suppose the following script is executed:

#!/bin/sh

word=$1

grep $word my_file >/tmp/count



count=`wc -l 

echo I found $count words in my_file

The script searches for a pattern inside a file, and prints out how many times the pattern is found. The grep

program, however, exits with an error status if no words are found. If the "e" option is set, the shell terminates

before executing the count program. If you were concerned about errors, you could set the "e" option at the

beginning of the script. If you find out later that you want to ignore the error, bracket it with instructions to

disable the option:

set +e # ignore errors

grep $word my_file >/tmp/count

set -e


T - Bourne Shell test one command flag

Another way to make a script exit quickly is to use the "t" option. This causes the shell to execute one more

line, then exit. This would be useful if you wanted to check for the existence of a script, but didn't want it to

complete. Perhaps the script takes a long time to execute, and you just care if it's there. In this case, executing

sh -t script

will do this for you.

A - Bourne Shell mark for export flag

Previously, I mentioned you had to explicitly export a variable to place it in the environment, so other

programs can find it. That is, if you execute these commands

a=newvalue

newscript

The script newscript will now know the value of variable "a."

in the environment with

export a


Bourne Shell Tutorial

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

33 of 66

11/21/2011 12:03 PM




A second way to do this is to assign the variable right before executing the script:

a=newvalue newscript

This is an unusual form, and not often used. There is no semicolon on the line. If there was a semicolon

between the assignment and myscript, the variable "a" would not be made an environment variable.

Another way to do this is to set the "a" option:

set -a


If set, all variables that are modified or created will be exported. This could be very useful if you split one

large script into two smaller scripts, and want to make sure all variables defined on one script are known to

the other.

K - Bourne Shell keyword flag

While many of the options I have discussed are useful for debugging, or working around problems, other

options solve subtle problems. An obscure option is the "k" switch. Consider the following Bourne shell

command

a=1 myscript b=2 c d=3



When myscript executes, four pieces of information are passed to the program: The environment variable "a"

has the value 1. Three arguments are passed to the script: "b=2," "c," and "d=3."

Any assignment on the same line as a command is made an environment variable, but only if it's before the

command. The "-k" options changes this All three assignments become environment variables, and the script

only sees one argument.

H - Bourne Shell hash functions flag

I've read the manual page, and was unclear. This seems to be a way to speed up program executings by

pre-storing the paths for each command. The Bash manpage says this is enabled by default. "-h" option.

The $- variable

As I mentioned, you can use the set command to change the value of these flags. However, it cannot be used

to check the values. There is a special variable, called "$-," which contains the current options. You can print

the values, or test them. It has another use. Suppose you had a complex script that called other scripts.

Suppose you wanted to debug all of the scripts. You could modify every script, add the option you wanted.

That is, assume, newscript might contain

#!/bin/sh

myscript arg1 arg2

If this is replaced by

#!/bin/sh

Bourne Shell Tutorial

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

34 of 66

11/21/2011 12:03 PM




Yüklə 478,18 Kb.

Dostları ilə paylaş:
1   ...   8   9   10   11   12   13   14   15   ...   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ə