Script loop question

Cameron Hutchison lists at xdna.net
Thu Nov 12 22:45:47 UTC 2009


=?UTF-8?B?TWFya3VzIFNjaMO2bmhhYmVy?= <ubuntu-users at list-post.mks-mail.de> writes:

>Oliver Marshall:

>> I have a script that takes an argument (a file name) and performs
>> actions on that file. I want to mod the script so that i can pass, as
>> arguments, multiple filenames and have the script perform the same
>> set of commands on each file in turn, like this;
>> 
>> ./filename.sh file1 file2 file3
>> 
>> etc.
>> 
>> I guess it's bringing the two parts of the loop and the arguments
>> together to loop through each argument in turn, but that's where I
>> get stuck.

>for f in "$@"; do
>  echo $f
>done

for f ; do
  echo "$f"
done

Two things:
1) 'in "$@"' is redundant. It is the default in a for loop
2) always quote expansions - i.e. "$f", so that spaces are properly retained





More information about the ubuntu-users mailing list