Friday, October 28, 2022

[SOLVED] "cannot execute binary file" when trying to run a shell script on linux

Issue

I am very new to linux and shell scriprting. I am trying to run a shellscript from secure shell (ssh) on linux using following commands:

chmod +x path/to/mynewshell.sh

sh path/to/mynewshell.sh

I get this error:

path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.

Tried using this command:

bash path/to/mynewshell.sh

I get the same error.

Tried with this command: su - myusername sh path/to/mynewshell.sh It is asking for my password and giving me this error: no such file or directory.

1.The result of cat -v path/to/mynewshell.sh is: ^@^@^@^@^@^@^@^@Rscript "$dir"/diver_script.R done

2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:

#!/bin/bash/Rscript^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
for dir in /path/to/* ; do 
^@^@^@^@^@^@^@^@Rscript "$dir"/myRscript.R
done

3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"

Please give any advice on how I can try executing the shellscript.


Solution

chmod -x removes execution permission from a file. Do this:

chmod +x path/to/mynewshell.sh

And run it with

/path/to/mynewshell.sh

As the error report says, you script is not actually a script, it's a binary file.



Answered By - Jahid
Answer Checked By - Dawn Plyler (WPSolving Volunteer)