sections in this module | City
College of San Francisco - CS160B Unix/Linux Shell Scripting Module: Advanced Topics |
module list |
Example 1
In Example 1 of Examples-Loops2, we had the following code:
while read file ; do
Change the lines highlighted in bold to use substring operators.
Example 2
In Example 2 of Examples-Loops2, we had the following line of code
members=$(echo "$group" | cut -d: -f4 | tr ',' ' ')
Rewrite the line of code to use substring operators rather than the external commands cut and tr (there are 4 colon-separated fields in $group)
Example 3
You have logged onto a new linux system. You are looking through a shell script you found and encounter the lines
Explain what they mean
Example 4
You are writing a shell script and want to use command tracing to trace the execution of your commands to try to find a bug. Explain how you could run your shell script with tracing on.
Example 5
What does the message ok in the following line of code mean?
[ "${file##*.}" = doc ] && echo ok
Answers
Example 1
while read file ; do
Example 2
Since field 4 is the last colon-delimited field of "$group", this is pretty easy:
Example 3
Normally, shell scripts want the substitution of a variable that does not exist to simply output the empty string. set -o nounset changes this behavior. When the nounset option is turned on in the shell, the expansion of unset variables results in an error:$
unset field
$ echo $field
$ set -o nounset
$ echo $field
bash: field: unbound
variable
$
A variable that has been set to empty is not considered unset.
The allexport option to the shell means that all variables that are created or modified in the future will be marked as envionment variables (i.e., exported). It does not affect existing local variables, unless you modify them.
Example 4
You need to turn on the xtrace (-x) option for the shell script. You could do this in any of the following ways:
$ bash -x fixfiles args
where args is replaced with the normal arguments to fixfiles
Example 5
In the command
[ "${file##*.}" = doc ] && echo ok
The longest string ending in a period is stripped off from the left side of $file. The result is compared to the string doc. This is an attempt to determine whether "$file" ends in .doc . You will probably find it easier to understand than the usual construct
[ "${file%.doc}" != "$file" ] && echo ok
As far as I can tell, there is only one difference: if $file is exactly doc , ok will be output.
Prev | This page was made entirely
with free software on linux: Kompozer and Openoffice.org |
Next |