Home > Software engineering >  How to write a bash script to generate a file named inputFile with comma separated values with index
How to write a bash script to generate a file named inputFile with comma separated values with index

Time:11-21

Can someone help on how to write an bash script to generate a file named inputFile whose content looks like:

0, 334

1, 933

2, 34123

3, 123

These are comma separated values with index and a random number.

Running the script without any arguments, should generate the file inputFile with 10 such entries in current directory.

You should be able to extend this script to generate any number of entries, for example 100000 entries.

I tried below script, But its not as expectation could someone help to fix this as I am new to scripting?

RANDOM=$$
num=0

while [[ ${num} -le $1 ]]
do
    echo $num $RANDOM
    (( num = num  1 ))
done

CodePudding user response:

Use a C-style for loop in bash:

#!/bin/bash

n=10
for ((i=0; i<n;   i)); do
    echo "$i,$RANDOM"
done > inputFile

Modify the n=10 as needed.
Alternatively, using a while loop:

#!/bin/bash

n=10
i=0
while ((i<n)); do
    echo "$((i  )),$RANDOM"
done > inputFile
  • Related