使用gets() 给出“没有这样的文件或目录";将参数传递给脚本时出错 [英] Using gets() gives "No such file or directory" error when I pass arguments to my script

查看:42
本文介绍了使用gets() 给出“没有这样的文件或目录";将参数传递给脚本时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习一个简单的 ruby​​ 脚本,我使用 gets.chomp 和参数制作表单,问题是当 gets.chomp 使用脚本返回当我应用参数 test 时出现错误.

代码:

#!usr/bin/ruby定义公式(奎恩)而 (1)打印[+] 字:"字 = 获取.chomp打印 quien + " -> " + word结尾结尾奎恩 = ARGV[0]公式(奎恩)

错误:

[+] Word : C:/Users/test/test.rb:8:in `gets': 没有那个文件或目录@rb_sysopen - test (Errno::E没有)来自 C:/Users/test/test.rb:8:in `gets'来自 C:/Users/test/test.rb:8:in `formulario'来自 C:/Users/test/test.rb:17:in `<main>'

有人可以帮忙吗?

解决方案

看起来您希望用户通过从 STDIN 读取一行来输入一些输入,最好的方法是通过调用 STDIN.gets 而不是 gets.所以你的线路变成:

word = STDIN.gets.chomp

这被记录为IO.gets.STDINIO 的一个实例.

现在,您正在执行 Kernel.gets,它做了一些不同的事情(强调我的):

<块引用>

ARGV 中的文件列表(或$*)或从标准输入返回(并分配给$_)下一行,如果没有文件存在在命令行上.

如果 ARGV 为空,这 似乎 的行为类似于 STDIN.gets,但不是同一件事,因此会造成混淆.

Hi I am making a simple ruby script practiced where I make a form using gets.chomp and arguments, the problem is that when gets.chomp use the script returns me an error when I apply the argument test.

The code:

#!usr/bin/ruby

def formulario(quien)
    while (1)
        print "[+] Word : "
        word = gets.chomp
        print quien + " -> " + word
    end
end

quien = ARGV[0]

formulario(quien)

The error:

[+] Word : C:/Users/test/test.rb:8:in `gets': No such file or directory @ rb_sysopen - test (Errno::E
NOENT)
        from C:/Users/test/test.rb:8:in `gets'
        from C:/Users/test/test.rb:8:in `formulario'
        from C:/Users/test/test.rb:17:in `<main>'

Can anyone help?

解决方案

It looks like you want to the user to type some input by reading a line from STDIN, the best way to do this is by calling STDIN.gets and not gets. So your line becomes:

word = STDIN.gets.chomp

This is documented as IO.gets. STDIN is an instance of IO.

Right now, you're executing Kernel.gets, which does something different (emphasis mine):

Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line.

This appears to behave like STDIN.gets if ARGV is empty, but is not the same thing, hence the confusion.

这篇关于使用gets() 给出“没有这样的文件或目录";将参数传递给脚本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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