LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-24-2012, 03:48 AM   #1
kaveiros
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, 04:18 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which language are you using?
 
Old 01-24-2012, 04:58 AM   #3
lithos
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
Hi,

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++) {
        echo 
"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 10:47 AM.
 
Old 01-24-2012, 08:00 AM   #4
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 134Reputation: 134
How about this?

Code:
#!/bin/bash

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

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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?

Code:
nofiles=20
for (( i = 1; i <= nofiles; i++ )); do

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

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

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

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

done
The [..] field in array expansion carries an arithmetic context, allowing such operations to be done inside them.
 
Old 01-25-2012, 03:34 AM   #6
kaveiros
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.

Again,
many thanks!
 
  


Reply



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 06:41 PM
[SOLVED] "C preprocessor "/lib/cpp" fails sanity check" every single time. masmddr Linux - Software 3 08-03-2010 10:37 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Lost DHCP Lease when putting computer in "Standby" mode for a "long" time pnellesen Linux - Networking 1 01-06-2005 11:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:38 PM.

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