pythonbashwindows-subsystem-for-linuxbinaryfiles

Why I fail to run a shell script generated by a python script, which gives me "cannot execute binary file" error?


I use the powershell to generate a shell script by a python script, as the following.

python generate_large_local_array_run.py > large_local_array_run.sh

But when I run the shell script using bash in the powershell (or Ubuntu, I got wsl2 installed in my win10), it gives me "cannot execute binary file" error.

bash large_local_array_run.sh
large_local_array_run.sh: bash large_local_array_run.sh: cannot execute binary file

To solve this problem, I open the shell script large_local_array_run.sh in VScode and directly copied the content into a new text file, and save the new text file as temp.sh. This time, I am able to run temp.sh in powershell as well as Ubuntu with the command bash temp.sh. Why is that the case? What's the difference between the original large_local_array_run.sh and the new text.sh. They are completely the same for raw eye, but do they have some different formats?


Solution

  • Your program

    python generate_large_local_array_run.py
    

    generates a file that is not pure ascii. The problem is probably at the beginning of the file. If possible, you will want to revise the code in generate_large_local_array_run.py so that it provides a valid shell script.

    python generate_large_local_array_run.py | strings > large_local_array_run.sh
    

    lets strings filter-out the non-printables.

    strings prints the sequences of printable characters in files. It is a hack that removes the non-printable characters, and those are the characters that bash chokes on. It is far from fool-proof. strings prints only sequences of at least 4 characters. So,

    echo something
    <binary stuff, non printables>
    ls
    <binary stuff, non printables>
    

    will give you the echo, but not the ls.

    As said: you will need to revise generate_large_local_array_run.py to produce a valid script, without the non-printables.