bash — передача запроса sql в качестве аргумента psql
У меня есть следующий SQL-запрос, который правильно выполняется в оболочке psql:
SELECT c.component_name AS "Component",
c.component_version_name AS "Component Version",
c.version_origin_id AS "Version Origin ID"
FROM reporting.component c
JOIN reporting.component_license cl
ON cl.component_table_id = c.id
WHERE cl.license_display = 'Unknown License'
GROUP BY c.component_name, c.component_version_name, c.version_origin_id;
Я пытаюсь понять, как передать его в качестве аргумента в оболочке Bash и в сценарии Bash.
Я пытался использовать HEREDOC, но похоже, что он не передается в качестве аргумента psql:
psql -d bds_hub -c <<SQL_QUERY
SELECT c.component_name AS "Component",
c.component_version_name AS "Component Version",
c.version_origin_id AS "Version Origin ID"
FROM reporting.component c
JOIN reporting.component_license cl
ON cl.component_table_id = c.id
WHERE cl.license_display = 'Unknown License'
GROUP BY c.component_name, c.component_version_name, c.version_origin_id;
SQL_QUERY
psql: option requires an argument: c
Try "psql --help" for more information.
Я также пытался передать запрос как переменную, но, похоже, это тоже не работает. Переменная:
echo $SQL_QUERY
SELECT c.component_name AS "Component",
c.component_version_name AS "Component Version",
c.version_origin_id AS "Version Origin ID"
FROM reporting.component c
JOIN reporting.component_license cl
ON cl.component_table_id = c.id
WHERE cl.license_display = 'Unknown License'
GROUP BY c.component_name, c.component_version_name, c.version_origin_id;
Выход:
psql -d bds_hub -c $SQL_QUERY
psql: warning: extra command-line argument "AS" ignored
psql: warning: extra command-line argument ""Component",c.component_version_name" ignored
psql: warning: extra command-line argument "AS" ignored
psql: warning: extra command-line argument ""Component" ignored
psql: warning: extra command-line argument "Version",c.version_origin_id" ignored
psql: warning: extra command-line argument "AS" ignored
psql: warning: extra command-line argument ""Version" ignored
psql: warning: extra command-line argument "Origin" ignored
psql: warning: extra command-line argument "ID"" ignored
psql: warning: extra command-line argument "FROM" ignored
psql: warning: extra command-line argument "reporting.component" ignored
psql: warning: extra command-line argument "c" ignored
psql: warning: extra command-line argument "JOIN" ignored
psql: warning: extra command-line argument "reporting.component_license" ignored
psql: warning: extra command-line argument "cl" ignored
psql: warning: extra command-line argument "ON" ignored
psql: warning: extra command-line argument "cl.component_table_id" ignored
psql: warning: extra command-line argument "=" ignored
psql: warning: extra command-line argument "c.id" ignored
psql: warning: extra command-line argument "WHERE" ignored
psql: warning: extra command-line argument "cl.license_display" ignored
psql: warning: extra command-line argument "=" ignored
psql: warning: extra command-line argument "'Unknown" ignored
psql: warning: extra command-line argument "License'" ignored
psql: warning: extra command-line argument "GROUP" ignored
psql: warning: extra command-line argument "BY" ignored
psql: warning: extra command-line argument "c.component_name," ignored
psql: warning: extra command-line argument "c.component_version_name," ignored
psql: warning: extra command-line argument "c.version_origin_id;" ignored
psql: FATAL: role "c.component_name" does not exist