巴什 - 从标到父脚本返回值 [英] Bash - Return value from subscript to parent script

查看:83
本文介绍了巴什 - 从标到父脚本返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的bash脚本。父脚本调用来执行某些操作并返回一个值的下标。我怎样才能从标到父脚本返回一个值?添加在标a 收益和追赶父值没有工作。

I have two Bash scripts. The parent scripts calls the subscript to perform some actions and return a value. How can I return a value from the subscript to the parent script? Adding a return in the subscript and catching the value in the parent did not work.

推荐答案

我假定这些脚本是在两个不同的进程中运行,即你是不是源他们中的一个。

I am assuming these scripts are running in two different processes, i.e. you are not "sourcing" one of them.

这取决于你想返回什么。如果你希望只对0和255之间,然后返回一个退出code:

It depends on what you want to return. If you wish only to return an exit code between 0 and 255 then:

# Child (for example: 'child_script')
exit 42

# Parent
child_script
retn_code=$?

如果您想返回文本字符串,那么你将不得不这样做,通过标准输出(或文件)。有捕获的几种方法,最简单的是:

If you wish to return a text string, then you will have to do that through stdout (or a file). There are several ways of capturing that, the simplest is:

# Child (for example: 'child_script')
echo "some text value"

# Parent
retn_value=$(child_script)

这篇关于巴什 - 从标到父脚本返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆