FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-12-2009, 07:59 AM
Oliver Marshall
 
Default Script loop question

Hi chaps,


*


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
 
Old 11-12-2009, 08:13 AM
Markus Schönhaber
 
Default 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
 
Old 11-12-2009, 08:16 AM
Oliver Marshall
 
Default 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

Web: www.g2support.com
Twitter: g2support
Newsletter: www.g2support.com/newsletter


-----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
 
Old 11-12-2009, 10:02 AM
Derek Broughton
 
Default 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
 
Old 11-12-2009, 09:45 PM
Cameron Hutchison
 
Default Script loop question

=?UTF-8?B?TWFya3VzIFNjaMO2bmhhYmVy?= <ubuntu-users@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


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 07:50 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org