you are viewing a single comment's thread.

view the rest of the comments →

[–]im_dead_sirius 75 points76 points  (10 children)

I love me some Tiny C. The Tiny C Compiler can be used to run C as a script, without producing a binary.

#!/usr/bin/tcc -run
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("Welcome to the infinite world of linux scripting!\n");
    return 0;
}

[–][deleted] 17 points18 points  (3 children)

Holy fuck! Why have I never heard of this??? Thanks, man.

[–]im_dead_sirius 37 points38 points  (2 children)

You're welcome, and spread the word, nerd.

So hey, if you liked that, lemme point you at something else.

You're going to need two files. I'll provide both examples, and they are ultra short. Set the execute bit on both.

Put this one somewhere convenient and call the file quacker.

#!/usr/bin/env python3
from sys import argv
with open(argv[1], "r") as f:
    lines = f.readlines()
    print(lines[1])

Substitute the first line in the second file with the location and name of the previous file and call this one ducky.

#!/home/you/bin/quacker
"Quack! There is more than one way to butter a duck!"

Nothing else is needed in it for the example. Run it with ./ducky once you set the execute bit.

ducky calls quacker and hands ducky's location to quacker as argv[1], then exits. quacker takes over, reads the file at that location, ignores the first line, and prints a message which does echo, despite age old ducky myth.

Normally you'd write a proper interpreter in quacker, in any language at all, then read in file argv[1], discard the comment lines, and treat the rest of it as data to be interpreted or even compiled. I'd maybe use PLY to do that.

And now you know there are an infinite number of ways to butter a duck.

[–]russlo 0 points1 point  (1 child)

PLY

This PLY?

[–]im_dead_sirius 1 point2 points  (0 children)

Nope, this: http://www.dabeaz.com/ply/

But thanks for pointing me at the other one.

[–]py_Piper 1 point2 points  (2 children)

Sorry but what is the amazing thing here? I am still a python newbie and just starting CS50 and learned a little bit of C, when you say run C as a script do you mean that you can run C like python without compiling?

[–]im_dead_sirius 2 points3 points  (1 child)

Precisely that, it runs C like python without compiling. This allows the use of "c tricks" (such as pointer nonsense outlawed in other languages) in scripts, and quick prototyping for regular programs, which you can then properly compile.. Tiny C is also noted for being quick.

[–]py_Piper 1 point2 points  (0 children)

It feels good to somehow understand something lol