Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: char* argv[]

  1. #1
    Join Date
    Aug 2012
    Beans
    623

    char* argv[]

    Hey,
    Is it possible to get the arguments in argv[], even when instead of <Enter>, some other key, say tab is pressed ?

    What I want is
    Code:
    $Name_Of_ObjectFile ubu<tab>
    This should run the object file, with the argument as ubu.
    Please advise.
    Thanks.

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: char* argv[]

    Short answer: no.

    1) You can't run an object file, you probably meant "executable".
    2) Unless you modify your shell, the program is not run until the Enter key is pressed.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: char* argv[]

    Quote Originally Posted by Bachstelze View Post
    2) Unless you modify your shell, the program is not run until the Enter key is pressed.
    no need to modify the shell for that, every more advanced shell has this feature builtin. tab completions can perform arbitrary tasks, also stuff not related to the completion (though it might get a little tricky to clear the prompt after the execution)
    But it is a very bad idea! it totally goes against what users expect from completions

    there are plenty guides found with google, search for bash tab completion or programmable completion
    Last edited by MadCow108; September 27th, 2012 at 07:47 PM.

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: char* argv[]

    Quote Originally Posted by MadCow108 View Post
    no need to modify the shell for that, every shell has this feature builtin.

    on default ubuntu systems google for bash completion
    Excuse me? Shell completion does not run the program...

    {i]EDIT:[/i] Well, sure, it can be programmed to. But for crying out loud, why whould anyone want to do that?
    Last edited by Bachstelze; September 27th, 2012 at 07:50 PM.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: char* argv[]

    normally not, but it can.
    advanced bash completion beyond the default just execute bashscripts which agian can execute whatevery they want.
    E.g. scp completions can query file listing on the remote machine you want to scp from provided the authentification is passwordless (e.g. via keys), very useful!
    git completions complete tag and branch names by executing git itself to get them (so its even aware of our aliases)
    probably what op wants to do is very similar to what git does.

    look at the stuff in the bash-completion package, there is a lot interesting stuff in there.
    Last edited by MadCow108; September 27th, 2012 at 07:55 PM.

  6. #6
    Join Date
    Aug 2012
    Beans
    623

    Re: char* argv[]

    Quote Originally Posted by Bachstelze View Post
    1) You can't run an object file, you probably meant "executable".
    Bachstelze, sorry for that. Googled up the difference, and I now understand that in an object file, linking is not done with the libraries, and so, it cannot be executed. Thanks for that.

    Please look below for my queries on Tab Completion. Not mixing with this post as it is slightly off-topic with respect to the original post.
    Thanks.

  7. #7
    Join Date
    Aug 2012
    Beans
    623

    Re: char* argv[]

    Quote Originally Posted by MadCow108 View Post
    there are plenty guides found with google, search for bash tab completion or programmable completion
    Quote Originally Posted by Bachstelze View Post
    Shell completion does not run the program...
    Thanks for the response MadCow108,Bachstelze. I googled for some of those, but most info I found like complete and compgen don't allow to run an executable. They only allow to write your custom string to tab-complete. Am I reading the wrong documents?

    Quote Originally Posted by MadCow108 View Post
    normally not, but it can.
    advanced bash completion beyond the default just execute bashscripts which again can execute whatever they want.
    This would be wonderful if it works! Can you provide me some info on how to get started with this.




    My approaches:
    --------------
    1)Download the bash code, I downloaded bash-2.0.tar.gz and try and figure out how the tab-complete works. Haven't gone through the code yet.

    2)Look in the ls/cat/cd code, to see how it handles tab-complete.(But I could be wrong and by the time control comes to ls/cat/cd, the tab-complete part is already done by bash.
    I have downloaded coreutils-5.0.tar.bz2. Haven't gone through the code yet.

    3)Download kernel source code and see where tab-complete comes. I don't think this is a right approach. It's not the kernel that takes care of tab-complete.

    4)compgen . As Bachstelze mentioned above, I don't think this can be used to execute a program.

    Will you be able to tell me if I'm missing the right approach, and/or which of these approaches is correct. I think 1) is the right approach.
    Please advise.
    Thank you MadCow108, Bachstelze.

  8. #8
    Join Date
    Aug 2012
    Beans
    623

    Re: char* argv[]

    Can I add 1 more approach?
    5) Is it possible to trick both the shell/bash and the user such that,
    Let the user enter <Tab>, but the shell thinks that <Enter> is entered and so, responds by running the executable. Is it possible to do some ascii-value aliasing or something of that sort ?

    Sorry for the newbie ideas. But really excited to find a solution to this problem.

    Thanks.

  9. #9
    Join Date
    Aug 2012
    Beans
    623

    Re: char* argv[]

    Just an additional info, my shell is
    Code:
    # echo $0
    -sh
    It doesn't have tab-complete for anything, be it ls/cd etc.

    My intention is to give it tab-complete option for my application alone and not for the entire shell(not really required).
    Please refer my first post, if I'm not clear replacing the word ObjectFile with executable(thanks to Bachstelze).

    Thanks.
    Last edited by IAMTubby; September 28th, 2012 at 11:31 AM.

  10. #10

    Re: char* argv[]

    Why would you do that? Are you going to distribute this modified shell with your program?

    It sounds like you're trying to make the shell itself part of the user interface to your program. This is a Bad Idea. It runs contrary to the principle of least surprise and violates modularity on several levels. It's also contrary to the Unix philosophy, if you're into that kind of thing.

    If this tab-to-run thing is an essential part of your program, then you shouldn't be abusing a shell to make it happen -- it should be part of your program's UI code.

    If, on the other hand, this is just a convenience feature for you, and you have some odd reason for not wanting to press Enter (amputated right pinky?) then feel free to do it any way you can figure out.

    N.B. /bin/sh is a very simple shell. If I wanted this feature, and my primary concern was that it run programs, I'd probably just write my own shell. Even with a rudimentary command history buffer, it would probably take less time than figuring out how to make bash do it without odd side effects. Obviously this isn't an option if the shell needs support for control structures like for, while, if etc.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •