Again, this is just a discussion point. And I'm quite willing to admit defeat if most people don't think that it is worth the effort.-1, at least per the example. I would not want "-U postgres" inside the file. I tend to rely on service entries, not environment variables, and wouldn't want to hard-code them either. While psql has grown more flow-control capabilities recently it is, in most cases, a support language for me, not a main entry point. Shell scripts merge the per-instance run-time environment I need with the behavior the script provides - merging that I find I need more often than not and don't miss the added overhead in the few cases where it is unnecessary.David J.
I agree. I wouldn't want the -U inside a "regular" shell script either. As a minor example, consider the following _almost_ equivalent scripts.
$ cat psql-script.sh
#!/bin/sh
psql "$@" -f - <<EOT
select * from table;
EOF
$ cat psql-script.sql
#!/usr/bin/psql -f -
select * from table
$ chmod 755 psql-script.{sh,sql}
$ ./psql-script.sh -U postgres -d somedb -h remote-host.com
$ ./psql-script.sql -U postgres -d somedb -h remote-host.com
These are _almost_ equivalent. The first execution shown after the chmod is effectively:
psql -U postgres -d somedb -h remote-host.com -f - <<EOT
select * from table;
EOT
The second is effectively:
/usr/bin/psql -f ./psql-script.sql -U postgres -d somedb -h remote-host.com
The only difference is whether the -f is "at the front" or "at the end" of the "generated" command which is actually sent to the exec() function. In reality, from what the BASH maintainer has said, the first script is a bit like:
file=$(mktemp) # generate a temporary file name
{ cat <<EOT
select * from table;
EOT
} >${file}
psql -U postgres -d somedb -h remote-host.com -f ${file}
It just that the HERE document doesn't actually create the ${file} variable. I have NO idea how other shell implement HERE documents.
However, in the second case, the "magic" first line causes psql, at present, to report an error and abort. This is why I'd like to modify how the file referenced via the -f argument is processed. That is, the first line of any file referenced & executed via the -f argument will be ignored if and only if it starts with a shebang (#!). If the first line of the file does not start with a shebang, it is processed normally as are all subsequent lines.
If I get the energy & time, I'll give a look at the actual source. If it is within my, admitted limited, ability to generate a patch to implement what I'm thinking of, I'll post it over on the development forum.
We all have skeletons in our closet.
Mine are so old, they have osteoporosis.
Maranatha! <><
John McKown
John McKown