Issue
I try to create randomly between 30 and 130 files in a specific folder with a shell script. I might have made some mistakes because I am not working with shell for long time. Here is my code:
ran=$RANDOM
while [$ran > 100]; do
$ran = $RANDOM
done
$ran=$ran+30
i=0
while [$i < $ran]; do
cat > 'Lobby/data/map_'$i'.dat'
$i=$i+1
done
Hope you can help me. I'm working with debian.
Solution
Try this (use bash):
#!/bin/bash
ran=$(( RANDOM % 100 ))
ran=$(($ran+30))
for (( start = 1; start <= $ran; start++ ))
do
touch './Lobby/data/map_'$start'.dat'
done
or sh:
ran=$(( $$ % 100 ))
ran=$(($ran+30))
i=0
while [ "$i" -le "$ran" ]; do
touch './Lobby/data/map_'$i'.dat'
i=$(( i + 1 ))
done
Make sure you have permissions to write in this folder. Random % 100 will generate number up to 100 - adding 30 will make sure you are not below 30 or above 130 so no need for additional check like you did.
Regards, Vedran
Answered By - Vedran Mile Tomljanovic