Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-24-2012, 04:48 AM   #1
LQ Newbie
Registered: May 2011
Posts: 7

Rep: Reputation: 0
Using "i" and "i+1" at the same time

Hi to all,

I have a list of files : file1, file2, ... file48, and I want to use each time 2 files. First file1 and file2, then file2 and file3, then file3 and file4 etc. In a for loop, is it possible to echo both "i" and "i+1" from my file list?

Thanks in advance.
Old 01-24-2012, 05:18 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Which language are you using?
Old 01-24-2012, 05:58 AM   #3
Senior Member
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217

the logic you need is (this is in PHP):
PHP Code:
for ($i 1$i <= $numfiles$i++) {
// loop through i from 1 to $numfiles (total number of files)
echo $i;
//   do something with files
for ($n 0$n <= 1$n++) {
"Current file number is: ".$i+$n;  // first loop gets i=1, n=0, meaning i+n=1
        // second loop gets i=1, n=1, meaning i+n=2 etc...
}  // end loop for n
}  // end loop for i 
I hope this helps you.

Last edited by lithos; 01-24-2012 at 11:47 AM.
Old 01-24-2012, 09:00 AM   #4
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 133Reputation: 133
How about this?


for ((i = 1; i <= 48; i += 2)); do
    echo "Do something with file$i and file$((i+1))"
Old 01-24-2012, 09:55 AM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
i=i+1, i++, and other variations thereof, when done in an arithmetic context, set the variable to the new value. Once executed, the previous value is lost.

To add another variation to the above suggestions, how about simply using a second variable?

for (( i = 1; i <= nofiles; i++ )); do

	(( j = i + 1 ))
	echo "file$i file$j"

If the filenames are stored in an array first, it's also possible do something like this (assuming bash):

files=( * )
for i in "${!files[@]}"; do

	echo "${files[i]} ${files[i+1]}"

The [..] field in array expansion carries an arithmetic context, allowing such operations to be done inside them.
Old 01-25-2012, 04:34 AM   #6
LQ Newbie
Registered: May 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you all for your answers.
Currently I solved this in a more "newbie" way. Maybe I should have added that I read my list of files from a text file.

many thanks!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 07:41 PM
[SOLVED] "C preprocessor "/lib/cpp" fails sanity check" every single time. masmddr Linux - Software 3 08-03-2010 11:37 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
Lost DHCP Lease when putting computer in "Standby" mode for a "long" time pnellesen Linux - Networking 1 01-07-2005 12:44 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration