0

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
4
  • Please add your expected output. Commented Nov 18, 2022 at 5:04
  • Say bash script csv.sh, It should generate a file named inputFile whose content looks like. 0, 334 1, 933 2, 34123 3, 123 4, 12345 5, 6783 Running the script without any arguments, should generate the file inputFile with 10 such entries in current directory also we should be able to extend this script to generate any number of entries, for example 100000 entries. This is requirement ! Commented Nov 18, 2022 at 5:15
  • Please update/edit your question to reflect such requirement, not here in the comment section, so others can read everything also. Commented Nov 18, 2022 at 5:29
  • I have updated the clear requirement in the question Commented Nov 18, 2022 at 6:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.