public inbox for [email protected]  
help / color / mirror / Atom feed
Bash function from psql (v14)
5+ messages / 4 participants
[nested] [flat]

* Bash function from psql (v14)
@ 2024-11-09 17:41  Murthy Nunna <[email protected]>
  0 siblings, 3 replies; 5+ messages in thread

From: Murthy Nunna @ 2024-11-09 17:41 UTC (permalink / raw)
  To: [email protected] <[email protected]>

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bash function from psql (v14)
@ 2024-11-09 18:02  Efrain J. Berdecia <[email protected]>
  parent: Murthy Nunna <[email protected]>
  2 siblings, 0 replies; 5+ messages in thread

From: Efrain J. Berdecia @ 2024-11-09 18:02 UTC (permalink / raw)
  To: [email protected]; [email protected] <[email protected]>

Maybe try using full path...

Yahoo Mail: Search, Organize, Conquer 
 
  On Sat, Nov 9, 2024 at 12:41 PM, Murthy Nunna<[email protected]> wrote:   
<!--#yiv2777555521 filtered {}#yiv2777555521 filtered {}#yiv2777555521 p.yiv2777555521MsoNormal, #yiv2777555521 li.yiv2777555521MsoNormal, #yiv2777555521 div.yiv2777555521MsoNormal {margin:0in;font-size:11.0pt;font-family:"Calibri", sans-serif;}#yiv2777555521 span.yiv2777555521EmailStyle17 {font-family:"Calibri", sans-serif;color:windowtext;font-weight:normal;font-style:normal;}#yiv2777555521 .yiv2777555521MsoChpDefault {}#yiv2777555521 filtered {}#yiv2777555521 div.yiv2777555521WordSection1 {}-->
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
 
  
 

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bash function from psql (v14)
@ 2024-11-09 18:22  Vijaykumar Jain <[email protected]>
  parent: Murthy Nunna <[email protected]>
  2 siblings, 0 replies; 5+ messages in thread

From: Vijaykumar Jain @ 2024-11-09 18:22 UTC (permalink / raw)
  To: Murthy Nunna <[email protected]>; +Cc: [email protected] <[email protected]>

On Sat, 9 Nov 2024 at 23:11, Murthy Nunna <[email protected]> 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 <<EOS
\! bash /tmp/myscript.sh
EOS

postgres

*/



-- 
Thanks,
Vijay

Open to work
Resume - Vijaykumar Jain






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Bash function from psql (v14)
@ 2024-11-10 14:55  Greg Sabino Mullane <[email protected]>
  parent: Murthy Nunna <[email protected]>
  2 siblings, 1 reply; 5+ messages in thread

From: Greg Sabino Mullane @ 2024-11-10 14:55 UTC (permalink / raw)
  To: Murthy Nunna <[email protected]>; +Cc: [email protected] <[email protected]>

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


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* RE: Bash function from psql (v14)
@ 2024-11-10 15:21  Murthy Nunna <[email protected]>
  parent: Greg Sabino Mullane <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Murthy Nunna @ 2024-11-10 15:21 UTC (permalink / raw)
  To: Greg Sabino Mullane <[email protected]>; +Cc: [email protected] <[email protected]>

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.


^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2024-11-10 15:21 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-11-09 17:41 Bash function from psql (v14) Murthy Nunna <[email protected]>
2024-11-09 18:02 ` Efrain J. Berdecia <[email protected]>
2024-11-09 18:22 ` Vijaykumar Jain <[email protected]>
2024-11-10 14:55 ` Greg Sabino Mullane <[email protected]>
2024-11-10 15:21   ` Murthy Nunna <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox