请帮忙!
我这里有一个时间敏感的问题,所以我转向 Stackoverflow,希望能得到相当快的答复。我不是 bash 专家,尤其是在必须快速思考的时候。我正在尝试将用户数据库中的两列 awk 转换为 OpenStack API。数据库(我创建的平面文件)非常简单。它看起来像这样:
| ID | username | True |
Run Code Online (Sandbox Code Playgroud)
数据库非常大,所以我试图循环遍历它。这是我尝试运行的命令:
for user in $(cat users.txt); do keystone user-role-add --user $user --role blah --tenant $tenant; done
Run Code Online (Sandbox Code Playgroud)
假设 users.txt 列表的每一行都有一个用户名,这非常有效。
我需要修改此命令以从 users.txt 中提取 $user 的用户名,并从 users.txt 中提取 $tenant 的相应 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
Run Code Online (Sandbox Code Playgroud)
再次感谢!
小智 7
这种方式避免使用 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
Run Code Online (Sandbox Code Playgroud)
你可能想要类似的东西...
while read line;do
id=$(echo $line|awk '{print $2}')
username=$(echo $line|awk '{print $4}')
bool=$(echo $line|awk '{print $6}')
# do other
# action here
done <users.txt
Run Code Online (Sandbox Code Playgroud)