Запрос Postgresql в скрипте bash

2183
GuruFrog

У нас возникла проблема с запросом postgresql bash. Вывод запроса из скрипта bash добавляет дополнительные кавычки вокруг даты. Пожалуйста, вы можете помочь мне исправить это ?!

Автор сценария:

#!/bin/sh  lastmonth=$(date -d "-1 month -$(($(date +%d)-1)) days" +%Y-%m-%d) thismonth=$(date -d '-1 month -12 days' +%Y-%m-%d)  sudo -u postgres psql -d linetest_uptime -A -o /tmp/linetest_$(date +%Y-%m-%d).txt -c "select t.*, ip,port from line_test t,service_address s where '$thismonth' <= start and start < '$lastmonth' and service_address_id=s.id order by t.start,status desc" >/dev/null 2>&1 && sudo mv /tmp/linetest_$(date +%Y-%m-%d).txt /reporting/line_test_data/ && sudo chown $USER:$USER /reporting/line_test_data/linetest_$(date +%Y-%m-%d).txt 

Выход:

sudo -u postgres psql -d linetest_watchdog -A -o /tmp/linetest_2014-03-13.txt -c 'select t.*, ip,port from line_test t,service_address s where '\''2014-02-01'\'' <= start and start < '\''2014-02-01'\'' and service_address_id=s.id order by t.start,status desc 

Нам нужно удалить '\' '\', который теперь оборачивается вокруг переменной даты.

1
Вы пробовали использовать sed? tftd 10 лет назад 0

1 ответ на вопрос

0
Toby Speight

That doesn't look like output from your commands - is it some debug output instead?

What you have looks correct, in that it could be fed into the shell and give what you asked for. Note that 'aaa'\''bbb' is identical to "aaa'bbb"!

Side note: your variables look a bit overcomplicated, and you repeat $(date +%Y-%m-%d); the following may help:

lastmonth=$(date -d "-1 month" +%Y-%m-01) thismonth=$(date +%Y-%m-01) today=$(date +%Y-%m-%d) 

and the SQL query may be more idiomatic as ... where date between '$lastmonth' and '$thismonth' ....