Linux Almalinux - While or Array

52 views Asked by At

I would like to realise the following in Linux-Almalinux: So i would like to add 5 records with a new random word in it.

Problem is that following code will add N records with the same word. How can i some how add or something or maybe some temp variabele where i can delete this after every while loop...

So when INPUT_NUMBER is 5 then it adds 5 records with 5 random different names

local word=$(sort -R names.list | head -1)
Local INPUT_NUMBER=${2}

while [[ 1 -le $INPUT_NUMBER ]]
do
    create_record ${word} ${Domain}
    ((i = i + 1))
done

Thanks in advance!

I tried to think about a solution but since im more experienced with JAVA, these WHILE/IF is completely different in Linux and more complex.

2

There are 2 answers

1
Barmar On

Put N words in an array and loop over the array.

local n=$2
local words=($(sort -R names.list | head -n "$n"))

for word in "${words[@]}"
do
    create_record "${word}" "${Domain}"
done
0
Ed Morton On

Untested since you didn't provide any sample input/output and create_record isn't a common command that most people have (I don't) but this might be what you want:

shuf -n "$n" names.list | xargs -I {} -n 1 create_record {} "$Domain"