Calling a script from a C program??

Hi, I’m a little new at QNX and I am having a really annoying problem. I need to FTP a file every minute to my web server and then make some graphs and such. I figured why not call an FTP script from the C program??

I can call other functions like “touch” and “rm” using the system command with no problem:

system(“touch blah”); ← Creates a file called blah…

Then, not even getting into FTP, I tried to just write an FTP script called script to do touch:

#!/bin/sh

touch blah

It runs fine when script is called, but when added to the C program, it won’t call it:

system(“script”);

Shawn,

script is a shell reserved word. So what’s happening is the shell reserved word is getting executed not your script.

Just rename to foo (don’t use test because that is also a reserved shell word) and it will work.

Tim

Yea…I’ve tried a couple of different names for my script. Still doesn’t work…

Have you thought about the path? When you spawn a system call, you aren’t neccessarily where you think you are, and you don’t neccessarily have the environment you think you have. Even “.” may not be in the path. Try

system("/path/to/the/script/foo"); 

instead.

-James Ingraham
Sage Automation, Inc.

Yep…tried the system path too…i’ve been google-ing this thing for days now. I’ve tried:

system(“foo”);

system("/temp/foo");

system("/bin/sh /temp/foo");

system("/bin/sh -c /temp/foo");

system("/bin/foo"); <–moved to bin for the heck of it at one point

Then I got into the execl(“foo”); area

I even tried chmod 777 foo and making sure I was the owner and in the group

This is getting tiresome…sigh…I never thought something to simple would slow my project down so much…

Is the script not being found, or is it not working? You mentioned using ftp. ftp usually requires human intervention, so it doesn’t work well in a script.

Define doesn’t work. Does system() return an error code, what is the value of errno?

Shawn,

I’m going to assume if you call your script from a shell it works fine? I’m going to assume that if you then run your program from the shell that it doesn’t work? Are these correct assumptions?

Can you post your program code and script code. I assume it’s just a simple 10 line program and 3 line script you are using for testing but it would at least be useful to see that.

Tim

Yea Tim, that’s exactly what it is. My script right now is simple:

#!/bin/sh

touch foo


And Here is my testing code…something simple…i would think it should work

#include <stdio.h>
#include <stdlib.h>

void main()
{

system("/temp/foo");

}

Shawn,

If your script is called ‘foo’ then this line inside the script

touch foo

isn’t going to do anything since you already have a file called foo which is your script file. This assumes of course that you are running from the /temp directory.

Don’t you mean to do:

#!/bin/sh

touch bar

in your script and then see a file called bar being created.

In your C code you should just do a

system(“foo”);

instead of doing all the pathing. Then just make sure you are in the directory where the exe and script file is when running your exe file.

Tim

Yea sorry…typo on my part, but i did try that as well. Originally I had a script called ftp1 that had “touch blah” in it and I called it by system(“ftp1”); inside the directory as well…sorry for all of the confusion. If anyone can write some example code that they have tried in C on QNX that would be awesome :slight_smile:

Works for me.

  • Named the script foo, did chmod +x to it.
  • Changed the script to touch foobar
  • Added error handling to system (ALWAYS put error handling)
  • Compile.
  • Executable is in same directory as script (system(“foo”))
  • Ran executable
  • File foobar was there with 0 size, as expected

Sounds like a permission issue. Does system() return any errors?

BTW, when you get into ftping from a script you’ll probably want to use “curl”. It doesn’t come by default on QNX but it’s an easy port. Also I think it’s part of the /opt/crank archive on our website ( cranksoftware.com/downloads/ )

simply run

curl -T ./file_to_be_uploaded.txt  [ftp://ipaddr/path/](ftp://ipaddr/path/) --user username:password

I’ve found ftp is terriable for it’s scripting support.

Jason

Ok, I did a very simple one and structured it like Mario said. It worked perfectly. Thank you all so far for all of your help. And I will look into curl, thanks. I still cannot get it to run in my final program though…the permissions are set fine, but I am beginning to think it’s my makefile. When I ran the simple version above, I compiled “cc test.c” and ran a.out…but with this program, I have a makefile that was created by someone back in the early 90’s (sigh)…here is somewhat of an example I have…

go: foo1.o foo2.o foo3.o foo4.o foo5.o foo6.o foo7.o foo8.o foo9.o
foo10.o foo11.o
cc -ltermlib -o go foo1.o foo2.o foo3.o foo4.o foo5.o foo6.o foo7.o foo8.o foo9.o
foo10.o foo11.o

foo1.o: foo1.c foo1.h defines.h
cc -c foo1.o foo1.c

foo2.o: foo2.c foo2.h defines.h
cc -c foo2.o foo2.c
.
.
.
clean:
$(RM) *.o go


I could be completely wrong, but the simple file works and this doesn’t…does this makefile look decent???

Ya know what…scratch all that…the system call works if I move it somewhere else in the code. Something weird is happening. I’ll have to debug this one on my own. Thanks you all for your help!

You’re not closing fds 0,1 or 2 are you. All sorts of hell breaks loose when trying to run shells (which is what system does) without those fds. We’ve found this the HARD way.