Execute bash commands in C

Asked

Viewed 1,211 times

2

I’m using the function system("comando"); to execute bash commands in a C program.

However, I am having difficulty printing the value of a variable.

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char* letra= "A";
    system("echo letra");
}

What I can do to work with the contents of a C variable for bash and vice versa??

2 answers

5

You want to print formatted, but instead of sending directly to the console will capture this, it would be something like this:

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

int main(void) {
    char* letra = "A";
    char *buffer = malloc(strlen(letra) + 6); //6 é o tamanho de "echo ", incluindo o terminador, dá para optimizar
    if (sprintf(buffer, "echo %s", letra) > 0) system(buffer);
    else printf("algo deu errado");
}

Behold working in the ideone. And in the Coding Ground. Also put on the Github for future reference. It’s a little different because I can’t access the system() in those places.

Documentation of sprintf().

Note that although the variable is written letra is allowing a word, phrase, or a whole text.

  • 1

    Missed the vice versa, the C picking things from the shell

  • @Jeffersonquesado the question does not speak of this, it speaks of printing a letter.

  • Last paragraph: "What can I do to work with the content of a C variable for bash and vice versa??"

  • 1

    It is that we can not know what he wants to do with it, deep down is an unclear (or broad) if we consider this. Put what you want to do and say no problem at all, you know that is not this tip ode question we wish here. And I’m seriously considering closing, and I probably should have done it sooner. I was in what he put and ignored the rest, I can not speculate what he wants. He may want to manipulate what the call of system() generated a result on the console or something.

4


You can write a Variadic Function capable of receiving a string de formatação as argument, enabling the manipulation of all primitive types in a standard way, see only:

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#define CMD_TAM_MAX   (1024)

int systemf( const char * fmt, ... )
{
    va_list args;
    char cmd[ CMD_TAM_MAX + 1 ];

    va_start( args, fmt );
    vsnprintf( cmd, CMD_TAM_MAX + 1, fmt, args );
    va_end(args);

    return system(cmd);
}

int main( void )
{
    int i = 123;
    char ch = 'X';
    char * txt = "Ola Mundo!";
    float pi = 3.1415f;
    double dbl = 1234567890L;

    systemf( "echo int: %d", i );
    systemf( "echo char: %c", ch );
    systemf( "echo char*: %s", txt );
    systemf( "echo float: %f", pi );
    systemf( "echo double: %f", dbl );

    return 0;
}

Exit:

int: 123
char: X
char*: Ola Mundo!
float: 3.141500
double: 1234567890.000000

See working on Ideone.com

To access the variáveis de ambiente of your shell, you can use a main() with a slightly different signature than usual:

int main( int argc, char **argv, char **envp );

Where char ** envp is the list of all variáveis de ambiente available in the format VARIAVEL=VALOR, with a terminator NULL indicating the end, see only:

#include <stdlib.h>

int main( int argc, char **argv, char **envp )
{
    char **env = NULL;

    for( env = envp; *env != NULL; env++ )
    {
        char * variavel = *env;
        printf( "%s\n", variavel );
    }

    return 0;
}

Exit:

LANG=en_US.UTF-8
WORKSPACE=/tmp/a4a2fff3-ba98-4fda-9d1d-e31d83ac61ee
PWD=/home/0NxZg3
HOME=/home/0NxZg3
TMPDIR=/tmp/WpSkY2
TMPDIR_GLOBAL=/tmp/a4a2fff3-ba98-4fda-9d1d-e31d83ac61ee
SHLVL=0
PATH=/usr/local/bin:/usr/bin:/bin

See working on Ideone.com

To access a variavel de ambiente specific within your program, you can use the function getenv() of the standard library stdlib.h, look at you:

char * valor = getenv("PATH");
printf( "PATH=%s\n", valor );
  • I didn’t know that char **envp. I always went by getenv

  • 1

    @Jeffersonquesado: char **envp is widely supported by various compilers, but is not part of the standard C and neither POSIX.

  • @Lacobus in the first case, in the function you called systemf as I would to return the new command as a string??

Browser other questions tagged

You are not signed in. Login or sign up in order to post.