如何在 bash 中指定用户输入
How to specify user input in bash
我正在开发一个 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>'
<<<
将单个字符串传递给程序。
./fitgem.rb <<< "hello"
大致相当于
./fitgem.rb <<EOF
hello
EOF
另一种选择是管道
echo hello | ./fitgem.rb
tee
将其标准输入复制到一个或多个文件,以及复制到标准输出。
$ echo hello | tee one.txt
hello
$ cat one.txt
hello
我正在开发一个 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>'
<<<
将单个字符串传递给程序。
./fitgem.rb <<< "hello"
大致相当于
./fitgem.rb <<EOF
hello
EOF
另一种选择是管道
echo hello | ./fitgem.rb
tee
将其标准输入复制到一个或多个文件,以及复制到标准输出。
$ echo hello | tee one.txt
hello
$ cat one.txt
hello