-4

I am trying to execute this execl command in a C program and it simply doesn't work.

execl("~/Desktop/taskc/validating/analyzer/numbers_analyzer", "numbers_analyzer", (char*)NULL);

bash: syntax error near unexpected token `"~/Desktop/taskc/validating/analyzer/numbers_analyzer",

I also tried with some validate examples that I've found on Internet and they also don't work. (I'm always getting the same error)

execl( "/bin/ls", "/bin/ls", argv[1], NULL );
bash: syntax error near unexpected token `"/bin/ls",'

execl("/bin/date", "date", 0, 0);
bash: syntax error near unexpected token `"/bin/date",'

Thanks and regards.

Peter M
  • 7,309
  • 3
  • 50
  • 91
Miguel
  • 1
  • 5

2 Answers2

5

The error message you're getting is what happens if you try to input the C function call directly to the shell:

[dbush] execl("/bin/date", "date", 0, 0);
-bash: syntax error near unexpected token `"/bin/date",'
[dbush]

You need to put the code into an actual C program, compile it, and run it:

#include <stdio.h>
#include <unistd.h>

int main()
{
    // the last argument should be a NULL pointer to signal the end of the arg list
    execl("/bin/date", "date", NULL);
}

Output:

[dbush] gcc  -g -o /tmp/x1 /tmp/x1.c
[dbush] /tmp/x1
Tue Nov 24 20:11:54 UTC 2015
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    And use [wordexp()](http://man7.org/linux/man-pages/man3/wordexp.3.html) to expand the path name first. It supports the same syntax as POSIX shells, so `~` and `$HOME` (and all other POSIX shell environment variable references) will work. – Nominal Animal Nov 24 '15 at 20:17
  • Indeed, `wordexp()` should work, whereas the now-deleted suggestion to use `execlp()` to accommodate the tilde-containing path probably would not have worked (the docs require `execlp()` to perform a path search only if the command name does not contain slash characters, and in any case is unlikely to handle `~` as the shell does). – John Bollinger Nov 24 '15 at 20:20
  • That works. Thanks! I'll try to fix the first command that I put in the question because that was put in a C program – Miguel Nov 24 '15 at 20:21
  • @Miguel Glad I could help. Feel free to [accept this answer](http://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Nov 24 '15 at 20:22
  • This is undefined behaviour. you're passing 2 integers, because 1 didn't work; the last argument to `execl` must be `(char *)0` – Antti Haapala -- Слава Україні Mar 10 '16 at 20:32
  • @AnttiHaapala Good catch. Updated. – dbush Mar 10 '16 at 20:34
0

On my computer, running ubuntu linux 14.04, using bash as the command shell, this command cannot be run from the command line.

However, in the following C program, it works correctly:

#define _POSIX_C_SOURCE  200112L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main( void )
{
    pid_t pid = fork();
    if( 0> pid)
    { // then, error
        perror("fork failed");
        exit( EXIT_FAILURE );
    }

    if( !pid )
    { // child
        execl( "/bin/ls", "/bin/ls", NULL, NULL );
        perror( "execl failed" );
        exit( EXIT_FAILURE );
    }

    printf( "parent running after successful fork\n");
    return 0;
}

giving an output that looks like this:

Note: I ran the program from a terminal

In the following output, I trimmed most of the list of files from the 'ls' command.

parent running after successful fork
a.out              howToCalcPrimeNumbers(works)    tellwait.c
bashloop.sh        howToCalcPrimeNumbers(works).c  tellwait.h
cards.h            howToCalcPrimeNumbers(works).o  tellwait.h.gch
cards.h.gch        input.txt               tellwait.o
cent_convert.c         libSensors.h            test
user3629249
  • 16,402
  • 1
  • 16
  • 17