Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
2  
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
3  
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

3 Answers 3

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.

share|improve this answer
    
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
    
That worked perfectly, thanks. –  ACarter Jul 14 '12 at 18:56
    
As soon as you put something in your $PATH, leave out file endings like .py. –  Tass Jul 15 '12 at 11:23
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

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).

share|improve this answer

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

share|improve this answer
    
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
3  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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