Linux Almalinux - while 或 array

Linux Almalinux - While or Array

提问人:JJJ 提问时间:11/7/2023 最后编辑:BarmarJJJ 更新时间:11/8/2023 访问量:33

问:

我想在 Linux-Almalinux 中实现以下几点: 所以我想添加 5 条记录,其中包含一个新的随机词。

问题是以下代码将添加具有相同单词的 N 条记录。 我怎么能添加一些东西,或者也许是一些临时变量,我可以在每隔一段时间循环后删除它......

因此,当INPUT_NUMBER为 5 时,它会添加 5 条具有 5 个随机不同名称的记录

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

提前致谢!

我试图考虑一个解决方案,但由于我对 JAVA 更有经验, 这些 WHILE/IF 在 Linux 中完全不同,而且更复杂。

数组 Linux if-statement while-loop almalinux

评论

1赞 knittl 11/7/2023
almalinux 在这里有什么关系?你用的是什么外壳,还是另一个外壳?至于你的解决方案:只需在循环中获取随机单词,而不是在循环之外shbash

答:

0赞 Barmar 11/7/2023 #1

将 N 个单词放入一个数组中,并在数组上循环。

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

for word in "${words[@]}"
do
    create_record "${word}" "${Domain}"
done

评论

0赞 JJJ 11/7/2023
非常感谢您的解决方案!
0赞 Ed Morton 11/8/2023 #2

未经测试,因为您没有提供任何示例输入/输出,并且不是大多数人拥有的常见命令(我没有),但这可能是您想要的:create_record

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