Pass function name as argument when running bash script - bash

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

Related

Exceuting function in Shell file from command

In my shellFile.sh I wrote this function:
#!/bin/bash
myFunction(){
echo 1
}
I tried to call the function from the command line this way:
shellFile.sh
shellFile.sh myFunction
After this two lines there is no output and no errors. I apologize if this question is asked before but I couldn't find it.
you can call your function inside your script like this
#!/bin/bash
myFunction(){
echo 1
}
myFunction
If you want to use the function interactively you can source the script, then call the function from the command line.
$ source shellFile.sh
$ myFunction
1
If you want the function to be available from the command line, you need to create the function in the current shell, using the dot . command, or in Bash its workalike source:
$ . shellFile.sh
$ myFunction
1
$
I suggest:
#!/bin/bash
myFunction(){
echo 1
}
$1
Usage: ./shellFile.sh myFunction
Output:
1
You need to call the function in the script:
#!/bin/bash
myFunction() {
echo 1
}
myFunction

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.

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"

bash FUNCNAME value expanding

I have one lib-file. It has one wrapper-like substitution of ponOS function. I want to display in function ponOS name of function where it is called from.
$> cat ./parasha_lib.sh
#!/bin/bash
function ponOS {
echo "$1: hello from ponOS"
}
ponOS='ponOS ${FUNCNAME}'
But, what I see is that this solution did not work as well as I want.
$> cat ./test.sh
#!/bin/bash
source ./parasha_lib.sh
function main {
echo "message from ${FUNCNAME}"
ponOS
}
main
So, I've got
$> ./test.sh
message from main
: hello from ponOS
But I wanna get this:
$> ./test.sh
message from main
main: hello from ponOS
What should I do?
One of the important things here is that ./test.sh shouldn't be modified (ponOS ${FUNCNAME} it's not the solution).
To do that, you need to set ponOS as an alias so that ${FUNCNAME} is expanded when called within the main function and not when the script is sourced.
Modify ./parasha_lib.sh to:
#!/bin/bash
function ponOS {
echo "$1: hello from ponOS"
}
shopt -s expand_aliases # enable alias expansion
alias ponOS='ponOS ${FUNCNAME}' # create an alias to call ponOS
Note that shopt -s expand_aliases is required to enable alias expansion. From the bash manpage:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
Now, without changing test.sh we get the desired output:
[me#home]$ ./test.sh
message from main
main: hello from ponOS
See FUNCNAME at http://www.gnu.org/software/bash/manual/bash.html#Bash-Variables
parasha_lib.sh
#!/bin/bash
function ponOS {
echo "${FUNCNAME[1]}: hello from ponOS"
}
test.sh
#!/bin/bash
source ./parasha_lib.sh
function main {
echo "message from ${FUNCNAME}"
ponOS
}
main
Result ./test.sh
message from main
main: hello from ponOS

Bash - How to call a function declared in a parent shell?

I am writing a bash script that calls functions declared in the parent shell, but it doesn't work.
For example:
$ function myfunc() { echo "Here in myfunc" ; }
$ myfunc
Here in myfunc
$ cat test.sh
#! /bin/bash
echo "Here in the script"
myfunc
$ ./test.sh
Here in the script
./test.sh: line 4: myfunc: command not found
$ myfunc
Here in myfunc
As you can see the script ./test.sh is unable to call the function myfunc, is there some way to make that function visible to the script?
Try
$ export -f myfunc
in the parent shell, to export the function.
#OP, normally you would put your function that every script uses in a file, then you source it in your script. example, save
function myfunc() { echo "Here in myfunc" ; }
in a file called /path/library. Then in your script, source it like this:
#!/bin/bash
. /path/library
myfunc
This also works but I noticed ${0} takes parent's value:
Maybe more useful if you don't want to have a bunch of export calls in your scripts.
script1:
#!/bin/bash
func()
{
echo func "${1}"
}
func "1"
$(. ./script2)
script2:
#!/bin/bash
func "2"
Output:
[mymachine]# ./script1
func 1
func 2

Resources