1

I just made a program that looks like cowsay. https://www.opendesktop.org/p/1271477/

This takes arguments, but I can't redirect stdouts to this program. What I want to do is like:

cat a_file.txt | cowspeak

Or

echo "Hello" | cowspeak

I have seen "lolcat" supports this (using trollop, I believe). I want to write it from scratch. How do I do that just in a single file?

4
  • Your question is unclear. What do you mean by "I can't redirect stdouts to this program"? The code you posted there should work, that is exactly how you redirect stdout in a Unix-like shell. If that doesn't work, then maybe your shell doesn't support pipes or uses a different syntax for them? Commented Dec 1, 2018 at 8:41
  • By the way, in a Unix-like shell, your first snippet is an example of the "Useless use of cat" anti-pattern, it should be cowspeak < a_file.txt instead. Commented Dec 1, 2018 at 8:42
  • Since I am using pipes, and using Linux syntax, it is clear that the question is asked for Linux systems. Also, the link will led you to a debian file, which is clearly, a Linux distribution. However, saying I can't redirect outputs mean that I am unable to redirect the output in a prior to emaillenin's answer. Also, now I am stuck with gets. If I use gets, then it will work if I am using pipes else, the it will wait for the input of the users. I want to run the program without any pause. If the user uses a pipe, then it will grab it, otherwise it will ignore the gets. Any idea? Commented Dec 1, 2018 at 9:41
  • What do you mean by "using Linux syntax"? Linux is an Operating System, not a programming language; Linux doesn't have a "syntax". If you are using, say, a bash shell or any other Unix-like shell (or even Microsoft PowerShell), then the code you posted should work and should redirect stdout to cowspeak. If it doesn't, then there is something wrong with your shell. Commented Dec 1, 2018 at 12:07

1 Answer 1

2

Use gets to receive the input from STDOUT via pipes.

Create a file cowspeak.rb:

puts "Cow speaks: " + gets

Demo:

❯ echo "hey there" | ruby cowspeak.rb
Cow speaks: hey there
Sign up to request clarification or add additional context in comments.

4 Comments

That's amazing, never thought of this before... So, is there any way to detect if any pipe has passed to the program, otherwise it would just wait for the stdin if no pipe is used...
Simply putting: If cowspeak.rb is run, it won't do nothing, and will not ask user for input. But if an STDOUT/STDERR is redirected, it will show that. Is there any way to do that?
Right, so here's what I did: ` #!/usr/bin/ruby -w require 'timeout' begin Timeout::timeout 0.00001 do p $stdin.gets end rescue Timeout::Error end `
Using a timeout is a bad idea because it's a race condition. Canonical Unix behavior is to read from stdin if no argument is provided. You can also check whether stdin is a tty with Ruby's isattyequivalent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.