function returns string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting function returns string
# 1  
Old 08-23-2007
function returns string

Can I create a function to return non-interger value in shell script?

for example,
function getcommand ()
{
echo "read command"
read command
echo $command

}


command=$(getcommand)


I tried to do something as above. The statement echo "read command" does not show up.

Normally, how can we create a function to return something except an integer?
# 2  
Old 08-23-2007
Code:
#!/bin/ksh

concatenate_two_words()
{
    echo "$1""$2"
}

word1="hi"
word2="there"
both=$(concatenate_two_words "$word1" "$word2")
echo "$both"

To return a string echo "the string" as the last line of the function and then call the function as a subprocess
with either ` ` backtics or $ ( )
# 3  
Old 08-23-2007
another alternative is to define a "global" variable

funct_ret_value=""


function getcommand ()
{
echo "read command"
read command
funct_ret_value=$command

}


getcommand
echo $funct_ret_value
# 4  
Old 08-23-2007
Quote:
Originally Posted by jim mcnamara
To return a string echo "the string" as the last line of the function and then call the function as a subprocess
with either ` ` backtics or $ ( )
When you call a function in ` ` or $( ), the child process is spawned. Am I right?
Therefore, the statement echo "read command" does not print out to the shell I am running the script. How can I print it out to the running shell if I use the function that is called in ` ` or $( ). The echo statement is in that function.

Last edited by lalelle; 08-23-2007 at 04:14 PM.. Reason: missing words
# 5  
Old 08-23-2007
Why are you echo - ing and then reading a value inside of a function that runs as a subprocess?

- The echo "read command" appears in the variable you are trying to receive in the calling process. It does not appear on the console.
# 6  
Old 08-23-2007
Actually, it is nothing I want to do. I am starting to learn unix and I am trying many possible ways to make myself understood as much as possible. Therefore, this question came up when I compare a function in shell programming to other programming langauges.
Furthermore, the book I have been studying writes something about break:

while true
do
cmd=$(getcommand)
if [ "cmd" =quit ]
then
break
fi
done

See that made me think about how to write the getcommand function. So I put the echo-ing and read in a function and return a string.

Thanks for your answers anyway
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Function Returns

I'm having a little trouble returning a value from a function or calling it, I'm not quite sure. I'm calling the function here function region_lookup_with_details { results = $(set_region) echo $results } This is the function I'm calling function set_region { ... (8 Replies)
Discussion started by: akechnie
8 Replies

3. Shell Programming and Scripting

How do I get the first string value from function?

Hello All, I am trying to get the value "node01_mymachine" and disregard the rest of the returned string (command ran*) from myscript.sh $ myscript.sh GetNodeName node01_mymachine Command ran successfully. If I called from another script like this: anyprocess=`myscript.sh... (2 Replies)
Discussion started by: msetjadi
2 Replies

4. Shell Programming and Scripting

Help in function returns value

Hi, I need to return a value from the function. the value will be the output from cat command which uses random fucntion. #!/bin/ksh hello() { var1=$(`cat /dev/urandom| tr -dc 'a-zA-Z0-9-!%&()*+,-/:;<=>?_'|fold -w 10 | head -n 1`) echo "value is" var1 return var1 } hello var=$?... (2 Replies)
Discussion started by: Nandy
2 Replies

5. Shell Programming and Scripting

Function returns a value but cannot be stored in other variable

I need help to store the value returned from the function to one variable and then use that variable. PREVIOUS_DATE_FUNCTION() { date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

6. Shell Programming and Scripting

Python call to bash script returns empty string

Hi all, I need help figuring out why my script won't work when envoked from web interface. First off, I'm fairly new to Linux and Shell scripting so, my apologies for being ignorant! So I have a python script that I envoke through a web interface. This script envokes my shell script and... (8 Replies)
Discussion started by: arod291
8 Replies

7. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

8. UNIX for Dummies Questions & Answers

dlsym() returns 0 for an existing function

Sometimes I observe this in gdb: (gdb) br my_function Breakpoint .. at 0x...: file ..., line ... i.e., "my_function" does exist in the current executable. however, dlsym does not find it: (gdb) p dlsym(0,"my_function") $6 = 0 This is a C program; dlsym does find other defined functions and... (2 Replies)
Discussion started by: sds
2 Replies

9. Shell Programming and Scripting

Function returns wrong values - solved

Hi I have a small function which returns a wrong value. The function tries to make a connection to oracle database and tries to get the open_mode of the database in the variable status. However when a database is down the value of the status column is set to READWRITE i am not sure why. I... (0 Replies)
Discussion started by: xiamin
0 Replies

10. Programming

string function

I have a question concerning string functions. I have not been able to locate a function that does what I want, so I fugured I'd ask before I wrote on myself. Is there a function to which I can pass 2 strings (character string a and character string b) and have it tell me if string b appears... (7 Replies)
Discussion started by: jalburger
7 Replies
Login or Register to Ask a Question