princem3
12-22-2003, 12:52 PM
I have a few quick things that I'm not too clear so if you are clearer on them than me just give a simple yes or no to each.
1. In the traditional Bourne Shell, is a sub-shell is created when there is I/O redirection to a loop.
2. when wirint scripts does Filename generation occurs on variable assignment
3. And what's the difference between these 3 commands:
for I in "$*"
do
echo $I
done
-----------------------------------------------
for I in "$@"
do
echo $I
done
----------------------------------------------
for I
do
echo $I
done
bwkaz
12-22-2003, 04:04 PM
I don't know about 1. I remember it was the subject of discussion a while ago here, though. The post had something to do with adding up the set of numbers echoed into a loop, or something, and returning the value in a variable. It didn't work because the loop (or something) was being done in a subshell.
Maybe it depended on the loop condition? Wish I remembered...
2. when wirint scripts does Filename generation occurs on variable assignment I'm not sure what you mean by "when wirint scripts", but filename expansion does not seem to occur when you assign to variables. At least, not in my bash. To check for yourself, create two files starting with the same letter (say, k), and do this:
TEST=k*
set Look through the output of "set" for the TEST variable. It'll probably be set to 'k*', without expansion having been done.
3. And what's the difference between these 3 commands:
for I in "$*"
-----------------------------------------------
for I in "$@"
----------------------------------------------
for I The last one I'm not sure on.
But the "$*" in the first one will expand to one single word containing all the positional parameters. The "$@" in the second one will expand to exactly the positional parameters -- if whitespace is included in any of the parameters, then that parameter will expand to one single word.
Maybe an example will make it a bit more clear. If I call a script with scriptname param1 param2 "param number 3" (so that the third parameter actually has two embedded spaces), then "$*" will expand to this:
"param1 param2 param number 3"
(i.e., one word containing everything). "$@" will expand to this:
"param1" "param2" "param number 3"
(i.e., three words, replicating the positional parameters exactly).
According to the bash manpage, the third for loop seems to be equivalent to the second one.
jim mcnamara
12-22-2003, 04:13 PM
$* is all of the arguments that were passed to the script - as one long string
"$@" --you need the quotes -- all of the arguments as separated strings, ie., "$0" "$1" ... etc.
$# is the number of aguments passed to the script
for i in I
do
echo "$i"
done
will echo the letter I. Generally this construct is used to list files
for i in I*
do
echo "Filename: $i"
# do something with the file
cat $i >> bigfile
done
This is one way to selectively feed files to your script to process.
This concatenate all of the files in the directory that start with capital I into one file == bigfile