bash 是否可以从 ruby​​ 脚本返回值并在 c 或 shell 脚本中读取该值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28075541/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:13:31  来源:igfitidea点击:

Is it possible to return value from ruby script and read that value inside c or shell script?

rubybashshellubuntu

提问by krunal shah

How can we return value from ruby script?

我们如何从 ruby​​ 脚本返回值?

#!/usr/bin/env ruby

a = "test"
a

How can we access the value of 'a' in Ubuntu terminal or java or c?

我们如何在 Ubuntu 终端或 java 或 c 中访问“a”的值?

回答by philippe lhardy

print your variable within ruby/python script, it can then be read from a shell script by example :

在 ruby​​/python 脚本中打印变量,然后可以通过示例从 shell 脚本中读取它:

#!/bin/bash
ruby_var=$(ruby myrubyscript.rb)
python_var=$(python mypythonscript.py)

echo "$ruby_var"
echo "$python_var"

take care your ruby/python script only print this variable ( there are more complicated ways with named pipes by example for more interaction ).

请注意您的 ruby​​/python 脚本仅打印此变量(例如,有更复杂的命名管道方式以进行更多交互)。

回答by glenn Hymanman

If the value is an integer between 0 and 255, you can use the exit status

如果值为 0 到 255 之间的整数,则可以使用退出状态

$ ruby -e 'var=42; exit var'
$ val=$?
$ echo $val
42