Results 1 to 3 of 3

Thread: [RESOLVED] #!/bin/bash ??????

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Resolved [RESOLVED] #!/bin/bash ??????

    I have written a program that takes input from STDIN. It works nicely to read a plain text file called test.rpn when I use the following syntaxes...

    Code:
    cat test.rpn | myApp
    Code:
    myApp < test.rpn
    That's all well and good. However I have read up on "shebang" commands that frequently appear at the top of executable text files (shell scripts and so on). I am now trying to use this technique by making test.rpn executable (chmod +x) and adding a shebang line to the top of that file to use the full path to myApp.

    When then run the file (./test.rpn) the shell does indeed invoke myApp but since myApp is expecting the data to be read in from STDIN, it cannot understand and exits without doing anything.

    Trouble is that "./myapp test.rpn" is not a valid way to start my app, and that justso happens to be the way that shebang lines pass the filename in.

    I've tried all sorts of slight changes to the shebang line to try to get it to "<" redirect the filename into myApp but to no avail.

    I also tried adding a second level of shebang by adding a normal shell script in between the test.rpn file and the myApp program. But apparently shebang lines are not able to be nested in this way, it just fails with shell "command not found" errors.

    I must have tried about 10 ways to do this but nothing seems to work.

    I am very reluctant to make myApp read from a filename in the parameter list because of the already complex logic to process the parameter list. I thought doing the shebang trick would be a neat idea that wouldn't require changes to my binary.

    Does anyone have a neat idea about how to achieve this?
    Last edited by wossname; Apr 26th, 2008 at 08:29 AM.
    I don't live here any more.

  2. #2
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: #!/bin/bash ??????

    The shebang only tells the Linux OS what shell to use. I personally like to use bash because it has some nice features, and is pretty common in the Linux world.
    For example I normally use:
    #!/bin/bash

    I'm kinda confused on what you want to do, and wondering why you just can't do this:
    Code:
    #!/bin/bash
    cat test.rpn | myApp
    Or even take that to another level...
    Code:
    #!/bin/bash
    cat $1 | myApp
    And if I remember from my unix class $1 holds the first parameter sent to the script (ie ./script somefile). And $# holds the number of parameters sent to the script.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  3. #3

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: #!/bin/bash ??????

    No, it's not limited to specifying the shell. Any executable file that takes a filename as an argument can be the target of a shebang line.

    For example, put this in a text file, set its executable flag and then run it. The OS reads the shebang line, then invokes cat which gets the filename passed to it. Cat then spits the file out to the terminal.
    Code:
    #!/bin/cat
    
    This is a self-printing help file.
    
    (lots of helpful text)
    
    Thanks for using this help file.
    This is kind of what I need (but instead of cat I would use "myApp"). My problem is that it apparently doesn't support < redirection.

    I don't think this can be overcome without altering my binary.



    EDIT: [RESOLVED]
    I ended up modifying my app a bit. Actually now it is quite a lot better, it now handles arguments in a simpler and more robust way. Now it is compatible with shebang lines (even with parameters).

    I'll post this screeny in case its useful to anyone doing this in the future. the "quadratic.rpn" file is a text file that can be understood by my app "rpnsolve". The shebang lines causes /usr/bin/rpnsolve -q scripts/quadratic.rpn "=1 @a 2 @b -3 @c ;" to be invoked in the shell. rpnsolve's output (having successfully processed the quadratic.rpn file) is -3 and 1 .

    Linux ROCKS!
    Attached Images Attached Images  
    Last edited by wossname; Apr 27th, 2008 at 08:48 AM.
    I don't live here any more.

Posting Permissions

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



Click Here to Expand Forum to Full Width