Bash Script Returning Value and Echo - bash

I'm following the code mentioned here but it doesn't echo $result. Here is my code, I added quotations around the "$result". The echo in myfunc works, but the echo outside the function doesn't work. What is the problem? How do I fix it?
My Code:
#!/bin/bash
function myfunc()
{
local myresult="Hello World"
}
result=$(myfunc)
echo "$result"

#!/bin/bash
function myfunc()
{
local myresult="Hello World"
echo "$myresult" # the function need to return something
}
result=$(myfunc)
echo "$result"

Related

Pass function name as argument when running bash script

Assuming I have the following script:
#!/bin/bash
function hello (){
echo hello,
}
function world (){
echo world!
}
Is it possible to select the functions to run while I start the script?
For exmaple:
./test.sh hello world
output:
hello,world!
Just iterate over the arguments and run them.
for i in "$#"; do
"$i"
done
Do not use function name(), just name(). See https://wiki.bash-hackers.org/scripting/obsolete . Check your scripts with shellcheck .
You can use eval command to execute first argument.
#!/bin/bash
function hello (){
echo hello,
}
function world (){
echo world!
}
eval $1

Using global variables in functions with parameters in bash

I'm trying to catch a return value from a function in bash, that modify a global variable.
Works perfectly with funtions with no parameters:
#!/bin/bash
tes=2
testfunction(){
tes=3
tes_str="string result"
return 0
}
if output=testfunction; then
echo "OK"
else
echo "KO"
fi
echo $tes
echo $tes_str
But no with parameters:
#!/bin/bash
tes=2
testfunction(){
tes=3
tes_str="string result"
return 0
}
if output=$(testfunction "AA" "BB"); then
echo "OK"
else
echo "KO"
fi
echo $tes
echo $tes_str
Because for bash, parameters ("AA" or "BB") are a command, and I must put it in backets (but if use backets, can't modify global variables).
How can I do it? I'm stucked.
Regards
Why use output? Just remove it and run the function.
if testfunction; then
Notes:
output=testfunction is assigning the text testfunction to the variable output.
output=$(testfunction) will not work, because $(...) runs everything inside a subshell.

capturing a string return value from one function in bash into another

I am trying to send a string as a return value from a function which is being called by another function in a different file. One sources the other.
The code is, like so:
#####################################################
##filename: conf_abc.menu
#######################################################
#!/bin/bash
source <path>/conf_pqr.menu
function abc () {
var=$(call_pqr)
echo ${var}
}
##Calling function abc
abc
#########################################################
##filename: conf_pqr.menu
########################################################
#!/bin/bash
RET_VAL=""
function get_intf() {
cmd=`some command`
RET_VAL=${cmd}
}
function call_pqr () {
comm=$(array of choices)
for choice in $comm
do
case $choice in
IF)get_intf;
echo "$RET_VAL";;
esac
done
}
I expect to see the choice from the array in "var" of function abc().
But the "echo ${var}" in conf_abc.menu does not print anything.
I run the script by doing:
./conf_abc.menu
What am I doing wrong?
Thanks for the comments. I found that when the return is called from the function, the entire output from echo is captured into the return variable. I then filtered the output using ">&2". I find that I do get the proper return string.
This I found from:
stackoverflow.com/questions/11758368/shell-script-function-return-a-string
I have put in a minimal example here:
##**file: vlan_menu.sh**
#!/bin/bash
source <path>/ifs_menu.sh
function conf_vlan () {
echo "Calling function if_menu" >&2
local output1=$(if_menu);
echo "printing result" >&2
ENET1="${output1}"
echo "ENET1 is= "${ENET1}"" >&2
}
conf_vlan; --> calling the main function
##**file: ifs_menu.sh**
#!/bin/bash
RET_VAL=""
function get_if() {
PI=$1
local var1=$(shell command)
if [ ! -z "${PI}" ]
then
local var2=<do something with var1>
else
local var2=<do something with var1>
fi
local cmd=$(start shell utility)
RET_VAL=${cmd}
}
function if_menu() {
comm=(1 2 3 4 5)
for choice in ${comm}
do
case $choice in
1) echo "IF" >&2;
get_if "";
echo "${RET_VAL}";;
2) echo "SF" >&2;
get_if $1;
echo "${RET_VAL}";;
esac
done
}
After running "vlan_menu.sh" script file, the value of "ENET1" is the same as "RET_VAL" which is what I wanted to see.
-rsmitha.

bash echo wrapper issue

My question is simple.
I am using some echo/print wrappers instead of simple echo calls in a bigger bash script. Example:
!/bin/bash
function print_common () { echo "$#"; }
function print_inf () { echo "INFO: $(print_common $#)"; }
function print_war () { echo "WARN: $(print_common $#)"; }
function print_err () { echo "ERROR: $(print_common $#)"; }
Generally, it works very well. The below script1.sh:
!/bin/bash
print_err "Whoops!"
generates:
ERROR: Whoops!
However, script2.sh:
!/bin/bash
print_err "*** Whoops!"
generates:
ERROR: script2.sh Whoops!
The expected print result is:
ERROR: *** Whoops!
Thanks for your help!
In your echo/print wrappers, wrap up a variable print_common with single qoutes
Example:
function print_err () { echo "ERROR: '$(print_common $#)'"; }
If you don't wrap it inside quotes it will:
echo *** Whoops
And if you put a star (*) as an arguments to an echo command it will show you a list of every file inside directory where scirpt is executed.
Using quotes will show you what you want.

global variable value not set inside a function in shell script

I have two shell script like as follows:
a.sh
tes=2
testfunction(){
tes=3
echo 5
}
testfunction
echo $tes
b.sh
tes=2
testfunction(){
tes=3
echo 5
}
val=$(testfunction)
echo $tes
echo $val
In first script tes value is '3' as expected but in second it's 2?
Why is it behaving like this?
Is $(funcall) creating a new sub shell and executing the function? If yes, how can address this?
$() and `` create new shell and return output as a result.
Use 2 variables:
tes=2
testfunction(){
tes=3
tes_str="string result"
}
testfunction
echo $tes
echo $tes_str
output
3
string result
Your current solution creates a subshell which will have its own variable that will be destroyed when it is terminated.
One way to counter this is to pass tes as a parameter, and then return* it using echo.
tes=2
testfunction(){
echo $1
}
val=$(testfunction $tes)
echo $tes
echo $val
You can also use the return command although i would advise against this as it is supposed to be used to for return codes, and as such only ranges from 0-255.Anything outside of that range will become 0
To return a string do the same thing
tes="i am a string"
testfunction(){
echo "$1 from in the function"
}
val=$(testfunction "$tes")
echo $tes
echo $val
Output
i am a string
i am a string from in the function
*Doesnt really return it, it just sends it to STDOUT in the subshell which is then assigned to val

Resources