Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"sh: ./<file> not found" error when trying to execute a file

I've come across a weirdest problem I ever met. I'm cross-compiling an app for ARM CPU with Linux on-board. I'm using buildroot, and all goes well until I'm trying to run the application on the target: I'm getting -sh: ./hw: not found. E.g.:

$ cat /tmp/test.cpp 
#include <cstdio>
#include <vector>

int main(int argc, char** argv){
        printf("Hello Kitty!\n");
        return 0;
}
$ ./arm-linux-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw

load the executable to the target; then issuing on the target:

# ./hw
-sh: ./hw: Permission denied
# chmod +x ./hw
# ./hw
-sh: ./hw: not found
# ls -l ./hw
-rwxr-xr-x    1 root     root          6103 Jan  1 03:40 ./hw

There's more to it: upon building with distro compiler, like arm-linux-gnueabi-g++ -march=armv7-a /tmp/test.cpp -o /tftpboot/hw, the app runs fine!

I compared executables through readelf -a -W /tftpboot/hw, but didn't notice much defference. I pasted both outputs here. The only thing I noticed, are lines Version5 EABI, soft-float ABI vs Version5 EABI. I tried removing the difference by passing either of -mfloat-abi=softfp and -mfloat-abi=soft, but compiler seems to ignore it. I suppose though, this doesn't really matter, as compiler doesn't even warn.

I also thought, perhaps sh outputs this error if an executable is incompatible in some way. But on my host PC I see another error in this case, e.g.:

$ sh /tftpboot/hw
/tftpboot/hw: 1: /tftpboot/hw: Syntax error: word unexpected (expecting ")")
like image 321
Hi-Angel Avatar asked Jul 07 '14 09:07

Hi-Angel


People also ask

How do I run a .sh file in putty Linux?

“run sh file in putty” Code Answer ./script-name-here.sh #Chage the "script-name-here" to the name of the . sh file.

How do I open a sh source file?

Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output.


1 Answers

sh prints this weird error because it is trying to run your program as a shell script!

Your error ./hw: not found is probably caused by the dynamic linker (AKA ELF interpreter) not being found. Try compiling it as a static program with -static or running it with your dynamic loader: # /lib/ld-linux.so.2 ./hw or something like that.

If the problem is that the dynamic loader is named differently in your tool-chain and in your runtime environment you can fix it:

  • In the runtime environment: with a symbolic link.
  • In the tool-chain: use -Wl,--dynamic-linker=/lib/ld-linux.so.2
like image 200
rodrigo Avatar answered Sep 21 '22 09:09

rodrigo