Nandy
1
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=$?
echo 'value of var is' $var
It is not working.. could anyone please help me out ?
Regards,
Nantha.
RudiC
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:
Use global variables or assign a function's result to a variable using "command substitution".
Regarding command substitution:
either use `command`
or $(command)
. You shouldn't nest them.
The function simply prints to stdout.
Example:
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:
hello(){
hello_return=$(< /dev/urandom tr -dc 'a-zA-Z0-9-!%&()*+,-/:;<=>?_' | fold -w 10 | head -n 1)
}
hello
var=$hello_return