[英]Bash Script: how to pass arguments to a function of a running script from another one?
I am trying to call a function of a running script from another, and then pass to it an argument (in runtime) 我试图从另一个调用正在运行的脚本的函数,然后将参数传递给它(在运行时)
// Original script //原始脚本
#!/bin/bash
VALUE_OF_EXIT_CODE=256
/bin/bash /home/dev/runningScript.sh arg1 arg2
# some code
doSomething ($VALUE_OF_EXIT_CODE)
// running script //运行脚本
#!/bin/bash
function doSomething(){
EXIT_CODE=$1
}
while(//something)
do
if [ "$EXIT_CODE" == 0 ]; then
echo "normal exit => OK"
exit 0
fi
# If the exit code is not 0, then there was a error.
if [ "$EXIT_CODE" > 0 ]; then
echo "Error => $EXIT_CODE"
exit $EXIT_CODE
fi
done
I want that the exit_code that I set in the original script, pass to the running script so this can evaluate it, and continue doing whatever has to do or EXIT. 我希望在原始脚本中设置的exit_code传递给正在运行的脚本,以便它可以对其进行评估,然后继续执行必须做的事情或退出。
Bash does not work that way. Bash不能那样工作。 Here is what you should do:
这是您应该做的:
pidof
to get its pid. pidof
来获取其pid。 Yet another method would be to create a named pipe using mkfifo
. 还有一种方法是使用
mkfifo
创建命名管道。 Your running script should read from the pipe as its STDIN while your "another" script could write to it as its STDOUT. 您正在运行的脚本应该以管道的STDIN形式从管道中读取,而您的“另一个”脚本可以以管道的STDOUT形式对其进行写入。
There may be other ways of passing information between two different running bash scripts but multi-threaded programming in bash is still a long way to go. 在两个不同的正在运行的bash脚本之间传递信息可能还有其他方法,但是bash中的多线程编程仍然任重道远。
This is the wrong way round, in my opinion. 我认为这是错误的方法。 It would be easier to make what you call the "running script" start the "original script", instead of vice-versa.
使所谓的“运行脚本”启动“原始脚本”会更容易,而不是相反。
Then you can read the exit code with $?
然后,您可以使用
$?
读取退出代码$?
, and call a function with it: ,并使用它调用一个函数:
myFunction $?
For this kind of functionality, every language relies on interprocess messaging . 对于这种功能, 每种语言都依赖进程间消息传递 。 In bash it always is a bit hackish because it is not "cooked&ready".
在bash中,它总是有点骇人听闻,因为它不是“煮熟的”。
I tried to change as little as possible to your code (did fix some syntax though). 我试图对您的代码进行尽可能少的更改(尽管已修复了某些语法)。
You have the possibility now to choose your IPC mechanism: file or named pipe on ram or disk. 您现在可以选择IPC机制:RAM或磁盘上的文件或命名管道。 sockets are also possible and even the inotify kerneldaemon could be used.
套接字也是可能的,甚至可以使用inotify kerneldaemon。
This one works with a named pipe in tmpfs and with a signal . 这与tmpfs中的命名管道和信号一起使用。
// Original script //原始脚本
#!/bin/bash
mkfifo "/tmp/$$" ; exec 3<>"/tmp/$$"
doSomething() { echo "$1" >&3 ; pkill -SIGUSR1 "runningScript.sh" ;}
VALUE_OF_EXIT_CODE=256
/home/dev/runningScript.sh $arg1 $arg2 &
# some code
doSomething "$VALUE_OF_EXIT_CODE"
// running script //运行脚本
#!/bin/bash
trap "read EXIT_CODE <&3" SIGUSER1
while something
do
if [ $EXIT_CODE = 0 ]; then
echo "normal exit => OK"
exit 0
fi
# If the exit code is not 0, then there was a error.
if [ $EXIT_CODE -gt 0 ]; then
echo "Error => $EXIT_CODE"
exit $EXIT_CODE
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.