Concepts:Chapter 10
|
script |
command |
result |
---|---|---|
extest2 |
export
cheese=american |
Create an exported variable called
cheese, set equal to American |
echo
"$0 1: $cheese" |
extest2
1: American |
|
./subtest |
Calls the subtest script. It inherits
the cheese variable and its value. |
|
subtest |
echo "$0 1: $cheese" | subtest
1: American |
cheese=swiss |
changes value of the inherited
variable to Swiss |
|
echo "$0 2: $cheese" | subtest
2: Swiss |
|
extest2 | echo "$0 2: $cheese" | The subtest script ends, control
returns to next line in extest2 script: extest2 2: American |
What happened?
On page 472, the text explores a variable
that has no value. If we declare
or export a variable, it exists.
However, if we do not assign a value to it, the shell typically assumes
that the variable holds an empty string. That is pretty accurate, and
it is is more intuitive than the three alternatives proposed by the text
that either use or assign
a default value, or assign an
error message if the variable
is empty. This concept is a little slipshod for my taste. If your variable
needs a value, give it one, and change it when needed.
Page 474 bring us to the subject of arrays.
The author spends just over a page on them, and does not make his points
very clearly. Let me suggest that you should read this
article on the subject on the Linux Journal web site. In Bash, an
array is a series of memory locations that can be accessed together or
individually. The article behind the link in the sentence above starts
with an example.
arr=(Hello World)
This looks a lot like assigning some words to a variable. In this case,
we did not use quotation marks. We used a set of parentheses to enclose
the words. This makes arr the name of an array. We can access each member
of the array by using the name of the array followed by square brackets
enclosing a number that represents the order of the members.
arr[0]=Hello
arr[1]=World
This pair of commands would have accomplished the same thing as the previous example: arr is the name of an array, its first member, identified by index 0, is Hello, and its second member, identified by index 1, is World. Note that this method did not require the use of parentheses.
So, that is two methods to create an array and load some values into it. As the text and the article mention, it is not quite so easy to read what is in an array. Normally, you deal with one item in a array at a time. An example in the text shows how to use the proper notation with echo:
echo ${arrayname[index]}
Using the example array above,
we could enter the command echo ${arr[0]}
if we wanted to see the first item in that array, which is Hello.
Basic syntax rule: the index value is enclosed by square brackets. The entire reference to the array item (name and index) is enclosed by curly braces. A dollar sign is needed to the left of the left curly brace to read the value stored in the array item. The text makes a point of stating that you must use both the square brackets and the curly braces. If you stick with this notation and remember the first item in the array is at index [0], you should be fine when reading from and writing to the array items.
The text becomes difficult to understand for the rest of its discussion. Let's move ahead to the bottom of page 475, Functions often have variables in them, however, the point of this discussion is that a script that calls a function automatically gives that function access to all variables in the script. You may be filled with indignation at this idea, but it is so. The text explains that a function called from a script is operating in the same environment as that script, so it has access to the same environment variables, not copies of them. This solves the problem you may have seen in the discussion of inheritance. A script and the functions it calls can access the same variables, so they can be changed by either the script or by a function, and the change matters to the other one. This benefit, however, leads to the opposite problem we saw under inheritance.
Suppose a function is useful. Suppose it is popular and it is used by most of the programmers on our staff of many. We make sure that it is available in a startup file on our Linux workstations. Now suppose that the programmer who wrote that function and the next programmer to use it both used the same variable name in their respective work, This variable is changed in the script and changed in the function, in different counterproductive ways. Well, what do we do about that?
When we write a function, we may want to make sure that variables that are meant to have meaning only to the function are created with a modifier that limits their visibility. That modifier is local. The text offers an example of how this might be used:
This way, what happens inside the function, like what happens in Vegas, stays inside the function. Well, maybe its even more so for the function. Anyway, its a better way to write your functions if there is a possibility of the function changing something it should not change.
The text brings up a term that it has used several times, but not explained too well. Builtin commands are commands that are part of the shell. Some commands are stored in separate files, typically stored in a bin folder somewhere. Builtin commands are not separate, so when you call one of them in the shell, a builtin command does not start a new process, it simply continues the process that is the shell. The text discusses six builtin commands (for fifteen pages!), then offers a list of more on pages 491 and 492. A point the text made previously is that if a command is a builtin, it will run before any command in a bin folder is run, making it hard to execute the binary version.
This is an interesting point, but not worth a lot to us. It is more valuable to know what some of these commands do, so look through this section of the chapter to learn about more commands you can use in scripts.
Page 492 returns to doing math in a shell script. We see three acceptable syntaxes for an equation:
There is more on this page, but three ways of writing math statements are enough. The next two pages cover more examples of conditional testing, reminding us that a conditional test will evaluate to being true or false. Starting on page 495, there is a list of math operators that can be used with variables. The ++ and -- operators are handy in programs, and are worth some attention. First, be aware of the two operators. Then, worry about how their position affects what they do. Hang on, it isn't that bad.
The text continues with several familiar math operators. Look them over, and do a web search for any you don't know about. Note that some symbols are used for more than one function, so discuss what you are not sure about in class.
On page 499, the text reminds us that the && and || operators can stand for Boolean concepts (AND and OR, respectively) but they mean something else when they separate two commands.
The single pipe character takes the output from the process on the left and supplies it as input to the process on the right. This is still true if the pipe character is the last
character on a line. In this case, the next command line is assumed as
the process that is receiving the output of the process on the first
line.
Turn to page 501 to see a pretty dry discussion of recursion, a method
of doing something that requires your method to call itself as needed.
The text mentions that it should be possible to rewrite any script requiring
recursion with an appropriate loop. Take a look at the material and decide
which makes more sense to you for the second assignment for this week.
Week 6 Assignments: Assignment 1: Write a script that loads an array with a
sentence. Write a loop that displays the sentence by echoing
each member of the array. Assignment 2: Write a script that will ask the user for a positive
integer. The script must then calculate and display the factorial
of that integer. This can be done with recursion, or with a loop.
Explain your choice of method, and submit your script
that meets this objective.
|