1825950 Members
3358 Online
109690 Solutions
New Discussion

Problem with argv

 
SOLVED
Go to solution
Sergio Tancredi
Occasional Advisor

Problem with argv

Hello to everyone,

Now my problem is this:
I have this little script that allows to an user to execute "My_shell_script.sh"
with root privileges:
_____________________________________
#include
main()
{
system("/home/My_shell_script.sh");
}
_____________________________________

But the script it would have to be run (in enviroment Unix) with two parameters, inserted by the user when he run the script:
./My_shell_script_compiled $1 $2

So I've tried to modify My_shell_script.sh:
____________________________________________
#include
char * argv[1];
main()
{
system("/home/My_shell_script.sh $argv[1] $argv[2]");
}
___________________________________________
and then run:

./My_shell_script_compiled $1 $2

as another user, but (as I thought)it doesn't work!

Some errors:
chmod:can't access [1]
ksh: [2]: Arguments must be %job or process ids

Please forgive my syntax but I'm not C expert
;-)

Could you help me?
Thanks,
Sergio
5 REPLIES 5
RikTytgat
Honored Contributor

Re: Problem with argv

Hi,

Use sprintf!

--------------------
int main(int argc, char **argv) {
...
char strCommand[256];
sprintf(strCommand, "/home/My_shell_script.sh %s %s", argv[1], argv[2]);
system(strCommand);
...
}
-------------------

Hope this helps,
Rik
Andreas Voss
Honored Contributor
Solution

Re: Problem with argv

Hi,

if you only want to run you script you should better use the execlp() call:

#include
main(argc,argv)
int argc;
char **argv;
{
execlp("/home/My_shell_script.sh", argv);
}

Other solution for only 2 parameters:
#include
main(argc,argv)
int argc;
char **argv;
{
char cmdbuf[1024];

sprintf(cmdbuf, "/home/My_shell_script.sh %s %s", argv[1], argv[2]);
system(cmdbuf);
}

Regards
Sergio Tancredi
Occasional Advisor

Re: Problem with argv

Hi All,

To Rik: This is the error that I have when compile your script:
(Bundled) cc: "Myscript.c", line 2: error 1705: Function prototypes are an ANSI feature.

- Probably 'cause I haven't ANSI C on my HP-UX.
Thanks however.

To Andreas: This is the error that I have when run your first script:

chmod: can't access {?{?{?^{?:{?F{?P{?`{?i{?|{??{??{??{??
- (???)

Very good instead your second script. It works!
:-)
Thank you very much.
Sergio
Andreas Voss
Honored Contributor

Re: Problem with argv

Sorry,

the mistake at my first code was execlp(..)
It has to be execvp(...)

Regards
RikTytgat
Honored Contributor

Re: Problem with argv

Hi,

The error you had with my program is that you do not have the ansi c compiler. Use K&R style function prototypes (Andreas's examples) instead.

For the rest, my code should have worked!!

Bye.