[英]batch file to store the psql query output into a variable
嗨,我正在創建一個批處理文件來執行查詢並為輸出寫入日志文件。 因此,根據某些條件,將執行兩個查詢。 所以我試圖將查詢的輸出值存儲到一個變量中,並使用該變量值來檢查條件。 但是在這里我面臨一個問題,我應該如何使用 psql 命令聲明輸出值並將其存儲到變量中。 請幫忙!! 這是我嘗試過的:
SET /A a = psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"
SET /A b = 1
if %a% == 3 goto is_stat
else goto is_start
REM to copy the log
:is_start
psql -U postgres -d rpd -c "SELECT
a.table_name , 'MATCH' status FROM rpd.rpm_rpd_count A WHERE
a.rpd_count = a.rpm_count UNION ALL SELECT a.table_name, 'NOT MATCH'
AS status FROM rpd.rpm_rpd_count A WHERE a.rpd_count <> a.rpm_count;" >> C:\Users\admin\Desktop\err.log
goto END
:is_stat
psql -U postgres -d rpd -c "SELECT a.table_name , 'MATCH'
status FROM rpd.rpm_rpd_count A WHERE a.rpd_count = a.rpm_count;" >>
C:\Users\admin\Desktop\err.log
goto END
:END
echo %b% >> C:\Users\admin\Desktop\err.log
這里的問題是變量a中沒有保存任何內容
您可以使用 for 循環來解析輸出
for /f %%i in ('psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"') do set A=%%i
(如果您在命令行控制台中嘗試,請使用單個%
,並在批處理文件中使用雙%%
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.