Jump to content
  • Advertisement
Sign in to follow this  
LostSource

C/Unix use execve to execute a simple script file

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to execute a small script file in a c/unix program. Does anyone have any idea why the script file isn't executing? When I run the bash.exe I give it the arguments "ls" so that when the bash.c file calls execve with the script file it executes with the argument "ls". --------Small Script file "my_bash"--------
#!/bin/sh

echo "Inside my_bash"
echo "$1"

$1
--------C file "bash.c"--------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[], char *envp[]) {
	char *cmd = "source YshBash ";
	
	//printf("%s\n", cmd);
	
	execve(cmd, argv, envp);
	
	exit(0);
}
Thank you ahead of time for reading my post. = ]

Share this post


Link to post
Share on other sites
Advertisement
Thanks for that, I didn't see that! (hahaha). Now when I run the program it doesn't do anything. When I do gcc bash.c and then ./a.out nothing happens. Can execve run scripts?

Share this post


Link to post
Share on other sites
Quote:
Original post by LostSource
Thanks for that, I didn't see that! (hahaha). Now when I run the program it doesn't do anything. When I do gcc bash.c and then ./a.out nothing happens. Can execve run scripts?


Well I ran a google with execve and it seemed to say it could, provided your script beings like yours does.

I suggest adding some output to your c program, to examine what is hapeening and when.


--------C file "bash.c"--------



#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>



int main(int argc, char *argv[], char *envp[])
{

char *cmd = "my_bash";
printf("attempting to run %s\n", cmd );

execve(cmd, argv, envp);
printf("%s failed? \n", cmd );
perror("execve");
}




perror prints errors on library and system calls, and is quite handy for figuring out why stuff may not be working. Try that, and see how it goes.

Share this post


Link to post
Share on other sites
I'm getting this output:

execve: Premission Denied

And I know what that means... Drops head on desk. I forgot to change the file premissions of my_bash with chmod. I did a chmod 755 my_bash and it works now. Thank you for the hint/tip on using perror to show the problem.

PS: I love this place!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!