Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to output function result

I have a bash shell script executed by Jenkins / Hudson. For some reason when I create a function, the output of that function is not logged back to the console. This is an example of my code:

EDIT!!! - this culprit here is SSH... Is there a way to force its output back to console?

perform_task()
{
   ssh [email protected] pwd
}

echo "Starting a task";
perform_task || { echo "The Task Failed"; exit 1; } 

The output in the console is:

"Starting a task"

if I move the commands outside of the function, I can see their output.

Help on this would be greatly appreciated!

like image 613
Greg R Avatar asked Aug 01 '26 01:08

Greg R


2 Answers

I partially solved the problem by getting the script to echo the command:

#!/bin/bash -ex

This still hides the output of that command, but at least I have insights about what's being run

like image 95
Greg R Avatar answered Aug 02 '26 18:08

Greg R


I just copied your function, added #!/bin/bash -e to the beginning of and pasted it into the "Execute Shell" of Jenkins.

It worked fine!

Is your bash executable actually located at /bin? What OS are you running the script on?

Building remotely on slave_1
[test] $ /bin/bash -xe /tmp/hudson1206345540964396738.sh
+ echo 'Starting a task'
Starting a task
+ perform_task
+ ssh slave_2 pwd
/home/jenkins
Finished: SUCCESS
like image 32
Sagar Avatar answered Aug 02 '26 17:08

Sagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!