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 10-27-2009, 08:43 PM   #1
baigmd
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
C program to execute Shell script with arguments


Hi,

Can anyone help with the below code. I am trying to execute Shell script with arguments in C program but it is giving errors.

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

int main (int argc, char *argv[])
{
int count;
for (count = 1; count < argc; count++)
{
return system("/tmp/lab5.sh ", argv[count]);
}
return 0;
}
 
Old 10-27-2009, 09:33 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,661

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by baigmd View Post
Hi,

Can anyone help with the below code. I am trying to execute Shell script with arguments in C program but it is giving errors.

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

int main (int argc, char *argv[])
{
int count;
for (count = 1; count < argc; count++)
{
return system("/tmp/lab5.sh ", argv[count]);
}
return 0;
}
Then how about telling us what the actual errors ARE, and maybe we can help.
 
Old 10-27-2009, 09:34 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
The system call only takes a single string argument. You will need to append the lab5 argument to the "/tmp/lab5.sh" string inside the C code before calling system.

Last edited by neonsignal; 10-27-2009 at 09:35 PM.
 
Old 10-27-2009, 09:40 PM   #4
baigmd
LQ Newbie
 
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
modified the script but not getting actual result

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

int main (int argc, char *argv[])
{
int count;
for (count = 1; count < argc; count++)
{
return system("/tmp/lab5.sh argv[count]");
}
return 0;
}


The result i m getting is
[root@RH100 /]# ./lab5 ad /etc/passwd
This is lab5.sh, running at Tue Oct 27 22:36:21 EDT 2009
It is running on RH100.com
Script being run by
User root
UID 0
who is really root

Called with 1 arguments
-> Argument argv[count] either is not a regular file, or doesn't exist

===========
It should show called with 2 arguments and first one should display not a regular file and second one should display as a file. The problem is parameters are not being passed to shell script properly.
 
Old 10-27-2009, 09:51 PM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You might need to do a bit of background reading on using strings in C. The language has little native support for strings, so you need to write code and use string library functions to manipulate them (in your example, you might make use of strncat).

You might find this task easier in a scripting language such as bash shell script or perl, which handle strings natively, unless you have particular reasons for wanting to use C.

Last edited by neonsignal; 10-27-2009 at 09:57 PM.
 
  


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
C program to execute shell script baigmd Programming 1 10-26-2009 11:24 PM
csh Shell Script: set: Too many arguments. Anyway to overcome this? vxc69 Programming 2 05-04-2009 08:33 PM
Which program should I use to execute a shell script In Ubuntu 8.04 64-bit? joel.breger Linux - Newbie 4 05-26-2008 10:29 AM
LXer: Parsing arguments for your shell script LXer Syndicated Linux News 0 07-18-2007 03:17 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM

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

All times are GMT -5. The time now is 03:53 AM.

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