我正在开发一个Ruby程序,我正在使用Travis CI自动测试。问题是我无法在每个gets.chomp
指定用户输入。我正在尝试使用tee
和echo
,如下所示。
juniorRubyist@juniorRubyist-laptop:~/Projects/fitgem$ ./fitgem.rb <<< tee "hello"
Welcome to FitGem!
To get started, I need some information.
Please open your default browser and navigate to https://juniorRubyist.github.io/fitgem/authorize.html
Then authorize your Fitbit account.
Copy your Access Token: ./fitgem.rb:17:in `gets': No such file or directory @ rb_sysopen - hello (Errno::ENOENT)
from ./fitgem.rb:17:in `gets'
from ./fitgem.rb:17:in `initialize'
from ./fitgem.rb:45:in `new'
from ./fitgem.rb:45:in `<main>'
问题在于它一直认为我在尝试指定文件名而不是字符串。我做错了什么或不支持。如果您想查看代码,可以在https://github.com/juniorRubyist/fitgem/blob/master/fitgem.rb找到代码。
编辑1: 我通过 chepner 尝试了这个答案。弹出另一个错误。
joseph@Joseph-Ubuntu:~/Projects/fitgem$ ./fitgem.rb <<< "hello"
Welcome to FitGem!
To get started, I need some information.
Please open your default browser and navigate to https://juniorRubyist.github.io/fitgem/authorize.html
Then authorize your Fitbit account.
Copy your Access Token: Copy your User ID: ./fitgem.rb:19:in `initialize': undefined method `chomp' for nil:NilClass (NoMethodError)
from ./fitgem.rb:45:in `new'
from ./fitgem.rb:45:in `<main>'
答案 0 :(得分:1)
<<<
将一个字符串传递给程序。
./fitgem.rb <<< "hello"
大致相当于
./fitgem.rb <<EOF
hello
EOF
另一种选择是管道
echo hello | ./fitgem.rb
tee
将其标准输入复制到一个或多个文件,以及tostandard输出。
$ echo hello | tee one.txt
hello
$ cat one.txt
hello