The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl + array and incrementing number Optimus_P Shell Programming and Scripting 5 07-18-2008 02:55 AM
Port incrementing using one file coolkid Shell Programming and Scripting 0 06-05-2008 01:42 PM
Port num incrementing coolkid Shell Programming and Scripting 5 05-28-2008 05:51 PM
shell script help: sorting, incrementing environment variables and stuff secoif Shell Programming and Scripting 1 05-08-2008 07:04 AM
New iteration of for-loop without incrementing? jeriryan87 Shell Programming and Scripting 0 07-02-2007 02:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-27-2005
run_time_error run_time_error is offline
Registered User
  
 

Join Date: Dec 2004
Posts: 40
incrementing a for loop

I have,

LIST="a b c d e"
for word in $LIST
do
echo $word
done

would give me
a
b
c
d
e

With the first iteration of the for loop, I get "a" as the result.
Is it possible that I get both "a" and "b" in only the first iteration.
In the next iteration I get "c" and "d" and so on.
I hope I am clear enough!!
Thanks
  #2 (permalink)  
Old 03-27-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Interesting that you have an odd number of elements. It's not clear how to handle that final e. But you could do something like this:
Code:
$ cat loop
#! /usr/bin/ksh

LIST="a b c d e"
switch=1
for word in $LIST  ; do
        if ((switch)) ; then
                word1=$word
        else
                word2=$word
                echo word1 = $word1 word2 = $word2
        fi
        ((switch=!switch))
done

exit 0
$ ./loop
word1 = a word2 = b
word1 = c word2 = d
$
  #3 (permalink)  
Old 03-28-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Quote:
Originally Posted by run_time_error
I have,

LIST="a b c d e"
for word in $LIST
do
echo $word
done

would give me
a
b
c
d
e

With the first iteration of the for loop, I get "a" as the result.
Is it possible that I get both "a" and "b" in only the first iteration.
In the next iteration I get "c" and "d" and so on.
I hope I am clear enough!!
Thanks
You can accomplish as,
# echo $LIST | awk '{ for (i=1;i<=NF;i++) { printf $i" "; if (i%2==0) { printf "\n"} } }END { printf "\n" }'
a b
c d
e
#

HTH.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0