I am attempting to write a 1 line while loop that creates a string of file names like list="temp0.txt temp1.txt temp2.txt ... " etc.. I want it to be a random number of files between 0 and 50, and this is what I have thus far.
argumentCount=$RANDOM
let "argumentCount %= 50"
i=0
list=""
while [ $i -lt $argumentCount ]; do; touch temp[$i].txt; $list="$list temp[$i].txt; ((i=i+1)); done
countLines $list > output.txt
I am trying to declare a random number between 0 and 50, and then using a while loop I want to create the same number of files as this random number. I want to then create a string that has all of the file names listed one after the other. This string will then be passed to countLines, which is a program that accepts any number of filenames as parameters.
I have this set of commands stored in a different file, and I am using eval on each line in this file, but it is giving me a syntax error. Is there something I can do to make it work? Thanks
printf -v varname "temp%03d.txt" $(($RANDOM % 50))Then$varnamewill containtemp000.txt-temp049.txtrandomly. (you can control the number of leading'0's with theprintfformat specifier. (add$(($RANDOM % 50 + 1))for001 - 050numbering).declare -i n=0) and in your file-create loop to((n++))andtest "$n" -gt '40' && breakif you like. (or whatever number you like). You also need to check for an existing file of the same name/number (e.g.test -f "$varname" && continueto generate a non-conflicting filename)