Output of Ruby Shell command differs from shell


jakeonrails

When I run this command from terminal:

$ grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb  | cut -d: -f1 | uniq

It returns a result:

spec/features/admins/change_payment_method_spec.rb

When I run this command (note that the InfoPage is now a TeamPage :

$ grep -rnw 'PageObjects::CompanySettings::TeamPage' spec/**/*_spec.rb  | cut -d: -f1 | uniq

I get a result:

spec/features/team_page_spec.rb

Now, when I go in irband invoke the shell command with backticks, I get the output of the second command (TeamPage) but not the output of the first command (InfoPage).

2.1.6 :001 > `grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb  | cut -d: -f1 | uniq`
""
2.1.6 :002 > # No result ^^
2.1.6 :003 >   `grep -rnw 'PageObjects::CompanySettings::TeamPage' spec/**/*_spec.rb  | cut -d: -f1 | uniq`
"spec/features/team_page_spec.rb\n"
2.1.6 :004 > # One result, as expected!

Can anyone help me figure out why this is happening?

yog noon

I noticed that in the first example, the file has multiple directories nested and you are using non-standard **wildcards.

My best guess is that your interactive shell supports that non-standard wildcard, but your batch shell doesn't.

A possible solution is to use findrather than rely on shell wildcards. Another possibility is to use Ruby, since you are already using Ruby anyway. (Note: grepvery optimized, so if you have a lot of very large files, Ruby may be slower.)

# Ruby's Dir class does support **
Dir['spec/**/*_spec.rb'].select {|filename|
  File.open(filename) {|file|
    file.each_line.any? {|line|
      line.inlcude?('PageObjects::CompanySettings::InfoPage')
    }
  }
}

[not tested]

Related


Output of Ruby Shell command differs from shell

jakeonrails When I run this command from terminal: $ grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb | cut -d: -f1 | uniq It returns a result: spec/features/admins/change_payment_method_spec.rb When I run this command (note that the Inf

Output of Ruby Shell command differs from shell

jakeonrails When I run this command from terminal: $ grep -rnw 'PageObjects::CompanySettings::InfoPage' spec/**/*_spec.rb | cut -d: -f1 | uniq It returns a result: spec/features/admins/change_payment_method_spec.rb When I run this command (note that the Inf

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

System R command differs from shell output

Priya I have ubuntu's default python and Anaconda Python installed in my system. When I run the system command in R: > system('which python') /usr/bin/python But from the shell: user@user:~/Documents/Rad/jee/Solver$ which python /home/user/anaconda2/bin/pytho

Shell - suppress output from a single command

CDT: I have a rm *.ocommand Makefilethat removes all object files generated during compilation. However, some error messages are output if some .ofiles do not exist . So how to suppress the error message output? Kaibu: In the context of make(more important tha

Shell - suppress output from a single command

CDT: I have a rm *.ocommand Makefilethat removes all object files generated during compilation. However, some error messages are output if some .ofiles do not exist . So how to suppress the error message output? Kaibu: In the context of make(more important tha

How to capture output from bourne shell command

Rajat Gupta I want to execute a bourne shell command, capture its stdout and its stderr (separately), and its exit code. User 6654769 You can use subprocess (part of stdlib): import subprocess cmd = input("enter your command: " ) result = subprocess.run(cmd,

read python from shell command output

high mark I understand you might think this is a duplicate question, but so far I haven't found a solution to my own problem. Currently, I have a C program that constantly produces a stream of data. I want my python program to have a way to read this data so I

Shell script: read input from command output

Rahul Why does the following code only give the first line of ps -eaf output in ps.out? while read line; do echo $line>ps.out; done < <(/bin/ps -eaf) Nicola You truncate the file every time, so you only get the last line. You might want to >>instead >.

Shell - suppress output from a single command

CDT: I have a rm *.ocommand Makefilethat removes all object files generated during compilation. However, some error messages are output if some .ofiles do not exist . So how to suppress the error message output? Kaibu: In the context of make(more important tha

Remove output from find command - Shell

cynic 77 I have a shell script that is installing a version of Tomcat on my system. I can print the found version stdoutusing that findcommand . However, I also get output for directories and files not found. How can I remove it from the output and only show t

read python from shell command output

high mark I understand you might think this is a duplicate question, but so far I haven't found a solution to my own problem. Currently, I have a C program that constantly produces a stream of data. I want my python program to have a way to read this data so I

Shell script: read input from command output

Rahul Why does the following code only give the first line of ps -eaf output in ps.out? while read line; do echo $line>ps.out; done < <(/bin/ps -eaf) Nicola You truncate the file every time, so you only get the last line. You might want to >>instead >.

Output Command Shell in if statement

Dasser Basyouni I've searched a lot and read a lot of code, but I don't understand why the variable (cmd output) can't be printed to make sure it's a duplicate question, but I can't comment on any of the code I see below 50 credit is me The code I'm trying, I'

Execute command on shell command output

Salt I have a command that will run forever and output something periodically until it is killed by something else (something like that tail -f), and I want to make it so that whenever there is new output, another command will be executed. Precautions: I can't

Execute command on shell command output

Salt I have a command that will run forever and output something periodically until it is killed by something else (something like that tail -f), and I want to make it so that whenever there is new output, another command will be executed. Precautions: I can't

The compressed output differs from the Go to Ruby implementation

Sam Genest: I'm implementing a program that breaks a file into git blobs and stores it appropriately. I have a ruby reference implementation based on a git book article I am trying to implement here However, I'm running into an issue where the compressed data

The compressed output differs from the Go to Ruby implementation

Sam Genest: I'm implementing a program that breaks a file into git blobs and stores it appropriately. I have a ruby reference implementation based on a git book article I am trying to implement here However, I'm running into an issue where the compressed data

The compressed output differs from the Go to Ruby implementation

Sam Genest: I'm implementing a program that breaks a file into git blobs and stores it appropriately. I have a ruby reference implementation based on a git book article I am trying to implement here However, I'm running into an issue where the compressed data

Redirect shell output to ruby script

rs41 I have simple ruby script: #! /usr/bin/env ruby require 'fileutils' FileUtils.rm "output.mkv" if File.exists?("outp ut.mkv") pid = Process.spawn("ffmpeg -i wrong_file.mp4 -c:v libx264 -preset veryslow -qp 0 output.mkv", STDOUT => "output.txt", STD

Redirect shell output to ruby script

rs41 I have simple ruby script: #! /usr/bin/env ruby require 'fileutils' FileUtils.rm "output.mkv" if File.exists?("outp ut.mkv") pid = Process.spawn("ffmpeg -i wrong_file.mp4 -c:v libx264 -preset veryslow -qp 0 output.mkv", STDOUT => "output.txt", STD