In bash getting weird output from function ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In bash getting weird output from function ?
# 1  
Old 04-28-2010
In bash getting weird output from function ?

My script-
Code:
result=""
times()
{
echo "inside the times function"
result=8
echo "Inside function $result"
return $result
}
result=$(times)
echo "the value is "$?
echo "the value of result $result"

when I run I get this, why the value still remain 0.

Code:
$ ./func
the value is 0
the value of result 0m0.015s 0m0.031s
0m0.000s 0m0.000s

Even the echo command also did not print anything.
# 2  
Old 04-28-2010
Hi.

times is a shell built-in. Choose a different name for your function.

Code:
$ cat myTest 
result=""
xtimes()
{
echo "inside the times function"
result=8
echo "Inside function $result"
return $result
}
result=$(xtimes)
echo "the value is "$?
echo "the value of result $result"

$ ./myTest
the value is 8
the value of result inside the times function
Inside function 8

# 3  
Old 04-28-2010
This works for me (with bash 2 and 3).
Code:
the value is 8
the value of result inside the times function
Inside function 8

But to be on the safe side, use a different name for your function, because times is the name of a shell builtin function.
# 4  
Old 04-28-2010
oh g8t, I changed the name it worked. But also 1 more thing to notice here is, it did not echo the value of global variable.

Code:
the value of result

# 5  
Old 04-28-2010
Quote:
Originally Posted by boy18nj
oh g8t, I changed the name it worked. But also 1 more thing to notice here is, it did not echo the value of global variable.

Code:
the value of result

The value of $result is that of the two echo's inside the function (in red) - not to be confused with the return value of the function (8):
Code:
the value is 8
the value of result inside the times function
Inside function 8

and is echo'd correctly.
# 6  
Old 04-28-2010
Scott, Thank you. You are the real expert. As I am a java programmer, I thought it will work this way-

Code:
result=$(times)

It will call times function and the return value will go in variable result. But no, the control went to result. So this result will be used to call the function.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Weird output from ps command

Hi Guys, I am regular Solaris user. I came across a weird problem that puzzled me. Hope you guys can help. I found that process's state(command & arguments) in two different variants of ps command is different. Can anyone explain how is this possible? bash-3.2$ ps -eLo pid,s,comm,args |... (2 Replies)
Discussion started by: brij123
2 Replies

2. Shell Programming and Scripting

Sqlplus function output to bash

Hi, I would like to have the output from an Oracle procedure be captured into a bash variable, then emailed to me when it runs on the cron daily as such: ~~~~~bash script~~~~~~~~~~~ #!/bin/bash shellvar=`sqlplus -s <<EOF execute test(); commit; exit; EOF` echo $shellvar mail -s "email... (1 Reply)
Discussion started by: inlinesidekick
1 Replies

3. HP-UX

[Solved] Weird 'ls -l' output

Hello folks, I've found an HP-UX server with a rare 'ls -l' output. Please see the attached file. Anybody knows how can I change the output to not have this extra tabulations? Thanks in advance! (10 Replies)
Discussion started by: carpannav
10 Replies

4. Programming

A weird problem with POSIX function

Hi all, Sorry for the title because I didn't find a proper name for it. My question is about POSIX functions, such as timer_create(), mq_open() and pthread_create(). void test_queue() { struct mq_attr attr; attr.mq_maxmsg = 10; attr.mq_msgsize = 64; mq_unlink("/my_test_queue");... (6 Replies)
Discussion started by: bus147
6 Replies

5. Shell Programming and Scripting

Traceroute script weird output

This script is giving weird output #!/bin/bash NETPATH=(`/bin/traceroute -n 4.2.2.2 | awk '{print $2}'`) for i in "${NETPATH}" do echo $i done The output: to 11.11.11.1 1.1.1.1 99.111.208.2 traceroute_test.sh traceroute_test.sh (7 Replies)
Discussion started by: thumbs
7 Replies

6. UNIX for Dummies Questions & Answers

weird 'ls' output

Hi, Anyone knows why I can't display the contents of my directory and how to fix this? http://i50.tinypic.com/4smfth.jpg Thanks in advance for any advise. Deanne Double post. Continued here. (0 Replies)
Discussion started by: Deanne
0 Replies

7. Shell Programming and Scripting

format of output is weird

hi all, have a ksh script which connects to a database and runs a sql and dumps it to a '.csv' file. The problem is the result is in multiple rows with long spaces in between when it should be just a single line and this screws up the format in the '.csv' file. script is : #!/bin/ksh... (1 Reply)
Discussion started by: cesarNZ
1 Replies

8. UNIX for Dummies Questions & Answers

Weird character in between echo function

Hi All, Appreciate if anyone can help. I've a script where it does echo function like this while do FILE_ARG="cu0${w}_${FILE}_${DT}.av" ORACLE_ERROR=`grep "ORA-" ${FILE_ARG}` if ]; then Func_Log_Writer "Fail! ${FILE_ARG}\n" Func_Log_Writer "Error message:... (2 Replies)
Discussion started by: agathaeleanor
2 Replies

9. Shell Programming and Scripting

weird echo output?

#!/bin/bash INPUT=$1 if then INPUT=0$1 TRACKNUMBER=$INPUT fi TRACKNUMBER=$INPUT echo "Track Number:" $TRACKNUMBER if then echo "File Does Not Exist!: split-track"${TRACKNUMBER}".wav" exit 0 fi CUEFILE="$2" (6 Replies)
Discussion started by: TinCanFury
6 Replies

10. AIX

Weird bootlist output

just setup a new system today - 9117 570, using HMC for console but not partitioned. I installed this system twice. the first time it started off as 5300-00, then updated to ML03. before the update, I believe I mirrored the rootvg, and then altered the bootlist, at that point, the display was... (4 Replies)
Discussion started by: davew1099
4 Replies
Login or Register to Ask a Question