arrays - bash 脚本 shuf 并保存在列表中
问题描述
我有 A 数字,我想选择其中的 B 而不重复并保存到列表中。
像这样:
A="100"
B=5
a=$(gshuf -i 1-$B -n $A)
for i in ${a}
do
echo $i
done
我该怎么做?
A 是我的代码中的一个字符串,我使用 gshuf 而不是 shuf 因为我在 Mac 中
解决方案
您可以使用process substitution
:
A="100"
B=5
while read -r i; do
echo "$i"
done < <(gshuf -i 1-$B -n $A)
如果要将生成的数字保存在数组中,请使用:
arr=()
while read -r i; do
arr+=("$i")
done < <(gshuf -i 1-$B -n $A)
检查内容:
declare -p arr
推荐阅读
- security - 尝试在安全的 spark-java 应用程序中连接到 localhost:4567 时出现奇怪的响应
- svg - svg 转换元素在 Edge 中不起作用
- javascript - 在 render() 中没有回调 React 函数
- firebase - 如何在 Cloud Firestore 上编写查询以获取文档?
- azure-devops - 是否可以让 Azure Pipeline 在提交时修改 README.md?
- python - 循环中的 jupyter print 正在打印垃圾
- c# - 从 .Net HttpClient 将大型 JSON(~40-80mb) 发布到无服务器 API
- mysql - 为什么当开始一个事务导致任何未决事务被提交时,我们需要在 SELECT 期间锁定一个 mysql 表?
- java - 我的主循环中的步骤有问题
- sql - 如果它是多对多关系并且只有一列,是否需要单独的数据库表?