View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-24-2004, 05:12 PM   #1
Registered: Jun 2003
Posts: 58

Rep: Reputation: 15
Accepting Shabang Scripts (Quick & Easy Question, Just Can't Find Answer)

I'm interested in writing a program that I can use to run shabang scripts.

Here is the program, /home/joe/main.c:
int main(int argc, char** argv) {
        char blah[256];
        if (argc > 0) {
                printf("cmd: %s\n", argv[0]);
        scanf("%s", blah);
        printf("stdin: %s\n", blah);
I compile it with "gcc -o /home/joe/main /home/joe/main.c".

Here's the script, /home/joe/tryme:
hubba hubba
But when I run /home/joe/tryme, it doesn't read the file into stdin (that was my guess), but instead waits for stdin from the prompt (i.e., exact same behavior as I get when running /home/joe/main).

I tried Googling for answers, but all I get (unsurprisingly) is material on the scripting side of things, not on the programming side. How do these scripts work, exactly? Thanks in advance.
Old 03-24-2004, 06:55 PM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Perhaps you would have more luck getting the program to invoke 'bash script_name'

I'm not entirely sure why you would want to write a program to do this, though.........

Old 03-24-2004, 06:56 PM   #3
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
The #! gives the filename of the script to your program as the first argument, not through stdin. You then get to open the file yourself.

btw, argc is 1 when the program is called with no arguments: argv[0] is normally the program's name.
Old 03-24-2004, 07:41 PM   #4
Registered: Jun 2003
Posts: 58

Original Poster
Rep: Reputation: 15
Ah, I see it - the first argument. Thanks!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question....Easy Answer iammike Linux - Software 2 08-22-2004 02:09 PM
Easy question to answer! Please help alpineidyll Linux - Software 7 11-16-2003 03:42 PM
iptables easy to answer question BajaNick Linux - Security 4 09-19-2003 09:22 PM
Easy question to answer BajaNick Programming 3 08-03-2003 06:49 PM
Quick and easy answer please Jimbo Mahoney Linux - General 4 11-20-2001 03:44 PM

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration