Bash function from psql (v14)

Поиск
Список
Период
Сортировка
Искать

Bash function from psql (v14)

От:
Murthy Nunna <mnunna@fnal.gov>
Дата:

Hi,

 

I am trying to code bash function and call it from psql. But it is failing. How can I get this to work. Creating a separate script instead of a function works, but I do not want to do that. I have too many variables to be passed back and forth. Any ideas?

 

#!/bin/bash

psql -d postgres  -p 5433 <<-PSQLBLOCK

\! run-bash-function                                        -- This doesn’t work

PSQLBLOCK

 

# Run the function outside PSQLBLOCK. This works!

run-bash-function

exit $?

 

# Create bash function

run-bash-function ()

{

  echo "in bash function"

}

# end of bash script

 

 

Run the above script:

./test-bash-function.sh

sh: line 1: run-bash-function: command not found

in bash function

 

Re: Bash function from psql (v14)

От:
"Efrain J. Berdecia" <ejberdecia@yahoo.com>
Дата:
Maybe try using full path...


On Sat, Nov 9, 2024 at 12:41 PM, Murthy Nunna
<mnunna@fnal.gov> wrote:

Hi,

 

I am trying to code bash function and call it from psql. But it is failing. How can I get this to work. Creating a separate script instead of a function works, but I do not want to do that. I have too many variables to be passed back and forth. Any ideas?

 

#!/bin/bash

psql -d postgres  -p 5433 <<-PSQLBLOCK

\! run-bash-function                                        -- This doesn’t work

PSQLBLOCK

 

# Run the function outside PSQLBLOCK. This works!

run-bash-function

exit $?

 

# Create bash function

run-bash-function ()

{

  echo "in bash function"

}

# end of bash script

 

 

Run the above script:

./test-bash-function.sh

sh: line 1: run-bash-function: command not found

in bash function

 

Re: Bash function from psql (v14)

От:
Vijaykumar Jain <vijaykumarjain.github@gmail.com>
Дата:
On Sat, 9 Nov 2024 at 23:11, Murthy Nunna  wrote:

> I am trying to code bash function and call it from psql. But it is failing. How can I get this to work. Creating a separate script instead of a function works, but I do not want to do that. I have too many variables to be passed back and forth. Any ideas?

> Run the above script:
>
> ./test-bash-function.sh
>
> sh: line 1: run-bash-function: command not found
>
> in bash function
>
>

/*

postgres@ubuntu:/tmp$ echo 'whoami' >> myscript.sh
postgres@ubuntu:/tmp$ chmod a+x myscript.sh
postgres@ubuntu:/tmp$ psql <

Re: Bash function from psql (v14)

От:
Greg Sabino Mullane <htamfids@gmail.com>
Дата:
What problem are you trying to solve? If you tell us that, we can guide you to some better solutions.

There are numerous issues here, but the most important are:

1) Calling a shell via \! invokes an entirely new process: there is no link to the parent or grandparent process

2) The run-bash-function must be declared before being called, so that bash code was never going to work anyway. In other words, this is valid:

run-bash-function() { echo "Here we are"; }

run-bash-function

This version is not:

run-bash-function

run-bash-function() { echo "Here we are"; }


Cheers,
Greg

RE: Bash function from psql (v14)

От:
Murthy Nunna <mnunna@fnal.gov>
Дата:

Thanks, Greg.

 

  1. This means what I am trying to do is not meant to work. This won’t work even if I declare the function before calling it in PSQL. I get it.
  2. You are correct. Function must be declared before the call.

 

Thanks again.

FAQ