Range specification in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Range specification in for loop
# 1  
Old 09-20-2007
Range specification in for loop

Hello Friends,

I want to use range in for loop. For that i used (..) operator but it is not working.

Ex:
for i in 1..24
do
echo $i
done

Instead of printing 1 to 24 nos, it gives o/p as: 1..24

Please help me

Thanks in advance.
# 2  
Old 09-20-2007
Your shell does not have the .. range operator. Try this instead:
Code:
#!/bin/bash
LIMIT=24
for ((a=1; a <= LIMIT ; a++))  # Double parentheses, and "LIMIT" with no "$".
do
  echo -n "$a "
done                           # A construct borrowed from 'ksh93'.
echo; echo

or this:
Code:
#!/bin/bash
NUMBERS="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24"
for number in `echo $NUMBERS`  # would be similar to: for number in 1 .. 24
do
  echo -n "$number "
done
echo

# 3  
Old 09-20-2007
using Perl:
Code:
#!/usr/bin/perl
for $i (1 .. 24) {
    print $i, "\n";
}

Perl one liner:
Code:
perl -e 'for $i (1 .. 24) { print $i, "\n"; }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

2. Shell Programming and Scripting

Help with column specification

Hi I am working on a program that reads a file with multiple columns and was curious how to specify the columns to be manipulated in the command line. For example the file may look something like: Column1 Column2 Column3 Column4 Column5 Column6 AC 82542 3525 ... (1 Reply)
Discussion started by: drossy
1 Replies

3. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

4. Shell Programming and Scripting

loop directories mv files to target in range

Hello, Currently I have a painstaking process that I use to move file for a monthly archive. I have to run the same two commands for 24 different directories. I wish to have a script with a for loop automate this and I have not been able to succeed. Here is what I do 24 times. I know this is... (5 Replies)
Discussion started by: jaysunn
5 Replies

5. Shell Programming and Scripting

For Loop Range Create Directories

Hello, I am a bit stumped on this. I am attempting to create 24 empty directories with a loop. Seems like I have incorrect syntax. When I run the following command I get the error below. Command $ for i in {2..24}; do mkdir $i_MAY_2011 ; doneError x 24 mkdir: missing operand Try `mkdir... (2 Replies)
Discussion started by: jaysunn
2 Replies

6. Solaris

Getting server specification

Hi all, Can anyone help me to get the server specifications like the following? eg. SUN Fire XXX X UltraSPARC X MHZ X MB Memory X GB od hard disk X On board PCI IO card X PCI HNI card Thanks. (4 Replies)
Discussion started by: beginningDBA
4 Replies

7. UNIX for Dummies Questions & Answers

RSC card specification

Hi I have a SUN box that i am supporting Model 480r . The RS card that is attached to it is giving some problems . It is pinging but i am unable to telnet into it therefore having no remote TC console access ( the system is currently up ) Is there a way by which i can remotely check the... (1 Reply)
Discussion started by: Sam4u
1 Replies

8. Solaris

System specification checking

Hi, anybody know how to check the system specification for the unix by using the command like the memory size, cpu's speed and etc. Thanks. (3 Replies)
Discussion started by: efang
3 Replies

9. Solaris

hardware specification ( very Urgent please )

I wana know my box hardware specification ( Like RAM , processor speed .... Etc ) and used version of Solaris i mean (SPARC, 32-bit) or (x86, 32-bit) or(SPARC, 64-bit) or what ?? very Urgent please (5 Replies)
Discussion started by: KSA
5 Replies

10. UNIX for Dummies Questions & Answers

HD specification

What command do I use to determine how many/what kind/how big the hard drives on a box are? (2 Replies)
Discussion started by: abolshoun
2 Replies
Login or Register to Ask a Question