Help in function returns value


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in function returns value
# 1  
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.

Code:
#!/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=$?
echo 'value of var is' $var

It is not working.. could anyone please help me out ?

Regards,
Nantha.

Last edited by jim mcnamara; 03-05-2015 at 04:15 PM..
# 2  
Please use code tags as required by forum rules!

shells' return - unlike its cousins in laguages like awk - can't be used to give back arbitrary values to the caller:
Quote:
- return [ n ] Causes a shell function or . script to return to the invoking script with the exit status specified by n. The value will be the least significant 8 bits of the spec- ified status. If n is omitted, then the return status is that of the last command executed. If return is invoked while not in a function or a . script, then it behaves the same as exit.
Use global variables or assign a function's result to a variable using "command substitution".
# 3  
Regarding command substitution:
either use `command` or $(command). You shouldn't nest them.
The function simply prints to stdout.
Example:
Code:
hello(){
  < /dev/urandom tr -dc 'a-zA-Z0-9-!%&()*+,-/:;<=>?_' | fold -w 10 | head -n 1
}

var=$(hello)
#var=`hello`

---------- Post updated at 03:57 PM ---------- Previous update was at 03:45 PM ----------

Global variables have the disadvantage that the calling code nust know the variables in the function.
Example:
Code:
hello(){
  hello_return=$(< /dev/urandom tr -dc 'a-zA-Z0-9-!%&()*+,-/:;<=>?_' | fold -w 10 | head -n 1)
}

hello
var=$hello_return

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #508
Difficulty: Easy
A variable is basically a memory location in which a value can be stored.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit always returns 0

This returns 0 even when it does not delete any files. Is it because -print returns 0? RETVAL=$? Docs_Backups=/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/ Scripts_Backups=/media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Script_Backups/ # create some old files #touch -d 20120101... (4 Replies)
Discussion started by: drew77
4 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

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

4. Shell Programming and Scripting

Calculation returns no value

#/bin/sh ..... #convert memory to MB let "mmsize_a= ($mmsize)/256" let "mminuse_a= ($mminuse)/256" let "mmfree_a= ($mmsize_a -$mminuse_a)" let "mmfreepercent= (($mmfree_a)/($mmsize_a))*100" # #format output echo "\n\n######################" >>$sndFile echo "\n$sysName Total Memory usage"... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

5. 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

6. Shell Programming and Scripting

Grep returns nothing

Hi all, I am trying to grep a .txt file for a word. When I hit enter, it returns back to $ The file is 4155402 in size and is named in this way: *_eveningtimes_done_log.txt I use this command, being in the same directory as the file: grep -i "invalid" *_eveningtimes_done_log.txt ... (16 Replies)
Discussion started by: DallasT
16 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Grep without returns...

Is there a command where I can pipe my grep into it and it will output it with spaces rather than returns? Example I want to turn prompt$ grep blah file blah blah into this prompt$ grep blah file | someCommand blah blah (1 Reply)
Discussion started by: mrwatkin
1 Replies

9. Shell Programming and Scripting

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. ... (5 Replies)
Discussion started by: lalelle
5 Replies

Featured Tech Videos