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
how to get number char from a string netbanker Shell Programming and Scripting 4 07-16-2008 04:16 PM
last char from a string broli Shell Programming and Scripting 6 12-07-2007 08:02 PM
replacing char with string phani_sree High Level Programming 1 11-20-2006 08:57 AM
string of 7 char length always... thanuman UNIX for Dummies Questions & Answers 3 04-12-2005 01:51 PM
Compare Char to String Phobos High Level Programming 3 04-09-2005 12:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-30-2009
zius_oram zius_oram is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 27
pass char string via system()

hello all
i am trying to pass a argument through system function to a shell script.

Code:
 
#shell script echo.sh to display the string
echo $1
and the c program is.

Code:
 
#include<stdlib.h>
int 
main()
{
  const char *str = "hello all";
  system("sh echo.sh str");
}
the output i get is str and not the string "hello all", what is going wrong here. I want to pass a string argument to a shell script via system function.
  #2 (permalink)  
Old 04-30-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,309
The system command expects a string with a command and str within the command is passed as a literal string, not as a variable. Assign the command to a string before you pass it to the system function, something like:

Code:
#include <stdlib.h>

int main()
{
  const char *str = "echo hello all";
  system(str);
}
  #3 (permalink)  
Old 04-30-2009
zius_oram zius_oram is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 27
thank you for your reply,
actually "hello all" is not fixed, it can be any string coming from a java program which i further pass to shell script, can i do it with system() or i have any other way to send this variable form c to shell script.
  #4 (permalink)  
Old 04-30-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,309
You can use sprintf to format a string with your command and the variable string.
  #5 (permalink)  
Old 04-30-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
The way echo.sh is written it will display just "hello" and not "hello all".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  const char *str = "hello all";
  char cmd[30];
  sprintf(cmd, "./echo.sh %s", str);
  system(cmd);
}
  #6 (permalink)  
Old 04-30-2009
zius_oram zius_oram is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 27
thank you franklin52 and shamrok its working, you dont know but it was the only stage incomplete in my project you helped me a lot......thankkkkkkkk youuuuuuuu.
Closed Thread

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 07:06 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