hello all
i am trying to pass a argument through system function to a shell script.
#shell script echo.sh to display the string
echo $1
and the c program is.
#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.
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:
#include <stdlib.h>
int main()
{
const char *str = "echo hello all";
system(str);
}
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.
You can use sprintf to format a string with your command and the variable string.
The way echo.sh is written it will display just "hello" and not "hello all".
#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);
}
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.