Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple line output in function

Status
Not open for further replies.

fiep

Technical User
Sep 2, 2003
66
NL
Hi,

I have created a function to do a database call:


function calldb
{
OUTPUT=`some DB command`
echo "OUTPUT =$OUTPUT"
}


But if I use the function the output is one line:


Output = -------- IQ_SYSTEM_MAIN MAIN T T 21 1000M 0B 1 1 T 1K 1H,25568F,32D,128M N IQ_SYSTEM_TEMP TEMPORARY T T 1 250M 0B 1 1 T 1K 1H,64F,32A N user_main MAIN T T 1 1000M 0B 1 1 T 1K 1H,24A N (3 rows affected) (return status = 0)


All newlines have been stripped from the output.
If I place the same call in the main part of the script the output looks fine. So including all the newlines.

Any idea what I have to do in order for the output to show the output as multiple lines?
I need the lines for further processing.

Thanks
 
Hi

It works for me on Linux with [tt]bash[/tt] and [tt]mksh[/tt] :
Code:
[blue]master #[/blue] function calldb {
[blue]>[/blue]     OUTPUT=`psql -d test -c 'select id,date from test'`
[blue]>[/blue]     echo "OUTPUT =$OUTPUT"
[blue]>[/blue] }

[blue]master #[/blue] calldb
OUTPUT =   id   |             date              
--------+-------------------------------
 390979 | 2010-01-04 12:23:24.363888+02
 390981 | 2010-01-04 12:23:24.800407+02
 390980 | 2010-01-04 12:23:24.596168+02
(3 rows)
I would say, the word splitting is performed later, somewhere else where you forgot some quotes.

If not, tell us about your shell and operating system.

Feherke.
 
Hi Feherke,

Thanks for the reply. I use ksh on Solaris 10.
In the main of the script I call the function calldb

The functions looks like this:

function calldb
{
SQLOUTPUT=`${ISQL} -l $TIMEOUT -U${AC} -S${SRV} << EOFSQLC15 -w300 | sed -n '7,$p'
$(echo ${PWD})
sp_iqdbspace
go
EOFSQLC15`
print "Output = $SQLOUTPUT"
}


Output is as in the first post, so one line. The output does not look pretty even when I do it on the command line.

Thanks
 
Try enclosing your isql command in double quotes...
Code:
function calldb
{
SQLOUTPUT=[COLOR=red yellow][b]"[/b][/color]`${ISQL} -l $TIMEOUT -U${AC} -S${SRV} << EOFSQLC15 -w300 | sed -n '7,$p'
$(echo ${PWD})
sp_iqdbspace
go
EOFSQLC15`[COLOR=red yellow][b]"[/b][/color]

print "Output = $SQLOUTPUT"
}
 
Thanks for the tip, but it does not work.
Funny thing is that if I set -x in the function I see that the debug output is correct. i.e. all lines are printed seperately.
My print statement does not show any newlines charachter though so still one line:


...
+ /appl/sybase/sw/iq1510.17515/IQ-15_1/bin64/iqisql -l -Utivoli_mon -SIDB_AM_CPE -w300
+ 0<<
sp_iqdbspace
go
...snip
+ echo Output = --------
IQ_SYSTEM_MAIN MAIN T T 21 1000M 0B 1 1 T 1K
1H,25568F,32D,128M
N
IQ_SYSTEM_TEMP TEMPORARY T T 1 250M 0B 1 1 T 1K
1H,64F,32A N
user_main MAIN T T 1 1000M 0B 1 1 T 1K
1H,24A N

(3 rows affected)
(return status = 0)
Output = -------- IQ_SYSTEM_MAIN MAIN T T 21 1000M 0B 1 1 T 1K 1H,25568F,32D,128M N IQ_SYSTEM_TEMP TEMPORARY T T 1 250M 0B 1 1 T 1K 1H,64F,32A N user_main MAIN T T 1 1000M 0B 1 1 T 1K 1H,24A N (3 rows affected) (return status = 0)


So I am still lost.
 
Can you show us what the code looks like where you are actually calling the calldb function?

Annihilannic.
 
Hi,

This is how I call the function:

T=`checkmainstore $VERSION`
if [[ $? -ne 0 ]]
then
echo "${SRV};;;Time Out on Command"
exit 0
else
echo $T
fi


Kind regards
 
Hi

And what is checkmainstore ? Have you renamed calldb ?

If yes, the I was probably right when I wrote "the word splitting is performed later, somewhere else where you forgot some quotes". Try this :
Code:
[navy]T[/navy][teal]=[/teal]`checkmainstore [navy]$VERSION[/navy]`
[b]if[/b] [teal][[[/teal] [navy]$?[/navy] -ne [purple]0[/purple] [teal]]][/teal]
[b]then[/b]
   echo [green][i]"${SRV};;;Time Out on Command"[/i][/green]
   [COLOR=chocolate]exit[/color] [purple]0[/purple]
[b]else[/b]
   echo [highlight][red]"[/red][/highlight][green][i]$T[/i][/green][highlight][red]"[/red][/highlight]
fi

Feherke.
 
That is it.
I knew it had to be something simple and lost lots of time finding this.

Thanks a lot all of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top