LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-24-2004, 04:12 PM   #1
overbored
Member
 
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:
Code:
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:
Code:
#!/home/joe/main
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, 05:55 PM   #2
ilikejam
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.........

Dave
 
Old 03-24-2004, 05:56 PM   #3
aluser
Member
 
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, 06:41 PM   #4
overbored
Member
 
Registered: Jun 2003
Posts: 58

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


Reply


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 01:09 PM
Easy question to answer! Please help alpineidyll Linux - Software 7 11-16-2003 02:42 PM
iptables easy to answer question BajaNick Linux - Security 4 09-19-2003 08:22 PM
Easy question to answer BajaNick Programming 3 08-03-2003 05:49 PM
Quick and easy answer please Jimbo Mahoney Linux - General 4 11-20-2001 02:44 PM


All times are GMT -5. The time now is 06:56 AM.

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