1
votes

When i execute : /bin/sh -xe node -v I get the error : node: node: cannot execute binary file

Please suggest what I need to do to resolve this error.

When i execute below commands:

file /bin/bash Output :/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

file node node: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped

1
I just learnt a lot about "cannot execute binary file" which is mostly caused by an incompatible binary. It doesn't look like this in your case. Hmm... Dumb question: Does your node file has executable (aka x) permissions? - Scheff's Cat
Yes it has when i execute without bash it .. and directly type #node -v i get the required output: #node -v v8.1.0 - user2964033
I could reproduce your problem even in cygwin. I just found out that I can run a self-compiled binary in cygwin's bash (e.g. $ ./test-int.exe) but I cannot when I call it this way: $ bash ./test-int.exe getting exactly your error. 1st guess: I have to provide full path but this didn't help. I'm still trying to find out what happens... - Scheff's Cat

1 Answers

1
votes

Out of curiosity, I played a little bit in my cygwin and got this (for me surprising) result:

$ cat >test-int.c <<EOF
> #include <stdio.h>
> 
> int main(int argc, char **argv)
> {
>   printf("sizeof (128): %u\n", sizeof (128));
>   printf("sizeof (4294967296): %u\n", sizeof (4294967296));
>   printf("sizeof (281474976710656): %u\n", sizeof (281474976710656));
>   return 0;
> }
> EOF

$ gcc -std=c11 -o test-int test-int.c

$ ./test-int 
sizeof (128): 4
sizeof (4294967296): 8
sizeof (281474976710656): 8

$ bash -xe ./test-int
./test-int: ./test-int: cannot execute binary file

$

After some searching I found it. Actually, you and me stumbled into the same trap...

According to man bash (close to the top):

If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands.

Having learnt this, I tried:

$ bash -c ./test-int
sizeof (128): 4
sizeof (4294967296): 8
sizeof (281474976710656): 8

$ bash -xec ./test-int
+ ./test-int
sizeof (128): 4
sizeof (4294967296): 8
sizeof (281474976710656): 8

$

Update:

I just realized another trap – the command line arguments. The following sample illustrates this:

$ cat >test-arg.c <<EOF
> #include <stdio.h>             
> 
> int main(int argc, char **argv)
> {
>   for (int i = 0; i < argc; ++i) printf("argv[%d]: '%s'\n", i, argv[i]);
>   return 0;
> }
> EOF

$ gcc -std=c11 -o test-arg test-arg.c

$ ./test-arg arg1 arg2 arg3
argv[0]: './test-arg'
argv[1]: 'arg1'
argv[2]: 'arg2'
argv[3]: 'arg3'

$ bash -c ./test-arg arg1 arg2 arg3
argv[0]: './test-arg'

$ 

So, what? The arguments are lost!

$ bash -xec ./test-arg arg1 arg2 arg3
+ ./test-arg
argv[0]: './test-arg'

$

This time, I found the solution without consulting the man bash:

$ bash -xec "./test-arg arg1 arg2 arg3"
+ ./test-arg arg1 arg2 arg3
argv[0]: './test-arg'
argv[1]: 'arg1'
argv[2]: 'arg2'
argv[3]: 'arg3'

$

To make it one call, the command and the arguments have to be "quoted together".