LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-05-2008, 06:42 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
Bash scripting help needed


Hi all,

I need a help is scripting. Can someone give me the equivalent for loop for the following "C" format in BASH:

Code:
i=10

for(i=0;i<10;i++)
{
executable cmd
}
Thanks
 
Old 08-05-2008, 07:08 AM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
I don't understand what the i=10 line is supposed to do in your example.
If you want to run the command whatever 10 times then try
Code:
for i in `seq 1 10`;do 
whatever
done
 
Old 08-05-2008, 07:13 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can explicitly loop over a sequence of numbers or strings, as in
Code:
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo $i
done
or you can generate the sequence using command substitution:
Code:
for i in $(seq 1 10)
do
  echo $i
done
Finally recent versions of bash permit the usage of a C-like loop, using the arithmetic operator, as in
Code:
for (( i = 1; i <= 10; i++ ))
do
  echo $i
done
The Advanced Bash Scripting Guide explains it all.
 
Old 08-05-2008, 07:17 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
If you have bash version 3 or above, you can do:
Code:
for i in {0..10}; do 
   echo $i
done
Otherwise, if you have an array of values:
Code:
some_array=(1 2 3 4 5 6 7 8 9 10)
for ((i=0; i<${#some_array[@]}; i++)); do
   echo $i
done
That array can contain words or numbers. The ${#some_array[@]} counts the number of elements in the array.

edit: beaten to it by colucix!

Last edited by pwc101; 08-05-2008 at 07:18 AM.
 
Old 08-05-2008, 07:23 AM   #5
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you for all your Replies.

Sorry I made a mistake in my post.
Code:
i=10
But "i" is a variable.
am got the value of "i" from another output
Code:
i=$(cat file1 |wc -l)
The value of i can vary. Can someone help me to do the for loop for the variable "i" for the same logic
 
Old 08-05-2008, 07:36 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
i=(wc -l < file1)
for ((j=0; j<$i; j++)); do
   echo $j
done
 
Old 08-05-2008, 07:48 AM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you PWC101

Its working .
 
Old 08-05-2008, 07:53 AM   #8
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Got struck at the next step
Am trying to compare the two files within the loop and got some error.

Code:
i=(wc -l < file1)
for ((j=0; j<$i; j++)); do
 diff `sed -n '($j)p' file1` file2 >file3  
done
How can i declare the "$j" inside sed -n 'p' file1.

Last edited by ZAMO; 08-05-2008 at 07:54 AM. Reason: using GO advanced
 
Old 08-05-2008, 08:01 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you want to compare 2 files, use diff. Otherwise, if you have special requirements, pls describe.
 
Old 08-05-2008, 08:07 AM   #10
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
I want to surely compare 2 files..
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help needed ZAMO Linux - General 3 05-08-2008 09:52 AM
Bash scripting help needed ZAMO Linux - General 11 05-02-2008 06:22 AM
bash scripting help needed tine Programming 3 09-03-2007 09:13 AM
Bash scripting help needed Zylar Linux - Newbie 8 05-03-2007 08:32 AM
bash scripting --- some help needed rajsharma Linux - Software 1 09-09-2005 02:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:31 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