0

The following command works well:

psql -h localhost -U user dados -f /etc/bkpdb/04-03-2015.sql&

However, when I use a variable in it:

valor=04-03-2015
psql -h localhost -U user dados -f /etc/bkpdb/$valor.sql&

It does not work.

It happens the following error:

.sql: No such file or directory2015
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

Your code works fine, the problem is that your file is saved on Windows where lines end with \r\n instead of Unix \n.

You can tell because of the weird, trailing 2015 in the error message. It got there because the tool intended to write out a message like:

psql: failed: /etc/bkpdb/04-03-2015\r.sql: No such file or directory

But due to the carriage return \r, the cursor moves to the start of the column, and starts overwriting the start of the message:

p̶s̶q̶l̶:̶ ̶f̶a̶i̶l̶e̶d̶:̶ ̶/̶e̶t̶c̶/̶b̶k̶p̶d̶b̶/̶0̶4̶-̶0̶3̶-2015
.sql: No such file or directory

You're therefore left with:

.sql: No such file or directory2015

To solve this, just convert your file from Windows/DOS format to Unix, either in your text editor or with dos2unix, fromdos or tr -d '\r'. Here's trouble shooting step #1 from the bash tag wiki:

  1. Check whether your script or data has DOS style end-of-line characters

    • Use cat -v yourfile or echo "$yourvariable" | cat -v .

      DOS carriage returns will show up as ^M after each line.

      If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'

Community
  • 1
  • 1
that other guy
  • 116,971
  • 11
  • 170
  • 194
-2

Try:

valor=04-03-2015
psql -h localhost -U user dados -f /etc/bkpdb/${valor}.sql &
Vic
  • 437
  • 3
  • 8