Reputation: 167
I want to create a Linux command which creates a file containing 10 random numbers.
This is a solution to generate 10 random numbers;
RANDOM=$$
for i in `seq 10`
do
echo $RANDOM
done
It is working to generate random numbers but how can I combine this with 'touch' command? Should I create a loop?
Upvotes: 0
Views: 1699
Reputation: 1
You could use head(1) and od(1) or GNU gawk with random(4).
For example, perhaps
head -20c /dev/random | od -s > /tmp/tenrandomnumbers.txt
Upvotes: 0
Reputation: 11219
Use >> to write to file, $1 is first argument of your .sh file
FILE=$1
RANDOM=$$
for i in `seq 10`
do
echo $RANDOM >> $FILE
echo "\n" >> $FILE
done
Upvotes: 1
Reputation: 26484
Using touch? Like this?
touch file.txt && RANDOM=$$
for i in `seq 10`
do
echo $RANDOM
done >> file.txt
Not sure why you need touch though, this will also work:
for i in `seq 10`; do echo $RANDOM; done > file.txt
Upvotes: 3