Execute bash commands in C

2

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

However, I'm having trouble printing the value of a variable.

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

int main(void) {

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

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

    
asked by anonymous 04.07.2018 / 03:27

2 answers

4

You can write a Variadic Function able to receive a string de formatação " as an argument, enabling manipulation of all primitive types by default, see:

#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;
}

Output:

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

See working at 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 VARIAVEL=VALOR format, with a NULL terminator indicating the end, see:

#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;
}

Output:

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 at Ideone.com

To access a specific variavel de ambiente within your program, you can use the getenv() function. from the default library stdlib.h , see:

char * valor = getenv("PATH");
printf( "PATH=%s\n", valor );
    
04.07.2018 / 13:32
5

You want to print formatted, but instead of sending directly to the console will capture this, it would look 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");
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference . It's a little different because I can not access system() in these places.

sprintf() documentation.

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

    
04.07.2018 / 06:12