The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
C program to execute shell script baigmd Shell Programming and Scripting 2 3 Weeks Ago 03:08 AM
How to execute a program at expect script fozay Shell Programming and Scripting 6 07-10-2009 04:10 AM
Script to open program and send/execute command in program tuathan UNIX for Dummies Questions & Answers 1 11-19-2008 09:59 PM
How to execute shell Script? siva Shell Programming and Scripting 3 05-14-2002 11:07 AM
Urgent!! How to write a shell program to execute command to access internet? firebirdonfire UNIX for Dummies Questions & Answers 2 04-26-2001 11:16 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
baigmd baigmd is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 5
C program to execute shell script

Hi,

Can anyone give me a sample code to execute shell script from C program.

Thanks
  #2 (permalink)  
Old 3 Weeks Ago
migurus migurus is offline
Registered User
  
 

Join Date: Sep 2008
Location: US
Posts: 49
man system
Example:
system("pwd");
  #3 (permalink)  
Old 3 Weeks Ago
pogdorica's Avatar
pogdorica pogdorica is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid
Posts: 53
In "C program to execute Shell script with arguments "thread, you can see an C++ example.
  #4 (permalink)  
Old 3 Weeks Ago
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
This line of code is invalid
Code:
return system("/tmp/lab5.sh ", argv[count]);
The system() API takes one argument only. Man system for more information.

Hint - create a buffer using an array or malloc(). strcpy "/tmp/lab5.sh " to buffer. strcat argv[count] to the buffer. Use buffer as argument to system API.
  #5 (permalink)  
Old 3 Weeks Ago
baigmd baigmd is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 5
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.

Code:
#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;
}

Last edited by pludi; 3 Weeks Ago at 02:24 AM.. Reason: code tags, please...
  #6 (permalink)  
Old 3 Weeks Ago
pogdorica's Avatar
pogdorica pogdorica is offline
Registered User
  
 

Join Date: Oct 2009
Location: Madrid
Posts: 53
C program to execute script

In C++, you can:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string>

int main (int argc, char *argv[])
{
 int count;
 std::string cadena = "";
 std::string orden;

 for (count = 1; count < argc; count++)
 {
   cadena.append (argv[count]);
 }
 orden = "/tmp/lab5.sh " + cadena;
 return system(orden.c_str());
}
If you need C language, only you must replace the "append" operation with concatenate (strcat or sprintf) variables C char *.

Last edited by fpmurphy; 3 Weeks Ago at 08:20 AM.. Reason: code tags added
  #7 (permalink)  
Old 2 Weeks Ago
tsurko tsurko is offline
Registered User
  
 

Join Date: Jun 2007
Location: Sofia, Bulgaria
Posts: 35
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.

Code:
#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;
}
One more thing - you call return system() in for(). return will terminate main() immediately so you won't have your script executed argc times. I'd suggest to save the return value of system in new variable and examine it if you need error checking.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0