Return vs. Echo


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return vs. Echo
# 1  
Old 02-03-2009
Return vs. Echo

In a Ksh functions, when you have both echo and return respectively - what does it do. E.g.

Func B ()
{
.....
{
.....
echo $Varaible
}
Return 0
}


Func A ()
{
$Var1 = Func B()
....
....
}

Echo $Var1 produces the value of $Variable, so where did Return 0 go? What if echo was not there then would would be the result?

Thanks
-Novice
# 2  
Old 02-03-2009
There is no similarity between them: echo prints its arguments to stdout; return sets a return code which may be tested directly (by if or a control operator, && or ||) or indirectly in the contents of $?.
# 3  
Old 02-04-2009
Return vs. Echo

Hi cfajohnson

Thanks for the reply. In the code below GetData() calls get_primary and is_primary functions

GetData()
{

prim_mach=`get_primary`
gprim_mach=`is_primary`

echo $prim_mach
echo $gprim_mach
}

-------------------

is_primary()
{
TFILE=$TEMPDIR/tempFile.$$
this_server=`uname -n`
dbaccess << PRIM > /dev/null 2>&1
database sscDB;
unload to $TFILE select s_status from serverstbl where s_name = "$this_server";
PRIM
[ -f $TFILE ] &&
{
status=`cat $TFILE | awk -F"|" '{ print $1 }'`
rm $TFILE
[ "$status" = "P" ] && return 0
}
return 1
}

----------------

TFILE=$TEMPDIR/tempFile.$$
dbaccess << GPRIMM > /dev/null 2>&1
database sscDB;
unload to $TFILE select s_name from serverstbl where s_status = "P";
GPRIMM
[ -f $TFILE ] &&
{
status=`cat $TFILE | awk -F"|" '{ print $1 }'`
rm $TFILE
echo $status
}
return 0
}

----------------------------------------------------
echo $prim_mach prints the column value - echo is the only command I see in the code that can send the output to $prim_mach variable in GetData(). So echo sent value to $prim_mach. But it returned 0 too. How do I see the 0? I thought the variable $prim_mach will have 0 in it, but it showed the echoed value.


echo $gprim_mach prints nothing, becasue there is no echo. But it did return 0 or 1 depending upon the condition. How can I see that?

Thanks.
# 4  
Old 02-04-2009
Quote:
Originally Posted by gagan8877
echo $gprim_mach prints nothing, becasue there is no echo. But it did return 0 or 1 depending upon the condition. How can I see that?

I repeat:
Quote:
in the contents of $?.
# 5  
Old 02-05-2009
Can you please show me the syntax of how to check the exit code in getdata() ?
# 6  
Old 02-05-2009
Quote:
Originally Posted by gagan8877
Can you please show me the syntax of how to check the exit code in getdata() ?

Code:
getdata
echo $?

If you want to check the error status of commands within getdata:

Code:
GetData()
{

prim_mach=`get_primary`
echo $?
gprim_mach=`is_primary`
echo $?

echo $prim_mach
echo $?
echo $gprim_mach
echo $?
}

Normally you would use a conditional expression and do something different depending on the exit code of the command.
# 7  
Old 02-05-2009
Thnx

Fantastic. Thanks a lot cfajohnson.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

If echo statement return false

I have this code that sometimes return a false value and the code inside the if statement gets executed and error out. Any idea why? thanks. So I set a debug and see what the value for $ScriptElapsedTime Here is the value I got ScriptElapsedTime='03:20'. Base on this value the if... (10 Replies)
Discussion started by: nugent
10 Replies

3. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

4. AIX

Return code 1 when echo to pipe

Hello, Our AIX box has recently been upgraded to TL12 (oslevel -s = 5300-12-04-1119). Now one of our ksh scripts is returning 1 when writing to a pipe, the command to write to the pipe is: echo "A" "B" "C" >> /usr/Pipe.Pipe Anyone have any ideas? Thanks (2 Replies)
Discussion started by: dendright
2 Replies

5. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

6. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

7. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

8. Shell Programming and Scripting

echo 2 txt files to screen no carraige return

I have two text files, each of then only containing ONE line and NO carraige return or white space at the end...how do I echo both of these text files to the screen without putting an extra line? I want to do this from the command line. file1.txt: this is file1.txt 1 file2.txt: this is... (4 Replies)
Discussion started by: ajp7701
4 Replies

9. UNIX for Advanced & Expert Users

Return the last echo in remsh !!!!

Hi, I have a question: the script A run in the HostA call the script B on the HostB: ex.. ksh:B ....... ........ ........ remsh HostB ec........ ...... ...... the prog.B on the host B make more function but the last command is echo of srting : ex ksk script B .... ...... (0 Replies)
Discussion started by: ZINGARO
0 Replies

10. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies
Login or Register to Ask a Question