Function invocation $(func "hi") does not work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function invocation $(func "hi") does not work
# 1  
Old 05-23-2013
Function invocation $(func "hi") does not work

Code:
#! /usr/bin/sh

func () {

        echo " PRAMATER RECEIVED IS $1"
        return 1
}
VALUE=$(func "Hi")

Not Working!. but if i invoke like:
Code:
func "Hi"

Works!!

Wat is it that i missing in previous invocation and why it is not working ..

Last edited by Franklin52; 05-23-2013 at 03:16 AM.. Reason: Please use code tags
# 2  
Old 05-23-2013
What do you mean by "Not working!"? Is it printing a diagnostic message? (If so, what is the message?)

What system are you using? On some systems, /usr/bin/sh may be a Bourne shell that doesn't recognize the $(...) form of command substitution. If that is the case, you could try:
Code:
VALUE=`func "Hi"`

Are you just saying that VALUE=$(func "Hi") didn't produce any output. If so, know that it shouldn't produce any output. If you wanted to see that the variable assignment worked as expected, you could use:
Code:
echo "$VALUE"

after the assignment to see what has been stored in the shell variable VALUE.
# 3  
Old 05-23-2013
Not Working means function statements are not executed(no diagnostic msg either) so prompt is returned without any output.

i tried :

VALUE=`func "Hi'` did not work(above statement holds good here also)...

So, wat is it that i am missing here?

echo $VALUE yes i will add this line, but a question here can i get a string in VALUE from function? or can i return a string from function?
# 4  
Old 05-23-2013
Quote:
Originally Posted by shreedhar.rk123
Not Working means function statements are not executed(no diagnostic msg either) so prompt is returned without any output.

i tried :

VALUE=`func "Hi'` did not work(above statement holds good here also)...

So, wat is it that i am missing here?

echo $VALUE yes i will add this line, but a question here can i get a string in VALUE from function? or can i return a string from function?
Both of the statements that you say are not working seem to be working just fine. Neither of them should produce any output. Both:
Code:
VALUE=`func "Hi"`

and
Code:
VALUE=$(func "Hi")

assign the text produced by func to the variable named VALUE (after stripping off the trailing newline). That assignment does not produce any output unless the shell detects an error while performing the assignment.

Note however, that the command:
Code:
VALUE=`func "Hi'`

is very different from the command:
Code:
VALUE=`func "Hi"`

because the opening quote and closing quote around Hi have to match.
# 5  
Old 05-23-2013
yes, the execution is fine with no errors but at the same time no output(which is not wat is expected).

i am expecting the
echo "PRAMATER RECEIVED IS $1"
to get executed and is seen on STDOUT.

in any of the cases

VALUE=`func "hi"`
or
VALUE=$(func "hi")
# 6  
Old 05-23-2013
Quote:
Originally Posted by shreedhar.rk123
yes, the execution is fine with no errors but at the same time no output(which is not wat is expected).

i am expecting the
echo "PRAMATER RECEIVED IS $1"
to get executed and is seen on STDOUT.

in any of the cases

VALUE=`func "hi"`
or
VALUE=$(func "hi")
Your expectation is wrong. That is not the way the shell works when you use command substitution in an assignment. The echo is executed, but it will not be seen on STDOUT because you are capturing the output and assigning it to a variable instead of writing it to STDOUT.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-23-2013
Quote:
Originally Posted by shreedhar.rk123
yes, the execution is fine with no errors but at the same time no output(which is not wat is expected).

i am expecting the
echo "PRAMATER RECEIVED IS $1"
to get executed and is seen on STDOUT.

in any of the cases

VALUE=`func "hi"`
or
VALUE=$(func "hi")
Based on what do you think this is not the expected output?

Adding to Don Cragun's explanation, from your example, when you run a command in backticks, the output of that command is stored in a variable.

When you say cont=`cat file.txt`, the contents of file.txt are stored in variable "cont".

Similarly, when you invoke the function "func", the output of func is stored in variable "VALUE". This is the expected behaviour.

Last edited by balajesuri; 05-23-2013 at 04:07 AM.. Reason: Late post by a minute. OP's query already clarified by Don Cragun.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. OS X (Apple)

Neither "which" nor "find" commands work

hi there, could use some basic PATH advice, i think, or something find sometimes work, but which hasn't ever seemed to. for years! what am i doing wrong that the commands which and find rarely work? they used to work on the workstations i used ages ago... running 10.5.8 because i... (4 Replies)
Discussion started by: zensnob
4 Replies

5. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

6. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

7. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question