如何默认Ruby的[Yn]响应;“清洁宝石”;指挥部?

如何默认Ruby的[Yn]响应;“清洁宝石”;指挥部?,ruby,automation,gem,default,prompt,Ruby,Automation,Gem,Default,Prompt,我经常使用Rubygemclean命令来保持本地gem存储库的状态 但是,由于依赖关系问题,该命令多次返回提示,例如: XXXXX-1.0.6 depends on [YYYYYY (~> 0.8.4)] If you remove this gems, one or more dependencies will not be met. Continue with Uninstall? [Yn] 虽然这很简单,但它需要手动干预(对于[Yn]响应),因此这会阻止我创建一个简单的cron

我经常使用Ruby
gemclean
命令来保持本地gem存储库的状态

但是,由于依赖关系问题,该命令多次返回提示,例如:

XXXXX-1.0.6 depends on [YYYYYY (~> 0.8.4)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  
虽然这很简单,但它需要手动干预(对于
[Yn]
响应),因此这会阻止我创建一个简单的
cron
脚本来自动化此过程

关于如何默认这些
gem
提示的响应,您有什么想法吗?

这应该可以:

echo|gem clean

这就像在提示下按回车键一样作为默认值,它将运行
gemclean
直到完成。

Thilo的答案是最简单的。然而,如果你需要使用“n”,你就不走运了。一些搜索发现此线程带有一些示例Java代码:

我针对这种情况对它进行了一些修改,我认为这将允许您使用“n”作为默认选项的更保守的选项。当然,如果您不希望在cron作业中运行java类,可以将这段代码修改为bash脚本,但我将把这留给更有bash经验的人

我现在没有一种简单的方法来测试这段代码,所以请告诉我它的运行情况。:)


您应该有一个
yes
命令,OSX版本的命令如下:

YES(1)BSD通用命令手册YES(1)
名称
是的,重复肯定
提要
是[咒骂]
描述
“是”输出咒语,或默认情况下永远输出“y”。
历史
“是”命令出现在4.0BSD中。
第四次伯克利发行1993年6月6日第四次伯克利发行
因此,也许这会起作用:

yes n | gem clean
gem clean
可能直接从终端读取,而不是从标准输入读取。在这种情况下,您可能需要
expect
四处走动:

Expect是一个根据脚本与其他交互式程序“对话”的程序。按照脚本,Expect知道可以从程序中得到什么以及正确的响应应该是什么。解释语言提供分支和高级控制结构来指导对话。此外,用户可以在需要时直接控制和交互,然后将控制权返回到脚本


因此,您可以编写一个
expect
脚本,根据需要使用“y”或“n”响应预期提示。

最新版本的
gem
具有相应的标志。例如,要忽略依赖项,请使用
-I
。要自动删除可执行文件,请使用
-x
。大多数选项相当于对特定类别的提示回答
yes
。在扩展选项名称前面加上一个
no-
,回答“否”而不是“是”

Options:
    -a, --[no-]all                   Uninstall all matching versions
    -I, --[no-]ignore-dependencies   Ignore dependency requirements while
                                     uninstalling
    -D, --[no-]check-development     Check development dependencies while uninstalling
                                     (default: false)
    -x, --[no-]executables           Uninstall applicable executables without
                                     confirmation
    -i, --install-dir DIR            Directory to uninstall gem from
    -n, --bindir DIR                 Directory to remove binaries from
        --[no-]user-install          Uninstall from user's home directory
                                     in addition to GEM_HOME.
        --[no-]format-executable     Assume executable names match Ruby's prefix and suffix.
        --[no-]force                 Uninstall all versions of the named gems
                                     ignoring dependencies
        --[no-]abort-on-dependent    Prevent uninstalling gems that are
                                     depended on by other gems.
    -v, --version VERSION            Specify version of gem to uninstall
        --platform PLATFORM          Specify the platform of gem to uninstall
        --vendor                     Uninstall gem from the vendor directory.
                                     Only for use by gem repackagers.

例如,要清除所有gem,您可以使用
gem uninstall-Iax

我刚刚测试了这个,但它似乎没有达到预期效果。在我的环境中,在这些情况下,我为gem clean输入一个“n”。所以,我跑了。。。“echo'n'| gem clean”并将其视为一个y并清除了所有的宝石。你是对的,但是,我猜它仍然会起作用。编辑以减少误导。
Options:
    -a, --[no-]all                   Uninstall all matching versions
    -I, --[no-]ignore-dependencies   Ignore dependency requirements while
                                     uninstalling
    -D, --[no-]check-development     Check development dependencies while uninstalling
                                     (default: false)
    -x, --[no-]executables           Uninstall applicable executables without
                                     confirmation
    -i, --install-dir DIR            Directory to uninstall gem from
    -n, --bindir DIR                 Directory to remove binaries from
        --[no-]user-install          Uninstall from user's home directory
                                     in addition to GEM_HOME.
        --[no-]format-executable     Assume executable names match Ruby's prefix and suffix.
        --[no-]force                 Uninstall all versions of the named gems
                                     ignoring dependencies
        --[no-]abort-on-dependent    Prevent uninstalling gems that are
                                     depended on by other gems.
    -v, --version VERSION            Specify version of gem to uninstall
        --platform PLATFORM          Specify the platform of gem to uninstall
        --vendor                     Uninstall gem from the vendor directory.
                                     Only for use by gem repackagers.