Linux 如何从命令行中的脚本运行函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8818119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a function from a script in command line?
提问by AAaa
I have a script that has some functions.
我有一个具有某些功能的脚本。
Can I run one of the function directly from command line?
我可以直接从命令行运行其中一个函数吗?
Something like this?
像这样的东西?
myScript.sh func()
采纳答案by Sven Marnach
If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the source
or .
command and then simply call the function. See help source
for more information.
如果脚本只定义函数而不执行其他任何操作,您可以首先使用source
or.
命令在当前 shell 的上下文中执行脚本,然后简单地调用该函数。有关help source
更多信息,请参阅。
回答by TimonWang
The following command first registers the function in the context, then calls it:
以下命令首先在上下文中注册该函数,然后调用它:
. ./myScript.sh && function_name
回答by sorpigal
Briefly, no.
简而言之,没有。
You can import all of the functions in the script into your environment with source
(help source
for details), which will then allow you to call them. This also has the effect of executing the script, so take care.
您可以使用source
(help source
了解详细信息) 将脚本中的所有函数导入到您的环境中,然后您就可以调用它们。这也有执行脚本的作用,所以要小心。
There is no way to call a function from a shell script as if it were a shared library.
无法从 shell 脚本调用函数,就像它是共享库一样。
回答by sdaau
Well, while the other answers are right - you can certainly do something else: if you have access to the bash script, you can modify it, and simply place at the end the special parameter "$@"
- which will expand to the arguments of the command line you specify, and since it's "alone" the shell will try to call them verbatim; and here you could specify the function name as the first argument. Example:
好吧,虽然其他答案是正确的 - 您当然可以做其他事情:如果您可以访问 bash 脚本,则可以对其进行修改,只需将特殊参数放在末尾即可"$@"
- 它将扩展为命令行的参数您指定,并且由于它是“单独的”,shell 将尝试逐字调用它们;在这里你可以指定函数名作为第一个参数。例子:
$ cat test.sh
testA() {
echo "TEST A ";
}
testB() {
echo "TEST B ";
}
"$@"
$ bash test.sh
$ bash test.sh testA
TEST A
$ bash test.sh testA arg1 arg2
TEST A arg1
$ bash test.sh testB arg1 arg2
TEST B arg2
For polish, you can first verify that the command exists and is a function:
对于波兰语,您可以先验证该命令是否存在并且是一个函数:
# Check if the function exists (bash specific)
if declare -f "" > /dev/null
then
# call arguments verbatim
"$@"
else
# Show a helpful error
echo "'' is not a known function name" >&2
exit 1
fi
回答by Arturas M
Edit: WARNING - seems this doesn't work in all cases, but works well on many public scripts.
编辑:警告 - 似乎这并不适用于所有情况,但适用于许多公共脚本。
If you have a bash script called "control" and inside it you have a function called "build":
如果您有一个名为“control”的 bash 脚本,并且在其中有一个名为“build”的函数:
function build() {
...
}
Then you can call it like this (from the directory where it is):
然后你可以这样调用它(从它所在的目录):
./control build
If it's inside another folder, that would make it:
如果它在另一个文件夹中,那就是:
another_folder/control build
If your file is called "control.sh", that would accordingly make the function callable like this:
如果您的文件名为“control.sh”,则相应地使函数可调用,如下所示:
./control.sh build
回答by vchrizz
I have a situation where I need a function from bash script which must not be executed before (e.g. by source
) and the problem with @$
is that myScript.sh is then run twice, it seems... So I've come up with the idea to get the function out with sed:
我有一种情况,我需要一个来自 bash 脚本的函数,该函数之前不能执行(例如 by source
),问题@$
是 myScript.sh 然后运行两次,似乎......所以我想出了这个主意使用 sed 获取函数:
sed -n "/^func ()/,/^}/p" myScript.sh
sed -n "/^func ()/,/^}/p" myScript.sh
And to execute it at the time I need it, I put it in a file and use source
:
为了在我需要的时候执行它,我把它放在一个文件中并使用source
:
sed -n "/^func ()/,/^}/p" myScript.sh > func.sh; source func.sh; rm func.sh
sed -n "/^func ()/,/^}/p" myScript.sh > func.sh; source func.sh; rm func.sh
回答by Irfan Khan
you can call function from command line argument like below
您可以从命令行参数调用函数,如下所示
function irfan() {
echo "Irfan khan"
date
hostname
}
function config() {
ifconfig
echo "hey"
}
once you function end put $1 to accept argument let say above code is saved in fun.sh to run function ./fun.sh irfan & ./fun.sh config
一旦你函数结束把 $1 接受参数让说上面的代码保存在 fun.sh 中运行函数 ./fun.sh irfan & ./fun.sh config
回答by Ivan
Using case
使用 case
#!/bin/bash
fun1 () {
echo "run function1"
[[ "$@" ]] && echo "options: $@"
}
fun2 () {
echo "run function2"
[[ "$@" ]] && echo "options: $@"
}
case in
fun1) "$@"; exit;;
fun2) "$@"; exit;;
esac
fun1
fun2
This script will run functions fun1 and fun2 but if you start it with option fun1 or fun2 it'll only run given function with args(if provided) and exit. Usage
该脚本将运行函数 fun1 和 fun2,但如果您使用选项 fun1 或 fun2 启动它,它将仅运行带有 args(如果提供)的给定函数并退出。用法
$ ./test
run function1
run function2
$ ./test fun2 a b c
run function2
options: a b c