9

After using linux for a month or two, I know what I'm doing now.

When creating programs, using whatever language, I've obviously been using code like this:

$ python test.py

And so if I wanted test.py to read a given file, I would have to use:

$ python test.py something.file

What I'd like to do now, it try and create a command line application, so I can use

$ myapp something.file

A program like the python in $ python test.py, or the nano in $ nano program.pl

But where on earth do I start building applications like these? A bit of web trawling has got me nowhere.

If you can tell me yourself that would be great, but I'll readily accept a bunch of links.

I'm totally open if there's more than one way, I don't really mind what language (an excuse to learn another!) or whatever.

ACarter
  • 389
  • 1
  • 4
  • 11
  • 3
    Do you want to learn Unix C programming or do you just want to be able to run your python scripts without having to specify the `python` part? – jw013 Jul 14 '12 at 18:19
  • @jw013 I want to learn Unix C programming. (Though I would be interested to know how you could run python scripts without the `python`.) – ACarter Jul 14 '12 at 18:25
  • 4
    You should probably fix your question then :) CLI applications can be written in *any* language that has a Unix compiler or interpreter, so if you are only interested in C, say so. Unix C programming is actually two parts - the Unix part and the C part. As it is, "How do I learn Unix C programming?" will probably get closed for being too general, and there's probably duplicates all over this site. "How do I learn C programming" is also too general and possibly off-topic as well. It'd certainly get closed on SO. – jw013 Jul 14 '12 at 18:28
  • @jw013 What I'm really after is advice *like* "CLI applications can be written in any language that has a Unix compiler or interpreter", so thanks. – ACarter Jul 14 '12 at 18:59
  • 1
    Ah, ok. There's always a bit of a steep initial hump in the learning curve when trying to get into the Unix environment, but once you get past it I'm sure you'll find the effort was worth it. Unfortunately, I don't know of any one resource that I can point to for getting started - you basically have to pick up bits and pieces as you go :\ Best of luck to you on your journey :) – jw013 Jul 14 '12 at 19:08
  • @Acarter if you're just asking how to make the script executable without doing `python foo.py` that's fine (chmod and shebang ar in scope). But anything more programmatic than that needs to go to http://stackoverflow.com I considered closing this as offtopic. – xenoterracide Jul 16 '12 at 04:20
  • @xeno I'm not (realising this is really not clear) I'm asking how you write a program like the `nano` in `$ nano test.bar`. (Not how as in code, but how as in which languages you can use, how you set it up ect.) – ACarter Jul 16 '12 at 16:19
  • @ACarter like I said above, that question is much too general, and would likely get closed. The problem is programs are designed to *do* something. You can't ask "How do I program foo?" and leave out the "foo", because that is the most important part. The demands and requirements of the problem will inform and sometimes dictate your approach, including what language you pick. E.g.: `nano` is a text editor. If you want to write a text editor, you have to start by designing a text editor (mostly UI stuff, doesn't involve coding at all). (continued due to char limit ...) – jw013 Jul 17 '12 at 17:28
  • Once you have a design for the next greatest thing since `vim` and `emacs`, you'll have to get into the implementation details. You'll need to familiarize yourself with terminal emulators and tty's and the workings of termcap/terminfo and/or the curses libraries. All of this stuff is specific to writing a text editor. If you wanted to do something else, there'd be a whole different set of concerns. With that said, most Unix applications and libraries are written in C, so if you want to write CLI apps in the future, learning C won't be a waste of your time. – jw013 Jul 17 '12 at 17:34
  • Fair enough, I know what you mean. Can you flag this to be closed for me please? – ACarter Jul 18 '12 at 15:22

3 Answers3

15

You can run python scripts by making them executable (chmod +x test.py) and making #!/usr/bin/env python the first line. Once you do that, running test.py args will invoke python to run your script. Read about shebang's if you want to learn more.

jw013
  • 50,274
  • 9
  • 137
  • 141
  • That didn't work for me. `$ test.py 30` gave a bash error: `-bash: test.py: command not found`. Thanks for your help so far though! – ACarter Jul 14 '12 at 18:31
  • 8
    @ACarter That's because it's not in your `$PATH`. Either use a path, like `/path/to/test.py` or `./test.py` if you are in `/path/to` already, or add `/path/to` to your `$PATH`. There are many duplicate questions on this site about the latter. – jw013 Jul 14 '12 at 18:32
  • As soon as you put something in your `$PATH`, leave out file endings like `.py`. – Reactormonk Jul 15 '12 at 11:23
  • You need the `#!/usr/bin/env python` at the top of your script before you can remove the .py extension. But any script which contains this invocation or some similar one (e.g. `#!/bin/sh` for shell scripts) can be named without an extension. It has nothing to do with whether it's in your PATH or not. – Michael Hampton Jul 15 '12 at 15:32
  • 2
    Just to be clear, extensions don't matter in the Unix world, unlike in Windows. where extensions like `.EXE` and `.COM` matter. However, the rationale behind removing the extension is that generic executables should just be generic executables. The meaning of this tautology is the users just want the functionality and should not need to know or care whether it is Perl or Python or sh or bash or awk etc. - that's a mostly irrelevant implementation detail. And, making a script executable and adding a shebang makes it a generic executable. – jw013 Jul 15 '12 at 18:13
  • But often the user is **you** - and don't you want the suffix? Already when you give the command to open the file for edit, you start to remember how you usually write such code, what shortcuts you must bring back to your muscle-memory, indentation, everything you know at the back of your mind starts to reappear. You don't want to wait for the letters to appear to realize, oh, that is done in X! Also, isn't suffixes used by for example emacs to put you in the correct mode? But when it comes to usage exclusively, the path should not be there, the extension should not be there, just the command. – Emanuel Berg Jul 21 '12 at 01:10
  • @EmanuelBerg You are talking about the *developer*'s perspective, *not* the user's. What I said applies to the user side. As a developer you can set up your workflow however you (or your team) like, but you should definitely take out the extension when you package and ship. (It's probably fine to ship a file with an extension and provide an extension-less symlink for the user.) Any decent editor like `vim` and `emacs` should be able to determine file type from the shebang line, *NOT* the suffix - after all it's what your system uses to find the interpreter to run the file. – jw013 Jul 22 '12 at 16:33
  • Right, good point. – Emanuel Berg Jul 22 '12 at 16:36
2

In C, it looks like this:

int main(int argc, char *argv[]) {

The argc is the number of arguments. Note that the program/tool name counts.

The arguments themselves end up in the argument vector (or array), argv.

Then there is the tricky part of writing code to deal with them the intended way.

Then compile with gcc. You specify the name of the program with the -o (outfile) flag. Run the file from its current directory like this: ./tool_name input_file_1 ... input_file_n (or put it in a directory that turns up when you write echo $PATH, then you can invoke it from anywhere, i.e., without the dot).

Emanuel Berg
  • 6,763
  • 7
  • 43
  • 65
1

I use Go. It's a compiled language, cross-platform, with the ease of programming of a dynamic language, and support for concurrency and communication.

I'm not going back to Python since it's very funny to develop with Go.

http://golang.org/
https://github.com/languages/Go

Here you have a simple program which gets the arguments used in that command:

package main

import (
  "fmt"
  "flag"
)

func main() {
  fmt.Println("Arguments: ", flag.Args())
}

http://play.golang.org/p/1dpUT11-cc

Marc
  • 1,651
  • 3
  • 14
  • 16
  • Go is great for some targeted use-cases; however, comparing Go and Python for general utilities is like comparing a hunting knife (Go) with a swiss army knife (Python) – Mike Pennington Jul 15 '12 at 17:56
  • 1
    Go is a general-purpose language, which is being used i.e in Youtube, in web applications, or to build games (there is a game finished and another ones in developing). In addition of to be used in CLI applications. – Marc Jul 15 '12 at 20:33
  • 5
    He just wants to know how to make an executable program; we don't really need to get in a language war – Michael Mrozek Jul 15 '12 at 22:22