Loop assistance, getting array of random numbers and feeding to a command, how-to?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop assistance, getting array of random numbers and feeding to a command, how-to?
# 1  
Old 08-06-2009
Computer Loop assistance, getting array of random numbers and feeding to a command, how-to?

Hi all,
I need a little assistance to complete the following script. I would like to take a file with a single number on each line and for each number, run it through a command. The loop will terminate once all numbers have been checked. Here is what I have thus far...

Code:
COUNTER=`wc -l ~/number_list | awk '{print $1}'`
while [ $COUNTER -ne 0 ]; do
$COMMAND $NUMBER
let COUNTER=COUNTER-1
done

How do I store each number, maybe in an array? And then pass each number to the command as $NUMBER?

Thanks!
-b2
# 2  
Old 08-06-2009
Quote:
Originally Posted by boolean2222
Hi all,
I need a little assistance to complete the following script. I would like to take a file with a single number on each line and for each number, run it through a command. The loop will terminate once all numbers have been checked. Here is what I have thus far...

Code:
COUNTER=`wc -l ~/number_list | awk '{print $1}'`


You don't need awk:

Code:
COUNTER=`wc -l < ~/number_list

Quote:
Code:
while [ $COUNTER -ne 0 ]; do
$COMMAND $NUMBER
let COUNTER=COUNTER-1


That is non-standard. The portable syntax for arithmetic is:

Code:
COUNTER=$(( $COUNTER - 1 ))

Quote:
Code:
done

How do I store each number, maybe in an array? And then pass each number to the command as $NUMBER?

You don't need a COUNTER.

Code:
file=$HOME/number_list
while read num
do
 : do whatever
done < "$file"

But you don't even need a loop to store it in an array:

Code:
array=( $( cat "$file" ) )

In bash or ksh93, cat isn't needed:

Code:
array=( $( < "$file" ) )

In bash 4.0, you can use the new builtin command, mapfile:

Code:
mapfile -t array < "$file"

# 3  
Old 08-06-2009
Thank you thank you thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

How to loop through array who's name is entered in command line?

Say I have a ksh program called test.ksh which has several defined arrays inside it such as array1,array2,array3...These arrays contain strings. I also have a method in the program: for x in $1 do ....(#do something) done So, when the user enteres: ./test.ksh array1, I want to... (3 Replies)
Discussion started by: mrskittles99
3 Replies

4. Shell Programming and Scripting

A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks. (4 Replies)
Discussion started by: AxlVanDamme
4 Replies

5. Programming

generate array of random numbers

hi guys, I am writing a c program that generates a two dimensional array to make matrix and a vector of random numbers and perform multiplication. I can't figure out whats wrong with my code. It generates a matrix of random numbers but all the numbers in the vector array is same and so is the... (2 Replies)
Discussion started by: saboture88
2 Replies

6. Solaris

feeding filenames to find command

Hi, I am having set of files whose names are stored in a file say "filelist.txt" Now, I want to find all files contained in "filelist.txt" from my parent directory. Is there any way to let find command understand "filelist.txt" just like we have option -f in awk. I donot want to run a... (4 Replies)
Discussion started by: sanjay1979
4 Replies

7. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

8. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

9. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question