LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2006, 04:16 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
bash script: how do I refer to a counter i in a "for i in" loop


Hi,

Is it possible to refer to counter i in a
for i in *
loop? How do I do that?

I just wanted to make my file names shorter after processing. Like I have files
anneal_i.radial_Si_Si
where i goes from 1 to 20, and I want to generate some other files with a script, but also in the process reduce names of the ones I have. Thanks
 
Old 10-01-2006, 04:32 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It's $i. Example:
Code:
for i in 1 2 3 ;do echo file_$i; done
The result:
Code:
file_1
file_2
file_3
 
Old 10-01-2006, 04:51 PM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
I know that. I want to refer to i itself. like for i in file[1-6]
I want to refer to 3, not file3.

$3 refers to file3 not 3. what refers to 3?

Last edited by frankie_DJ; 10-01-2006 at 04:53 PM.
 
Old 10-01-2006, 05:10 PM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
the for loop iterates over the elements you provide them. As far as bash is concerned all the elements are strings. They dont have the format you want and are magical parse so that something automatically point at the part you want.
$i will always point at the current element. If you want part of that element, then you have to parse it yourself.

Hope this is useful.
Cheers!
 
Old 10-02-2006, 03:22 AM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Quote:
I know that. I want to refer to i itself. like for i in file[1-6]
I want to refer to 3, not file3.

$3 refers to file3 not 3. what refers to 3?
$for i in `seq 1 20`;do echo "This is loop $i";done
This is loop 1
This is loop 2
This is loop 3
...
This is loop 20

Then to rename a file do this:
$for i in `seq 1 20`;do mv anneal_$i.radial_Si_Si anneal_$i.radial;done
 
Old 07-24-2007, 08:45 AM   #6
gloriant
Member
 
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42

Rep: Reputation: 16
using
Code:
$( seq <start> <stop> )
is one way to do it. By the way, i prefer the $( ... ) notation over the back quote, because sometimes back quotes are incorrectly interpreted/read/remembered/copied by readers.

Sometimes you'll want to allow the loop-code to adjust the counter, which then asks for something like:

Code:
for (( counter=0; ${counter} < 20; counter=$(( ${counter}+1 )) )); do
  echo "this is loop ${counter}.";
done
in this code, mind the separating spaces around (( and )).

Or you do it completely by hand:

Code:
counter=0;
while [ ${counter} -lt 20 ]; do
  echo "this is loop ${counter}.";
  counter=$(( ${counter}+1 ));
done
OR, if you have some files, but want to get the number that's in the file, I'd do it like this:

Code:
for file in file[0-9]*; do
  i=$(echo "${file}"|sed 's/^file//;');
  echo "file index ${i}";
done
so decrease all files by one for instance:
Code:
for file in file[0-9]*; do
  i=$(echo "${file}"|sed 's/^file//;');
  i=$(( ${i} - 1 ));
  if [ ! -f file${i} ]; then
    mv ${file} file${i};
  else
    echo "file${i} exists already.";
  fi
done
or then, when increasing files:
Code:
function increase_file {
  local newfile;
  fn=increase_file;
  if [ 1 != ${#} ]; then echo "${fn} invalid nr of arguments ${#}.";
  elif [ ! -f ${1} ]; then echo "${fn} called with non-file argument.";
  else
    i=$(echo "${1}"|sed 's/^file//;');
    i=$(( ${i}+1 ));
    newfile=file${i};
    if [ -f ${newfile} ]; then
      echo "first increase existing file ${newfile}.";
      increase_file ${newfile};
    fi
    mv ${1} ${newfile};
  fi
}

# calling with:
increase_file file1;
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
"Refer To" option to point questioners to existing discussion/material sboddy LQ Suggestions & Feedback 6 12-01-2005 02:14 AM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 02:20 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

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