views:

1431

answers:

9
+4  A: 

I think you might have missed the joke. The point is that cat is not an editor; it's simply a way of getting the text into the file in one go, with no editing or correcting. You write it once, and that's it.

If you're getting at something else, can you be clearer?

Vicky
Vicky: I think the question is legitimate. It may not be clear how to enter text using cat ( it was not clear for me definitely ) so. I think yours would be a very good comment, but not so good answer.
OscarRyz
Exactly what Oscar said! It didn't know how to use cat that way. I've used it to output the content of a file but it never ocurred to me you could do what user:chaos explained.
Sparragus
When no filename is specified, cat reads from stdin, like most other UNIX programs.
Adam Rosenfield
OK - I didn't realise that it wasn't commonly known that you could use cat this way! Fair enough.
Vicky
+49  A: 
$ cat > hello.c
#include <stdio.h>

main(void) {
    printf("Hello, world.\n");
}
^D
$ gcc hello.c
$ ./a.out
Hello, world.
chaos
+1 hehehe. That's how!!. No fancy editing commands. ( like in ed ) BTW, I've never managed to use ed. I always wanted to, because vi took sooooo long to start from a remote terminal ( emacs was out of the question ).
OscarRyz
sed would be an interesting day-to-day editor :P
Aiden Bell
... because Real Programmers get it right first time.
marijne
I tend to use cat > to do the first pass at small test programs, because the standard Solaris vi is evil. The temptation with vi is to correct you mistakes, which is a mistakes because the cursor keys will get interpreted as escape sequences and create a mess.
Tom Hawtin - tackline
Surely a REAL programmer uses "cat > a.out"
Martin Beckett
@Tom Hawtin: Obviously you need to play more Rogue, to ingrain the hjkl movement keys into your nervous system.
chaos
@chaos: This is very true. NetHack + enabling vi(m) bindings in every app that I can means that hjkl is completely ingrained.
Bernard
I do this often.
xnine
+4  A: 

The joke is that you don't edit, you just use cat and perhaps (if you were going to be obtuse)... echo. For example:

echo "#stdio.h\n int main(int argc, char **argv){\nprintf(\"hello World\\n\");\nreturn(0);\n}" > helloworld.c

then

cat helloworld.c

repeat.

(also, I must be bored to go and type that out)

Aiden Bell
+1 for actually giving main its proper argument list and return value. That's dedication.
chaos
:P , well I was going to the trouble of escaping the quotes (and even the newline in the quotes ... so why not hehe
Aiden Bell
If we're going to get really technical about it, shouldn't it be char** argv or char* argv[]?
MatrixFrog
Ha, you're right. You win one internet.
chaos
I think you misread MatrixFrog :P
Aiden Bell
+34  A: 

Nah, echo is clearly better:

echo 'main(){puts("Hello world\n");}'  | gcc -xc -

Even if you want to use cat (felines are after all wonderful), why bother putting the source to disk? Just redirect cat's output to the compiler, as in the echo case.

Alex Martelli
@Alex I though it was classier too. Nice pipe usage.
Aiden Bell
And you have built-in version control via .bash_history!
dbr
Too bad bash can't execute an executable coming in from a pipe, otherwise you could do it without saving the output of the compiler with something like "echo ... | gcc -xc - -o - | execute-stdin-command"
Adam Rosenfield
@Alex: OK, that's just f#$%ing cool! I've actually used cat on a system to enter code before, but the echo directly to the compiler I wish I had thought of that!
NoMoreZealots
+2  A: 

There's a book available on the subject.

+2  A: 

Lets see how far we can take this.

the output to all of these will be:

Hello World
$ perl -E"$(cat)"
say 'Hello World'
^D

$ test=$(cat); perl -E"$test"
say 'Hello World'
^D

$ cat | perl -M5.010
say 'Hello World'
^D

$ cat > test; test=$(cat test); perl -E"$test"
say 'Hello World'
^D

$ cat > test; test=$(cat test)$(cat); perl -E"$test"
say
^D
'Hello World'
^D

$ cat > test; test=$(cat test); perl -E"$test$(cat)"
say
^D
'Hello World'
^D

$ cat > test; test=$(cat test); perl -E"$(cat)$test"
'Hello World'
^D
Brad Gilbert
+4  A: 

gcc? real programmers use cat and xxd:

$ cat | xxd -r > a.out
0000000: cefa edfe 0700 0000 0300 0000 0200 0000  ................
0000010: 0b00 0000 a803 0000 8500 0000 0100 0000  ................
0000020: 3800 0000 5f5f 5041 4745 5a45 524f 0000  8...__PAGEZERO..
0000030: 0000 0000 0000 0000 0010 0000 0000 0000  ................
<lot of other stuff>
0003090: 0a00 0000 0b00 0000 2000 6479 6c64 5f73  ........ .dyld_s
00030a0: 7475 625f 6269 6e64 696e 675f 6865 6c70  tub_binding_help
00030b0: 6572 005f 5f64 796c 645f 6675 6e63 5f6c  er.__dyld_func_l
00030c0: 6f6f 6b75 7000 6479 6c64 5f5f 6d61 6368  ookup.dyld__mach
00030d0: 5f68 6561 6465 7200 5f4e 5841 7267 6300  _header._NXArgc.
00030e0: 5f4e 5841 7267 7600 5f5f 5f70 726f 676e  _NXArgv.___progn
00030f0: 616d 6500 5f5f 6d68 5f65 7865 6375 7465  ame.__mh_execute
0003100: 5f68 6561 6465 7200 5f65 6e76 6972 6f6e  _header._environ
0003110: 005f 6d61 696e 0073 7461 7274 005f 6578  ._main.start._ex
0003120: 6974 005f 7075 7473 0000 0000            it._puts....
^D
$ chmod 755 ./a.out
$ ./a.out
Hello, world.

btw, I didn't know that 0xFEEDFACE was the magic number in Mach-O binaries... cool.

Stefano Borini
It's not (judging from your listing). It's 0xFEEDFACE (assuming the machine is little-endian).
Adrian Pronk
Right! On my PowerPC machine (big endian) this is the result 0000000: feed face 0000 0012 0000 0000 0000 0002 ................
Stefano Borini
I treated it as a middle endian... getting old apparently :(
Stefano Borini
A: 

See Wikipedia article on Real Programmers. You can find tutorials on the web. Best to know assembly first (know it very well).

"Real Programmers don't use IDEs, they write programs using cat > a.out" (that is, they write machine-readable binary files from beginning to end without making any mistakes).

Ben0mega
A: 

I've stolen the accepted and most voted answer and combined it with the echo answer, but I have added automatic conditional execution if compilation was success.

$ cat | gcc -xc - && ./a.out
#include <stdio.h>

main(void) {
    printf("Hello, world.\n");
}
^D
Hello, world.
James Morris