sql - 如何将 .txt 文件中的行读入此 bash 脚本?
问题描述
我有这个连接到 postgre sql db 并执行查询的 bash 脚本。我希望能够将 .txt 文件中的行作为参数读入查询中。最好的方法是什么?非常感谢您的帮助!我在下面有我的示例代码,但是它不起作用。
#!/bin/sh
query="SELECT ci.NAME_VALUE NAME_VALUE FROM certificate_identity ci WHERE ci.NAME_TYPE = 'dNSName' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower('%.$1'));"
(echo $1; echo $query | \
psql -t -h crt.sh -p 5432 -U guest certwatch | \
sed -e 's:^ *::g' -e 's:^*\.::g' -e '/^$/d' | \
sed -e 's:*.::g';) | sort -u
解决方案
考虑到该文件每行只有一个 sql 查询:
while read -r line; do echo "${line}" | "your code to run psql here"; done < file_with_query.sql
这意味着:在逐行读取 file_with_query.sql 的内容时,对每一行执行一些操作。
推荐阅读
- reactjs - AsyncStorage getItem not working within drawer with react native router flux
- javascript - How to add box-shadow to bootstrap carousel image?
- javascript - 基于 javascript 的自定义代码编辑器的缩进
- python - 取消 dict 的子类时出现 AttributeError
- python - python:从工作目录中稳健地导入模块
- php - 我需要替换数字 (555) 564-8583 中的多余字符 我只想允许加号和整数
- sql-server - 从不同数据库上的表更新时 - 无法解决排序规则冲突
- spring-boot - 如何从 ServerHttpRequest 读取 Spring Boot 响应式请求正文
- kubernetes - Kubernetes 入口 nginx grafana 仪表板 prometheus 数据为空
- django - 在 django 表单中隐藏字段表单