Terminal-run

This is a discussion on Terminal-run within the Linux Programming forums, part of the Platform Specific Boards category; Is there a way I can set up my program (outputs a plaintext file into terminal) to run using my_custom_command, ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    Terminal-run

    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?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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:
    Code:
    #!/bin/sh
    # This is a comment
    # $@ evaluates to all of the arguments passed to the shell script
    program --dump $@
    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.

    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
    Code:
    $ konsole -e program --dump what.txt
    Again, you can alias that or put it into a shell script if you want to.

    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.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    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.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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.
    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)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Quote Originally Posted by dwks View Post
    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
    $
    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).

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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:
    Code:
    $ ./my_program arg1 arg2 arg3
    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:
    #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;
    }
    ^ Compile & run that, and pass it various things. Also, for convience, argv[argc] is always NULL.
    Thus, you could run you imaginary program like:
    Code:
    $ info ~/Documents/log
    and it's argv[1] would be "~/Documents/log". You could then fopen() that, and do whatever.
    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)

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    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.

  9. #9
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    The only thing about that is you're adding in an extra word, since ~/Documents/log on it's own would run the file.
    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:
    Code:
    $ ~/Documents/log
    But that will give you an error unless log is an executable, which, if it's a plaintext log file, it probably is not.

    Edit: also, I don't quite understand that program.
    Did you compile and run it? The program is very simple - it prints out the arguments that are passed to it.

    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)

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    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.
    Code:
    #!/bin/sh
    # This is a comment
    # $@ evaluates to all of the arguments passed to the shell script
    program --dump $@
    Though I couldn't get it to do anything. What would be replaced with what? And what is dump?
    Last edited by Muscovy; 06-20-2009 at 04:30 PM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.
    Code:
    $ export PATH=~/bin:$PATH
    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.

    e.g.
    Code:
    ~/wherever$ ./program
    $ mkdir ~/bin
    $ cp program ~/bin
    $ export PATH=~/bin:$PATH
    $ program
    $
    Note that what I've described only works for bash. If that doesn't work, try running
    Code:
    cat /etc/passwd | grep $(whoami)
    and look at the last part of the line; that tells you what your shell is. Tell us what it says.

    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:
    Code:
    #!/bin/sh
    # This is program.sh, a shell script to run ~/whatever/program from its installation directory.
    
    cd ~/whatever/
    ./whatever
    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.

    Let me know if this guess is wrong.

    [edit] My example shell script contained this line:
    Code:
    program --dump $@
    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]
    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.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    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'.
    Code:
    ./reader
    mkdir ~/bin
    cp reader ~/bin
    export PATH=~/bin:$PATH
    reader
    However, nothing happens yet when I run just plain 'reader' or shell.
    Last edited by Muscovy; 06-20-2009 at 05:16 PM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Muscovy View Post
    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.
    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

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Quote Originally Posted by MK27 View Post
    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.
    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 05:36 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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
    Code:
    PATH=~/bin:$PATH
    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:
    $ echo $PATH
    /usr/bin:/usr/local/bin:/bin
    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 see
    Code:
    $ echo $PATH
    /home/user/bin:/usr/bin:/usr/local/bin:/bin
    Where user is your username.

    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 07:00 PM
  2. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 04:04 AM
  3. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 12:55 PM
  4. MSVC: run w/o debugger vs run w/ debuger
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 09:49 PM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21