2
\$\begingroup\$

The mission is to implement a cat-like program (copy all STDIN to STDOUT).

Rules:

  • You may only use standard libraries
  • Indentation must be either two spaces or a single tab (in languages which require indentation)
  • Scripts must use shebangs
  • The result of cat anything.txt | ./yourprogram | diff anything.txt - should be nothing and should not be an infinite loop

Go example (84 bytes)

package main
import (
  "os"
  "io"
)
func main() {
  io.Copy(os.Stdout,os.Stdin)
} 

C++ example (78 bytes)

#include<iostream>
using namespace std;
int main() {
  cout << cin.rdbuf();
}

Ruby example (44 bytes)

#!/usr/bin/env ruby
$stdout << $stdin.read

Shortest code (by bytes) wins.

\$\endgroup\$
  • 55
    \$\begingroup\$ This indentation thing is nonsense \$\endgroup\$ – edc65 Jun 15 '14 at 12:49
  • 14
    \$\begingroup\$ The indentation rule doesn't make sense, the shebang one makes even less, because shebangs can be platform-specific, especially in the case of Python. \$\endgroup\$ – nyuszika7h Jun 15 '14 at 14:50
  • 9
    \$\begingroup\$ until end of June 2914… Woaah… \$\endgroup\$ – Qeole Jun 15 '14 at 18:03
  • 7
    \$\begingroup\$ Also, person with most implementation? What? \$\endgroup\$ – nyuszika7h Jun 15 '14 at 18:50
  • 4
    \$\begingroup\$ what about this python solution? for s in' /\___/\|( o o )|/ * \|\__\_/__/meow| / \| / ___ \| \/___\/'.split('|'):print s \$\endgroup\$ – Willem Jul 4 '14 at 15:50

37 Answers 37

1
\$\begingroup\$

Simplex v.0.7

bg
b  ~~ take string input
 g ~~ output strip

Simple enough.

\$\endgroup\$
  • \$\begingroup\$ I need an "output all" character now. c: \$\endgroup\$ – Addison Crump Oct 27 '15 at 17:03
1
\$\begingroup\$

TeaScript, 1 byte

x

Input is given in the first input field. Compile online here.

\$\endgroup\$
0
\$\begingroup\$

Shell script, 11 bytes (including shebang)

#!/bin/cat
\$\endgroup\$
  • \$\begingroup\$ XD somehow my cat was on /usr/bin/cat XD \$\endgroup\$ – Kokizzu Jun 26 '14 at 10:15
  • \$\begingroup\$ This isn't technically a shell script, it's a cat script. The shell isn't involved for executables with a shebang, unless the shebang happens to point to the shell. \$\endgroup\$ – nyuszika7h Jul 2 '14 at 18:20
  • \$\begingroup\$ @nyuszika7h Arguably it's a shell script that's just using cat as the shell. \$\endgroup\$ – fluffy Jul 3 '14 at 5:06
  • \$\begingroup\$ But this is not a cat program. This is a quine. \$\endgroup\$ – jimmy23013 Jul 4 '14 at 8:18
  • \$\begingroup\$ @user23013 Oh, so it is. \$\endgroup\$ – fluffy Jul 5 '14 at 5:46
0
\$\begingroup\$

Hassium, 52 Bytes

use IO;func main(){println(File.readText(args[0]));}

Expanded:

use IO;

func main () {
    println(File.readText(args[0]));
}
\$\endgroup\$
-1
\$\begingroup\$

grep - 2 or 7 bytes

grep ''

Tested on Linux.

The "program" of '' is two bytes but the complete command is 7 bytes.

\$\endgroup\$
  • 2
    \$\begingroup\$ Actually, '' as a grep program won't work (try printf "#!/bin/grep\n''" > prog; chmod +x prog; ./prog). In your example, '' is just shell quote syntax. I can't see a way to run this with grep as interpreter, so this should count as a 7-byte shell script. \$\endgroup\$ – nyuszika7h Jul 2 '14 at 18:14
  • \$\begingroup\$ '' as a program will work from the interactive shell: "seq 100 | grep '' | md5sum" produces the same result as "seq 100 | md5sum" (ignore the double quotes) \$\endgroup\$ – Ken A Jul 2 '14 at 18:52
-2
\$\begingroup\$

Many languages (49)

print("\\    /\\\n )  ( \')\n(  /  )\n \\(__)|");

Because :3 is more like a tame lion.

\$\endgroup\$
  • 1
    \$\begingroup\$ That's not what the question is asking for, although it sounds like it :) \$\endgroup\$ – aditsu Jun 25 '14 at 9:22
  • \$\begingroup\$ I couldn't resist! \$\endgroup\$ – Mardoxx Jun 26 '14 at 12:16
-2
\$\begingroup\$

Ruby, 21 characters

#!/usr/bin/env ruby
while 1;puts gets;end
\$\endgroup\$
  • 1
    \$\begingroup\$ but it won't stop '___' \$\endgroup\$ – Kokizzu Jul 1 '14 at 2:15
  • \$\begingroup\$ testing command: cat anything.txt | ./cat.rb \$\endgroup\$ – Kokizzu Jul 1 '14 at 2:16
  • \$\begingroup\$ neither will cat \$\endgroup\$ – 190n Jul 1 '14 at 21:23
  • 1
    \$\begingroup\$ @fogcityben For me cat stops looping when it reads EOF. What version of cat are you working from? \$\endgroup\$ – Anko Sep 15 '14 at 9:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.