Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Cannot execute binary file with bash command

I want to run a cpp executable from my git for windows bash. I do not understand why I can run it with ./Main but I can't run it with bash Main or bash Main.exe. In the latter cases, I'm getting an error:

cannot execute binary file

main.cpp

#include<iostream>

int main()
{
std::cout<<"Hello World";
return 0;
}

script.sh

 echo "Hello from Bash script.."
    echo "Hostname:${1}"
    echo "Port:${2}"
    echo "Listing contents:"
    ls -a
    echo "Launching cpp executable:"
    path=$(pwd)
    echo "Current path:${path}"
    bash "${path}/Main"

To compile the C++ code, I'm using: g++ -o Main main.cpp.

What is the problem? Can someone explain please?

Upvotes: 0

Views: 9280

Answers (1)

jbaptperez
jbaptperez

Reputation: 696

Just remove the bash on the last line of your script:

"${path}/Main"

Don't forget to make it executable.

chmod +x script.sh

It worked for me:

./script.sh hostname 80
Hello from Bash script..
Hostname:hostname
Port:80
Listing contents:
.       ..      Main        main.cpp    script.sh
Launching cpp executable:
Current path:/tmp/test
Hello World

Upvotes: 3

Related Questions