0

Need to call env file in shell script

PGHOST=${PGHOST-'local host'}
PGPORT=${PGPORT-5432}
PGDATABASE=${PGDATABASE-'mydb'}
PGUSER=${PGUSER-'myuser'}
PGPASSWORD=${PGPASSWORD-'pwd#0000'}

My shell script

#!/bin/bash
export MYPATH ="C/Documents/my_env.env":$MYPATH
# If psql is not available, then exit
if ! command -v psql > /dev/null ; then
  echo "This script requires psql to be installed and on your PATH. Exiting"
  exit 1
fi

RUN_PSQL="psql -X --set AUTOCOMMIT=off --set ON_ERROR_STOP=on "

${RUN_PSQL} <<SQL
select count(1) from my_table
SQL

When I try to run it throws error saying syntax error unexpected end of file and warning here document delimited by end of file.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • Does this answer your question? [Set environment variables from file of key/value pairs](https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs) – Luuk Apr 04 '23 at 15:49
  • Do you have ms DOS line ending? If yes, use `dos2unix` – Gilles Quénot Apr 04 '23 at 15:49
  • Hi Luuk, I referenced the link you shared and tried all including source command but it didn’t work . Am I missing anything here ? Thanks for your reply – Udayasoorian CN Apr 04 '23 at 16:01
  • 2
    @UdayasoorianCN Please [edit] your question, add what exactly you tried based on the linked question, copy&paste the resulting output and show the expected output. Explain in which way it "didn't work", unless it is obvious from the error message or from actual and expected output. – Bodo Apr 04 '23 at 16:05
  • Hi Bodu, thanks for your reply . My output shows ' No such file or directoryator/test_file.sh: line 2: ./test_env.env warning: here-document at line 13 delimited by end-of-file (wanted `SQL /c/Documents/test_file.sh: line 18: syntax error: unexpected end of file – Udayasoorian CN Apr 05 '23 at 06:32
  • This is the error i get when i try to execute a shell script – Udayasoorian CN Apr 05 '23 at 06:36
  • 1) It is pretty unclear what/where line #13 (or #18) is. 2) You get an error about `/test_file.sh`, which is possibly (???, I am guessing here!) the same file as `my_env.env` onl ine #3 of the script you did show. – Luuk Apr 05 '23 at 17:50
  • Syntax error: `export MYPATH ="C/Documents/my_env.env":$MYPATH`, do not put spaces around the `=` sign. Put your code in https://www.shellcheck.net/ to check syntax. – Nic3500 Apr 11 '23 at 06:31
  • Hi Nic, Thanks a lot for your reply. I will do it ...Thanks – Udayasoorian CN Apr 12 '23 at 13:41

0 Answers0