LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-09-2012, 04:12 AM   #1
ankitm
Member
 
Registered: Sep 2011
Posts: 33

Rep: Reputation: Disabled
calling a shell script from exec.


Hi ,

I was trying to use exec to run a shell script

#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
unsigned int child;
switch(child=fork())
{
case 0:
printf("in child");
execv("./check_script",NULL);
break;
case -1:
printf("Error");
break;
default:
printf("Parent %d",child);
break;
}
return 0;
}
However on executing the above code , I am getting the following output.
/a.out
Parent 17507in child
check_script just echoes a String .

Please explain this behaviour.
 
Old 04-09-2012, 05:40 AM   #2
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Can you share the contents of your check_script?
 
Old 04-09-2012, 05:43 AM   #3
ankitm
Member
 
Registered: Sep 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
Hi,
It just echoes iniside script.
vi ./check_script
echo "Inside script"

Thanks..
 
Old 04-09-2012, 05:53 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Please use [CODE] [/CODE] tags around your code, to make it readable.

Quote:
Originally Posted by ankitm View Post
execv("./check_script",NULL);
There is the first part of your problem. If you look at the man 3 execv man page, you'll see that the first parameter is the path to the file, and the second parameter is the argument array. The argument array itself cannot be empty, as the first entry in the array must be the script name. The last entry in the array must be NULL.

I often use something like
Code:
char *args[2] = { "./check_script", NULL };

execv(args[0], args);
especially if I have parameters I wish to supply to the script. If you don't need to supply any arguments to the script, you might wish to use execl instead:
Code:
execl("./check_script", "./check_script", NULL);
The first parameter is the path to the script, and the second parameter is the script name. If you add echo "$0" in ./check_script, you can output the script name, as seen by the script itself.

Just remember that the path to the script is first, then the script name -- in both argument array and execl() parameter list --, followed by parameters, followed by NULL, and that neither the path to the script nor the script name can be NULL.

The second part of your problem is that you are missing the shebang line, which tells the kernel which shell interpreter to use:
Code:
#!/bin/bash
echo "This is ./check_script, '$0'."
Hope this helps,
 
1 members found this post helpful.
Old 04-09-2012, 06:05 AM   #5
ankitm
Member
 
Registered: Sep 2011
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks for help ...
 
  


Reply



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
how to use exec in shell script sagar.gunjal Programming 16 12-19-2011 11:10 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
Using exec in a shell script opus-outlaw Linux - Desktop 1 01-19-2008 11:47 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration