Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用<;读取ruby中的输入文件;在命令行中?_Ruby_Input_Terminal_Command_Output - Fatal编程技术网

如何使用<;读取ruby中的输入文件;在命令行中?

如何使用<;读取ruby中的输入文件;在命令行中?,ruby,input,terminal,command,output,Ruby,Input,Terminal,Command,Output,我想让它起作用: ./search.sh < inputFile.json /search.sh ~$ ./search.sh foo.txt > bar.txt 你有多种选择 从标准文本中读取 一个选项是从标准输入读取。例如,您可以在search.sh中执行 #!/usr/bin/env ruby input = $stdin.read puts "here's the input i got:" puts input 假设我们有一个文件foo.txt,如下所示 foo

我想让它起作用:

./search.sh < inputFile.json
/search.sh

/search.sh
这将根据文件的不同输出不同的内容。我想这样做:

(./search.sh < inputFile.json) > results.json
(./search.shresults.json

我确信语法是错误的。有人能帮我找到正确的方向吗?我在我的ruby脚本中找不到如何做到这一点(我使用的是.sh,但它是ruby)。

我假设输入文件的内容将不同,您将有一些逻辑来确定这一点。实际上,您可以读取该文件输入,就好像它是由用户作为文本输入的一样,并对其执行任何需要执行的操作

例如:

测试.rb

puts gets.chomp
testfile

test
终端

$ ruby test.rb < testfile
$ test
$ruby test.rb
我假设输入文件的内容将不同,您将有一些逻辑来确定这一点。实际上,您可以读取该文件输入,就好像它是由用户作为文本输入的一样,并对其执行任何需要执行的操作

例如:

测试.rb

puts gets.chomp
testfile

test
终端

$ ruby test.rb < testfile
$ test
$ruby test.rb
您有多种选择

从标准文本中读取 一个选项是从标准输入读取。例如,您可以在
search.sh中执行

#!/usr/bin/env ruby

input = $stdin.read

puts "here's the input i got:"
puts input
假设我们有一个文件
foo.txt
,如下所示

foo
bar
baz
然后可以将其与unix管道一起使用

~$ ./search.sh < foo.txt
here's the input i got:
foo
bar
baz
虽然这是对cat的无用使用,只是为了达到演示的目的。您不仅可以使用管道文件,还可以使用其他命令进行输出

~$ echo "hello, world!" | ./search.sh
here's the input i got:
hello, world!
如果要将输出重定向到另一个文件,请执行以下操作

~$ ./search.sh < foo.txt > bar.txt
~$ cat bar.txt
here's the input i got:
foo
bar
baz
用法:

~$ ./search.sh foo.txt
here's the input i got:
asfdg
sdf
sda
f
sdfg
fsd
再次使用
>

~$ ./search.sh foo.txt > bar.txt

你有多种选择

从标准文本中读取 一个选项是从标准输入读取。例如,您可以在
search.sh中执行

#!/usr/bin/env ruby

input = $stdin.read

puts "here's the input i got:"
puts input
假设我们有一个文件
foo.txt
,如下所示

foo
bar
baz
然后可以将其与unix管道一起使用

~$ ./search.sh < foo.txt
here's the input i got:
foo
bar
baz
虽然这是对cat的无用使用,只是为了达到演示的目的。您不仅可以使用管道文件,还可以使用其他命令进行输出

~$ echo "hello, world!" | ./search.sh
here's the input i got:
hello, world!
如果要将输出重定向到另一个文件,请执行以下操作

~$ ./search.sh < foo.txt > bar.txt
~$ cat bar.txt
here's the input i got:
foo
bar
baz
用法:

~$ ./search.sh foo.txt
here's the input i got:
asfdg
sdf
sda
f
sdfg
fsd
再次使用
>

~$ ./search.sh foo.txt > bar.txt

这很有道理。我正在使用gets.chomp,但是没有意识到如果你给它一个文件,它会立即完成gets.chomp。(通常我在提示用户输入时使用gets.chomp。非常感谢这帮了大忙!@p11y chomp不是必需的,取决于您想对输入做什么,我只是想表明它可以用作通常输入到程序中的文本。这很有意义。我使用gets.chomp,但没有意识到您是否输入了它一个文件,它将立即完成gets.chomp。(通常我在提示用户输入时使用gets.chomp。非常感谢这帮了大忙!@p11y chomp不是必需的,取决于您想对输入做什么,我只是想表明它可以用作通常输入到程序中的文本。