请帮忙!
我这里有一点时间敏感的问题,所以我求助于 Stackoverflow,希望能得到相当快的答复。我不是 bash 专家,尤其是在必须快速思考的时候。我正在尝试将用户数据库中的两列 awk 转换为 OpenStack API。数据库(我创建的平面文件)非常简单。它看起来像这样:
| ID | username | True |
数据库非常大,所以我试图遍历它。这是我要运行的命令:
for user in $(cat users.txt); do keystone user-role-add --user $user --role blah --tenant $tenant; done
假设 users.txt 列表的每一行都有一个用户名,这会很好用。
我需要修改此命令以从 $user 的 users.txt 中提取用户名,并从 $tenant 的 users.txt 中提取相应的 ID
* 解决方案 *
while read line;do
tenant=$(echo $line|awk '{print $2}')
user=$(echo $line|awk '{print $4}')
keystone user-role-add --user $user --role blah --tenant $tenant
done <users.txt
再次感谢!
最佳答案
这种方式避免了使用awk,应该更优化:
while read id username tenant; do
echo "---DEBUG --- id=${id} username=${username} tenant=${tenant}"
# do other action here
# calling variables as in DEBUG line above
done <users.txt
关于bash - awk 两列进入 bash for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658777/