linux - 在循环中使用带有 sql 输出的 for
问题描述
我有一个 roles.txt 文件,其中包含
admin
user
SELECT ROLE_NAME FROM SENTRY_ROLE WHERE ROLE_NAME 产生:
admin
我试图回显roles.txt中不在sql_output中的所有角色(本例中的用户)。这是我到目前为止所拥有的:
for filename in roles.txt
do
sql_output=$(mysql -N -e "SELECT ROLE_NAME FROM SENTRY_ROLE WHERE ROLE_NAME = '$filename';")
if [ -z "$sql_output" ]
then
echo "sql_output is empty" #this is where i want to echo all the roles that are not in the output of sql_output AKA "user"
else
echo "\$sql_output is in sql_output"
fi
done
解决方案
这不是您遍历文件行的方式。见http://mywiki.wooledge.org/BashFAQ/001
我会这样做,只需一个 mysql 调用:
# Iassume this does not produce any extraneous output,
# just a list of role names, one per line
sql_output=$( mysql -N -e "SELECT distinct ROLE_NAME FROM SENTRY_ROLE" )
missing_roles=$( comm -23 roles.txt <(echo "$sql_output") )
请参阅http://manpages.ubuntu.com/manpages/bionic/en/man1/comm.1.html
推荐阅读
- amazon-s3 - 使用 kafka 连接器将 kafka 主题数据作为 csv 写入 s3 存储桶
- visual-studio-code - 有没有办法在 VS Code 中指示特定分支之间的文件和行差异
- python - 占位符和仅查看 Django 模型表单中的值
- symfony - API 平台 - 如何创建自定义收集操作 (GET)?
- uwp - 在 Windows 应用程序中拖放项目并获取项目的 StandardDataFormats
- python - Django 3图像和文件上传不通过表单保存
- jmeter - 有没有办法在 Jmeter 变量中使用 Loopnum 而不是 threadNum
- spring-boot - Jedis java.net.socketException: Broken pipe (Write failed)
- sql - 窗函数滚动和
- android - 使用 kotlin 通过 webview 启用下载