Can someone point me in the direction of an example that
will help me do this?
*
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.
*
Thanks
*
Olly
*
--
G2 Support
Network Support : Online Backups : Server Management
*
*
Tel:*** 0845 307 3443
Email:* oliver.marshall@g2support.com
Web:*** http://www.g2support.com
Twitter: g2support
Newsletter: http://www.g2support.com/newsletter
Mail:** 2nd Floor, 130a Western Rd, Brighton,
Sussex, BN12LA
*
G2 Support LLP is registered
at Mill House, 103 Holmes Avenue, HOVE
BN3 7LE. Our registered
company number is OC316341.
*
*
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-12-2009, 08:13 AM
Markus Schönhaber
Script loop question
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
--
Regards
mks
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-12-2009, 08:16 AM
Oliver Marshall
Script loop question
Thanks for that. I've also just found the magic shift command which is superb (MS are you listening?). Using it like this;
# Loop until all parameters are used up
while [ "$1" != "" ]; do
echo "Parameter 1 equals $1"
echo "You now have $# positional parameters"
# Shift all the parameters down by one
shift
done
--
G2 Support
Network Support : Online Backups : Server Management
-----Original Message-----
From: ubuntu-users-bounces@lists.ubuntu.com [mailto:ubuntu-users-bounces@lists.ubuntu.com] On Behalf Of Markus Schönhaber
Sent: 12 November 2009 09:13
To: ubuntu-users@lists.ubuntu.com
Subject: Re: Script loop question
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
--
Regards
mks
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
11-12-2009, 10:02 AM
Derek Broughton
Script loop question
Oliver Marshall wrote:
> Thanks for that. I've also just found the magic shift command which is
> superb (MS are you listening?).
Huh? DOS always had that...
--
derek
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>> 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
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users