bash echo wrapper issue - bash

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.

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

sourcing specific function in bash not working

I am trying to source specific function of a bash file.
Please find below simplified loadfun.sh :-
function a(){
echo "This is a"
}
function b(){
echo "This is b"
}
function load(){
echo "exporting $1"
export -f $1
}
$#
Also, please find below execution sequence of commands :-
$cat loadfun.sh
function a(){
echo "This is a"
}
function b(){
echo "This is b"
}
function load(){
echo "exporting $1"
export -f $1
}
$#
$
$
$
$sh loadfun.sh a
This is a
$
$
$a
bash: a: command not found
$
$
$sh loadfun.sh load a
exporting a
$
$
$
$a
bash: a: command not found
$
I am not sure why
export -f a
is not exporting function a.
If you only want to set specific functions whilst sourcing the file then you could use a case statement
case $1 in
a)
a(){
echo "This is a"
}
;;
b)
b(){
echo "This is b"
}
;;
*)
echo error message
;;
esac
And call the script with
. ./script [function to export]
Source your script rather than executing it, so that the definitions and the export take place in your current shell:
source loadfun.sh a
As an aside -- the .sh extension is misleading; since this uses bash-only functionality, it should be named loadfun.bash. (Using extensions is frowned on in general for executable scripts, but since this is intended to be loaded as a library, an extension is appropriate).

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.

How to execute bash function inside other function

I have this code:
myecho() {
if [ -z "$1" ]
then
echo "if";
else
echo "else";
fi
}
alias myecho=myecho
callmyecho() {
append="stuff";
myechoResult='myecho'; #`myecho` wont execute here!
echo $myechoResult$append
}
How I can execute myecho inside callmyecho and append a new text to it?
Editing your code as follows:
#!/bin/bash
myecho() {
if [ -z "$1" ]; then
echo "if"
else
echo "else"
fi
}
callmyecho() {
append="stuff"
myechoResult=`myecho`
echo $myechoResult$append
}
# callmyecho
If you want to call callmyecho within the file and have it execute the myecho function within its block, simply uncomment callmyecho by removing '#'.
On the other hand, if you want to call on the callmyecho function from the terminal, then source the file script first as follows:
$ source ./file
Then you can call on any function inside that file script and it will execute.

Bash Script Returning Value and Echo

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"

Resources