12

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.

6
  • is it /path/to/ or /path/toscript/? you seem to mix them up. Commented May 22, 2016 at 19:02
  • Try less path/toscript/mynewshell.sh and see if the file is actually a shell script. Commented May 22, 2016 at 19:03
  • 1
    Edit the question and add the result of cat -v path/toscript/mynewshell.sh. Did you import this script from a windows system? Is the very first line empty? Otherwise does it start with a shebang and a valid command? Commented May 22, 2016 at 19:19
  • 1
    Alternately, run file path/toscript/mynewshell.sh and edit your question to include the output. Commented May 22, 2016 at 19:22
  • 1
    That script file is severely damaged. We could try to guess what should have been in that file but you would be much better off if you could find the original source for it and retrieve an undamaged copy. Commented May 22, 2016 at 21:22

6 Answers 6

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

True, but the executable bit isn't necessary when invoking a script using sh so this shouldn't be the issue here.
I gave the command chmod +x path/toscript/mynewshell.sh /path/to/mynewshell.sh I get: /path/to/mynewshell.sh: Not a directory.
@RBud /path/to/mynewshell.sh, please try with correct paths, otherwise nothing expected would ever happen...
4

I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.

Comments

1

To anyone else having the problem i had.

i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.

open the text file click on "Format" at the top, go down to "Make Plain Text" click it.

open your terminal type chmod 777 /path/to/file.sh

put in terminal: /path/to/file.sh to run it

1 Comment

Whatever you are hoping to accomplish, chmod 777 is wrong and dangerous. You absolutely do not want to grant write access to executable or system files to all users under any circumstances. You will want to revert to sane permissions ASAP (for your use case, probably chmod 755) and learn about the Unix permissions model before you try to use it again. If this happened on a system with Internet access, check whether an intruder could have exploited this to escalate their privileges.
0

From a proposed duplicate:

run_me.sh.xz: run_me.sh.xz: cannot execute binary file

This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.

xz -d ./run_me.sh.xz
chmod +x ./run_me.sh  # probably not necessary if you already did that before
./run_me.sh

Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.

Comments

0

That script is simply not a shell script. A shell script is usually readable and contains shell code. The output your cat command shows looks indeed like it's a binary of some sort. As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.

You can check what it is identified as with the file command so:

file path/to/mynewshell.sh

Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?

Make sure the R scripts point to the right R script executioner.

Comments

0

In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.