1

I need to pass an argument which will change every time from C program to a shell script.

int val=1234;
char buf[100];
sprintf(buf,"echo %d",val);
system("call.sh $buf");

call.sh::

#!/bin/sh
echo "welcome"
echo $*
echo "done"

output of C is::

welcome    
done

I cant see the argument value which is 1234 in the script. Can anybody suggest me to get right value...

1
  • Even though Andrew's answer is right, I'd just make the C program print only the variable to stdout and then piping it into the shell script as an argument. Commented Oct 30, 2013 at 19:26

2 Answers 2

2

You can't pass a C variable as a shell variable. You need to build the whole command line in the string, and then pass it to system(...)

int val=1234;
char buf[100];
sprintf(buf, "call.sh %d", val);
system(buf);
Sign up to request clarification or add additional context in comments.

Comments

0

You should use the setenv(), getenv() or putenv() functions (defined instdlib.h). Quoting the man:

The setenv() function adds the variable name to the environment with the value value, if name does not already exist. If name does exist in the environment, then its value is changed to value if overwrite is nonzero; if overwrite is zero, then the value of name is not changed. This function makes copies of the strings pointed to by name and value (by contrast with putenv(3)).

The prototype of the function is the following:

int setenv(const char *name, const char *value, int overwrite);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.