1

I'm trying to create a simple script on my server, basically I would like to sent a string and display it via system function...

    #include <stdio.h>

int main()
{
    char txt[100];
    printf("Insert a text: ");
    fgets(txt, 100, stdin);
    system("echo %s"), txt;
    return 0;
}

Rght now I'm not getting any string just "%s"

any idea why?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Matt
  • 29
  • 1
  • First, the line with `system` thing is totally not what you intend to do. Use `printf`, or at least correct `system` syntax. Second, you should look up for **script** definition and how it is different from C program. – Eugene Sh. May 07 '15 at 15:27
  • I have to use "system" because I would like to run some commands in linux but to do this I need to sent string to command – Matt May 07 '15 at 15:32
  • `system` with `echo` will print to `stdout`. `printf` will print to stdout. So what is the difference? – Eugene Sh. May 07 '15 at 15:33
  • @Matt, Then form a string that includes the command and parameters and give that to `system` or use an API designed for starting processes. By the way, Clang warns about the `system` line because `txt` is evaluated and then not used. – chris May 07 '15 at 15:34
  • "*script*" is there a C (or even C++) interpreter around? – alk May 07 '15 at 15:39
  • 1
    You're echoing %s, so that's what you're seeing. – Darius X. May 07 '15 at 15:39
  • construct a string with complete command. str = "echo value_of_text". then system(str) – 911 May 07 '15 at 15:44
  • @alk Yes, there are. Some examples listed here: http://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers – deviantfan May 07 '15 at 15:45
  • 1
    see http://stackoverflow.com/questions/4881937/building-strings-from-variables-in-c – 911 May 07 '15 at 15:45
  • Please do not tag spam, this is c not c++ – Mgetz May 07 '15 at 15:46

4 Answers4

4
system("echo %s"), txt;

This isn't doing what you think; it's an expression which evaluates to txt. Since evaluating txt has no side effects, and since you're not capturing the result of the expression anywhere, adding , txt after the system call essentially does nothing. See this question for some information on the "comma"-operator in C.

Moreover, system doesn't support the use of printf-style format specifiers, so the %s in your string literal doesn't have any special meaning; it's just going to be echoed exactly as written, as you've seen. If you want to construct a command at runtime for use with system, you will have to do so with sprintf or similar.

Community
  • 1
  • 1
Joe Farrell
  • 3,502
  • 1
  • 15
  • 25
2

The prototype to system() is:

int system(const char * command);

From man 3 system:

executes the shell command specified in command

From this we can safely assume s refers to a C-"string".

So prepare the string using for example snprintf():

char s[1024];
snprintf(s, 1024 -1, "echo %s", txt); /* -1 for the C-"string"'s 0-terminator */

Then pass it:

system(s);
alk
  • 69,737
  • 10
  • 105
  • 255
  • Quick question, can I get string from a command line and display it in snprintf so txt will be replaced with a text from command line? – Matt May 07 '15 at 15:54
  • @Matt: Use `int main(int argc, char ** argv)` to access what had been given as parameters to the program on start-up via `argc` and `argv`. Details on this however would better go into another question you might like to pose. – alk May 07 '15 at 15:58
1

Instead of system("echo %s"), txt; try this:

printf("%s", txt);
Hexaholic
  • 3,299
  • 7
  • 30
  • 39
  • Irrelevant to op's problem – Mekap May 07 '15 at 15:28
  • @Mekap Why? Did you read `Rght now I'm not getting any string just "%s"` ? – deviantfan May 07 '15 at 15:46
  • @deviantfan He's willing to execute a script (ie a shell function) who will print a line. He doesn't want or need to use printf. Read the question more carefully before making any wrong asumptions. – Mekap May 07 '15 at 16:05
0

the system statement will not format the output, like printf.

suggest using:

#include <stdio.h>
#include <stdlib.h> // system()
#include <string.h> // strcpy(), strcat()

#define BUF_LEN (100)

int main()
{
    char output[10+BUF_LEN] = "echo ";
    char txt[BUF_LEN] = {'\0'};
    printf("Insert a text: ");
    fgets(txt, BUF_LEN, stdin);
    strcat( output, txt );
    system( output );
    return 0;
}

The above code works very nicely, however;

do not include any command separators, semicolons, or other characters that would be interpreted by the shell in the input string.

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Why `+10` when `+5` would do? – alk May 07 '15 at 15:59
  • What about if I will need to put string in the middle? for example echo [my string] >> text.txt ? – Matt May 07 '15 at 16:21
  • +10 .vs.+5? a little extra room never hurts. If the scenario has the input text in the middle, then there will need to be an extra strcat() and the initial string will need to be appropriately longer. (and probably there will need to be " surrounding the input text. – user3629249 May 09 '15 at 03:56