Is there a way I can set up my program (outputs a plaintext file into terminal) to run using my_custom_command, versus the filepath?
Is there a way I can set up my program (outputs a plaintext file into terminal) to run using my_custom_command, versus the filepath?
I'm not quite sure what you're asking. Are you trying to run a command like "program --dump what.txt" with one command? Or run a program and automatically open it in a terminal?
If you want to automatically add some options to your program, you can create an alias with alias program='program --dump' (that's for bash, for csh I think you just leave out the '='). Add that to your .bashrc (or whatever it's called for your shell) to make it always run on startup. Or you can create a shell script for it; for example:
Then call that program.sh or something, make it executable with "chmod +x program.sh", and run it with ./program.sh. You can put it into a special directory, e.g. ~/bin, and add that to the path if you want to be able to run the command from anywhere.Code:#!/bin/sh # This is a comment # $@ evaluates to all of the arguments passed to the shell script program --dump $@
Second interpretation: if you're trying to make a command open in a new terminal, so that you can run it from a GUI or something, then consider
Again, you can alias that or put it into a shell script if you want to.Code:$ konsole -e program --dump what.txt
Remember, if you want less vague answers next time, ask a more specific question.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I suppose that was fairly vague.
I think your first answer is talking about what I want. I'm not too familiar with Linux programming or terminal yet.
I want to be able to type "open myname" or somesuch to open the program. I don't actually know what the open command is. To be more specific, up until now I've been making single executable files. Now I want to do a shell program that something like "sudo apt-get purge myname" or soforth associates with the program in question.
I have no idea what that means.I want to be able to type "open myname" or somesuch to open the program. I don't actually know what the open command is. To be more specific, up until now I've been making single executable files. Now I want to do a shell program that something like "sudo apt-get purge myname" or soforth associates with the program in question.
Let me try the hat of divination: I think you're thinking something is magical, when it really isn't. In the command "sudo apt-get purge myname", sudo is the program that is run - sudo is a plain-old honest C program, just like anything you write. For it's argv, it will get passed {"sudo", "apt-get", "purge", "myname"}. sudo will the check that you are a sudo-er, that you can type the password correctly, and if you can, it will raise it's privileges (which it can do because it's setuid root) and then execute the program "apt-get" (also a plain-old honest C (maybe C++ or some other language, I don't know) program, with the arguments {"apt-get", "purge", "myname"}. No card up the sleeves, no magic tricks. sudo just interprets that first argument to be the name of the program. It's just a string - what the string represents is up to the program. To sudo, it's an executable's name/a command to run.
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Well, perhaps "vague" wasn't quite the right word. "Rambling", or "irrelevant", or "way-too-easy/way-too-difficult" is more what I was after.
Can you give an actual example, with what your executable is actually called and what you're actually trying to do?
You do know how to run a C/C++ program that you've created, right?
Code:$ cd where/ever $ gcc program.c -o program $ ./program $
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I'm aware of running things like that, yes.
In this case, the program takes a plaintext file and outputs its contents into terminal. The names and locations I haven't determined yet, so let's call the executable "info", and say the plaintext file is called "log" and located in /Documents (~/Documents/log).
Ok... did you hit post before you finished writing that example?
Perhaps what you're looking for is argv/argc. If I execute a program like so:
Then it will be passed 4 strings: "./my_program", "arg1", "arg2", and "arg3". These are available from the argc/argv arguments to the main() function:Code:$ ./my_program arg1 arg2 arg3
^ Compile & run that, and pass it various things. Also, for convience, argv[argc] is always NULL.Code:#include <stdio.h> int main(int argc, char *argv[]) { int i; // argc is how many arguments we've been passed. for(i = 0; i < argc; ++i) { printf("Argument %d: %s\n", i, argv[i]); } return 0; }
Thus, you could run you imaginary program like:
and it's argv[1] would be "~/Documents/log". You could then fopen() that, and do whatever.Code:$ info ~/Documents/log
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
The only thing about that is you're adding in an extra word, since ~/Documents/log on it's own would run the file.
Edit: also, I don't quite understand that program.
Elaborate on whatever you mean here. Following your example, "~/Documents/log" is a "plaintext file" -- presumably a log of some sort, judging by the name you gave it. You can do the following at the terminal:The only thing about that is you're adding in an extra word, since ~/Documents/log on it's own would run the file.
But that will give you an error unless log is an executable, which, if it's a plaintext log file, it probably is not.Code:$ ~/Documents/log
Did you compile and run it? The program is very simple - it prints out the arguments that are passed to it.Edit: also, I don't quite understand that program.
I feel like there is some deeper understanding of something that you've not quite grasped. The main problem I'm having replying to your posts is that you're not giving me a lot of information to work with. If you could, in your next post, take the time to elaborate. Tell us what you are trying to accomplish. Give examples of how it (whatever it is) should work, etc.
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
In this case, I'm running a compiled executable, so ./info (let's say it's in home) will run the program fine. The program takes the contents of /Documents/log and outputs them in terminal.
I want to know if I can use a nicer command to run this executable instead of ./info, the filepath.
Like sudo, as an example. To do a task with sudo, you don't need to type out the filepath, just plain 'sudo' and whatever the other peramaters are.
And I'll try running the program, but I can't tell what the arguments are, or the significance of it at all.
Edit: Alright, I understand what it does. Counts arguments put in. I must be missing something... why would I use this?
I'm pretty sure the shell script is what I need.
Though I couldn't get it to do anything. What would be replaced with what? And what is dump?Code:#!/bin/sh # This is a comment # $@ evaluates to all of the arguments passed to the shell script program --dump $@
Last edited by Muscovy; 06-20-2009 at 03:30 PM.
Oh, you mean, instead of having to type the "./" in front of "./program", you want to be able to just type "program" as you would run a system program like "sudo"?
That's even easier than you think. You just have to add a directory to your PATH. For example, I usually have a ~/bin directory where I put my own programs that I want to be able to run from anywhere. Then you can just copy (or symlink) your program to that directory. You do also have to add the directory to the path; a command like this will suffice.
To avoid having to type that every time you open a new shell, you can put that line into ~/.bashrc (if you're using bash) to have it run every time a new shell launches.Code:$ export PATH=~/bin:$PATH
e.g.
Note that what I've described only works for bash. If that doesn't work, try runningCode:~/wherever$ ./program $ mkdir ~/bin $ cp program ~/bin $ export PATH=~/bin:$PATH $ program $
and look at the last part of the line; that tells you what your shell is. Tell us what it says.Code:cat /etc/passwd | grep $(whoami)
A shell script can be useful if your program has to be run from a specific directory, for example. Then you could put a shell script like this into your ~/bin:
Shell scripts are really quite simple. They just list a sequence of commands, and the effect of running the script is exactly the same as if you'd typed those commands at the command-line directly.Code:#!/bin/sh # This is program.sh, a shell script to run ~/whatever/program from its installation directory. cd ~/whatever/ ./whatever
Let me know if this guess is wrong.
[edit] My example shell script contained this line:
That was a hypothetical program. The program's name was "program", and I was passing it the flag "--dump" as well as all other flags passed to the shell script. [/edit]Code:program --dump $@
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
This seems to be exactly what I'm talking about. Yay! I'll play around with it and see if I can comprehend it.
Is bin folder you're refering to the /bin in filesystem, or is it in a program folder? First makes more sense to me, but the mkdir supports the second.
Edit: Ok, it makes a /bin in wherever the executable is.
I tried it with a simple program called 'reader', and it would execute reader when I launched 'shell'.
However, nothing happens yet when I run just plain 'reader' or shell.Code:./reader mkdir ~/bin cp reader ~/bin export PATH=~/bin:$PATH reader
Last edited by Muscovy; 06-20-2009 at 04:16 PM.
No, altho it might seem that way because of coincidence. ~ is your home directory. You can make a directory called bin anywhere:
mkdir /tmp/bin
The nice thing about having a bin in your home directory is you can add this to your ~/.bashrc
PATH=$PATH:$HOME/bin
export PATH
That adds ~/bin to the path, but only for you. So now you can throw all your executables in there and bash will find them. Just make sure the names don't conflict with something already in the path! You don't have to call it bin either.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
PATH being...? Logical guess is, of course, filepath, but filepath=filepath would be utterly redundant. Is it the new run command for the HOME/bin/reader?
Wait, critical problem, I can't get at ~./bashrc. View hidden files doesn't show it, search doesn't, but if I take a new folder and call it .bashrc, it says it's in use.
Last edited by Muscovy; 06-20-2009 at 04:36 PM.
PATH is an environment variable. It's a special value that's available to bash and to all processes that bash starts (e.g. your program or sudo!) if they care to look.
The syntax
means "take the string '~/bin:' and append the current value of the PATH variable, and put the result back into the PATH variable." In effect, you add "~/bin:" onto the beginning of the PATH variable. If you look at a PATH variable, it might look something like this:Code:PATH=~/bin:$PATH
As you can see, directories in the PATH are separated by colons. What the command I mentioned does is to add "/home/user/bin" onto the beginning. (Directories are searched in the order they appear, so that this way your programs take precedence over built-in ones.) So after executing "PATH=~/bin:$PATH", you might seeCode:$ echo $PATH /usr/bin:/usr/local/bin:/bin
Where user is your username.Code:$ echo $PATH /home/user/bin:/usr/bin:/usr/local/bin:/bin
Anyway, got to go. Good luck.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.