LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-01-2013, 07:30 PM   #1
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Rep: Reputation: 30
Q: count inside a loop


hello

i am trying to write a for-loop that doesn't start from 1, but the loop count start from 1

Code:
for (( i=$a; i <= $b; i++ )); do echo "$x:$i"; done
where $a and $b always have different value
i want the result be like this:
Quote:
1: 10
2: 11
3: 12
4: 13
5: 14
6: 15
7: 16
8: 17
9: 18
10: 19
11: 20
any idea, thanks
 
Old 03-02-2013, 03:24 AM   #2
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,332
Blog Entries: 5

Rep: Reputation: 262Reputation: 262Reputation: 262
If I understand well..

Code:
#!/bin/bash

x=1
a=10
b=20

for i in `seq $a $b`
do
echo "$x: $i"
x=$((x+1))
done

Last edited by kooru; 03-02-2013 at 03:25 AM.
 
Old 03-02-2013, 05:04 AM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Or even simpler:

Code:
bash-4.2$ for i in $(seq 1 11); do printf '%d: %d\n' "$i" "$(($i + 9))"; done
1: 10
2: 11
3: 12
4: 13
5: 14
6: 15
7: 16
8: 17
9: 18
10: 19
11: 20
 
Old 03-02-2013, 07:04 AM   #4
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
thanks kooru,
Quote:
x=$((x+1))
did the trick
 
Old 03-04-2013, 12:15 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
This is a simple way
Code:
for (( x=1,i=10 ; x <= 10, i<=20; x++, i++))
do 
    echo "$x:$i"
done

# ./t.sh
1:10
2:11
3:12
4:13
5:14
6:15
7:16
8:17
9:18
10:19
11:20
 
Old 03-04-2013, 12:20 AM   #6
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
This is a simple way
Code:
for (( x=1,i=10 ; x <= 10, i<=20; x++, i++))
do 
    echo "$x:$i"
done

# ./t.sh
1:10
2:11
3:12
4:13
5:14
6:15
7:16
8:17
9:18
10:19
11:20
thanks chrism01, i never thought it's possible to do it in this way

thanks again
 
Old 03-04-2013, 12:24 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
No worries; glad to help
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
how to count specific entries inside a block Alkass Programming 8 07-16-2011 07:03 AM
[SOLVED] how to count the number of llines inside a file and put the output into a variable? auma78 Linux - Newbie 3 02-01-2011 06:21 AM
java problem within while loop and count variable.. xskycamefalling Programming 1 09-02-2010 06:41 PM


All times are GMT -5. The time now is 04:25 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration