You are not logged in.

#1 2012-05-10 01:36:39

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Non-numerical for loop in bash?

How can I write non-numerical for loop in bash?

for example.. I want something like this....

for cName in ('bzip2' 'bunzip2' 'gzip' 'gunzip')
do
    time $cName -kf $filename
done

instead of writing this...

time bzip2 -kf $filename
time bunzip2 -kf $filename
time gzip -kf $filename
time gunzip -kf $filename

Can this be done? (I have larger set of tools I want to run that have same flags and stuff like that in the end... not just 4)

Last edited by kdar (2012-05-10 02:03:12)

Offline

#2 2012-05-10 01:52:17

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Non-numerical for loop in bash?

Sure, just get rid of the parenthesis. Please quote properly to save yourself some headaches in the future.

Last edited by falconindy (2012-05-10 01:52:51)

Offline

#3 2012-05-10 02:02:58

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: Non-numerical for loop in bash?

Thanks.

Is it better to use double quotes?

Last edited by kdar (2012-05-10 02:10:49)

Offline

#4 2012-05-10 02:27:21

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: Non-numerical for loop in bash?

kdar wrote:

Thanks.

Is it better to use double quotes?

I think Falconindy meant to put double quotes around the variables when you use them. Think about what would happen if $filename had a space in it. Try running these two examples:

filename="foo bar"
for i in $filename; do
    echo $i
done
filename="foo bar"
for i in "$filename"; do
    echo $i
done

Offline

Board footer

Powered by FluxBB