Need to get a loop variable from output of a command


 
Thread Tools Search this Thread
# 1  
Need to get a loop variable from output of a command

Hi,

I am trying to get a loop counter i and set its value as the ouput of a command:

Code:
i=`printmo TEST1 | grep -i TEST2 | wc -l`

Then I want to use i as counter to run a loop i number of times.
Like if i gets a value of 5 I'll have to run loop 5 times.
But will i here be a numeric variable? If it won't be numeric then how can I make it a numeric variable and use it as couter to run the loop.

Please help. I tried to search but didn't get what I am looking for.

Thanks in advance,
Pramod

Last edited by Scott; 10-25-2012 at 02:59 PM.. Reason: Code tags
# 2  
Try: in the script before the loop add a test for i like:
Code:
echo "$i" | grep -q '^[0-9][0-9]*$' || { echo "$i" not a number ; exit ; }

# 3  
Thanks a lot for quick reply, I got this.
Does it mean its not a nomber or I shoul correct the command by changing q with r or something.

Code:
echo "$i" | grep -q '^[0-9][0-9]*$' || { echo $i not a number ; exit ; }
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
6 not a number


Last edited by Scott; 10-25-2012 at 03:32 PM.. Reason: Code tags
# 4  
Try:
Code:
echo "$i" | grep '^[0-9][0-9]*$' >/dev/null || { echo "$i" not a number ; exit ; }

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Hard
If you could fold a piece of paper in half 50 times, its' thickness will be 3/4th the distance from the Earth to the Sun.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. Shell Programming and Scripting

Use while loop - output variable

I'm very much a newbie and hence why this is going to be a stupid question. I'm attempting to create a korn shell script that pulls zone file locations and includes the copy command in the output. What? getzonedir.ksh #!/bin/ksh while read -r domain do ls */*"$domain" > $dir1 echo "cp... (5 Replies)
Discussion started by: djzah
5 Replies

3. Shell Programming and Scripting

Print for loop variable in output too

Hi, This is my input file cat input chr1:100-200 chr1:220-300 chr1:300-400 Now, I would like to run a program that will take each of the input record for i in `cat input`; do program $i | wc -l;done the output will be something like 10 20 30 But, I would like to print the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Redirect output of command line to for loop

I would like to redirect output of command line in for loop as $line. Output should be processed as line but instead it throw whole output. Could somebody help me on how to redirect output of command line and process it line by line without sending output to any file. below is my code ... (1 Reply)
Discussion started by: tapia
1 Replies

5. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

6. Programming

Command output into a variable

Hi, with this command: cu -l /dev/ttyACM0 -s 9600 > name.txt I put the output of the port in a txt Is posible to do the same (or similar) in a var directly, inside a C program? cu -l /dev/ttyACM0 -s 9600 > variable ? I have trying this withs pipes, but i dont know how to... (6 Replies)
Discussion started by: daaran
6 Replies

7. Shell Programming and Scripting

While loop using command output...

If I run this command networksetup -listallnetworkservices I get the following output. Ethernet AirPort *Parallels Host-Guest *Parallels NAT MY VPN Ethernet 2 I want to make changes to only anything that contains the word "Ethernet" which I can do with grep. But What I really need a... (6 Replies)
Discussion started by: elbombillo
6 Replies

8. Shell Programming and Scripting

get characters from output of a command in a variable

Hi, i have two questions, I am new to programming 1. I have an output of a command and i want to get some specific part of it in a variable. i am trying sr=`some comand xyz| grep 'Last Changed Rev:' | cut -c19-` now variable sr gets a end of line character at end. output of the command... (3 Replies)
Discussion started by: muaz
3 Replies

9. Shell Programming and Scripting

Command output to a variable.

With cut -c 8-13 myfile, I am getting some numeric value. In my shell script I am trying to assign something like this, var=cut -c 8-13 myfile But at the time of execution I am getting -c is not found. If I dont assign, then script executes well. Can we not simply use the value from one... (8 Replies)
Discussion started by: videsh77
8 Replies

Featured Tech Videos