I am trying to make a script that randomly generates 4 sets of numbers with decimal points. Then it outputs that to a variable and a file. This is what I have:
For some reason when I do these commands separate in terminal it works fine but together in a script it wont work right. Anyone know how to make this work?
You're using printf like echo, which it's not. 'printf "%s" a b c d' will only print a because you're only telling it to print one string with '%s'. You might as well just use echo and print all of them.
You don't even need a variable here. Just echo it right into the file.
This may not be the best way to generate random numbers. If RANDOM is between 0 and 65536, you'll never have a decimal point higher than .65. Let's try printf again, properly, with:
#!/bin/bash
( for ((N=0; N<4; N++))
do
printf " %d.%03d" "$((RANDOM%1000))" "$((RANDOM%1000))"
done ) > ./bp1/bin/random
This should generate random numbers between 0 and 1000, with three decimal places.
I know this isn't the proper way to do it but I am currently in a Linux/Unix programming class and they want this to be done using printf and $RANDOM (which I've never done it this way before). I need a new variable and a file both with the same outputted data.