Using Makefile bash to save the contents of a python file
<p>For those who are curious as to why I'm doing this: I need specific files in a tar ball - no more, no less. I have to write unit tests for <code>make check</code>, but since I'm constrained to having "no more" files, I have to write the check within the <code>make check</code>. In this way, I have to write bash(but I don't want to).</p>

<p>I dislike using bash for unit testing(sorry to all those who like bash. I just dislike it so much that I would rather go with an extremely hacky approach than to write many lines of bash code), so I wrote a python file. I later learned that I have to use bash because of some unknown strict rule. I figured that there was a way to cache the entire content of the python file into a single string in the bash file, so I could take the string literal in bash and write to a python file and then execute it. </p>

<p>I tried the following attempt (in the following script and result, I used another python file that's not unit_test.py, so don't worry if it doesn't actually look like a unit test):</p>

<h2>toStr.py:</h2>

<pre class="lang-py prettyprint-override"><code>import re

with open("unit_test.py", 'r+') as f:
    s = f.read()

s = s.replace("\n", "\\n")
print(s)
</code></pre>



<p>And then I piped the results out using:</p>

<pre><code>python toStr.py &gt; temp.txt
</code></pre>

<p>It looked something like:</p>

<p><code>#!/usr/bin/env python\n\nimport os\nimport sys\n\n#create number of bytes as specified in the args:\nif len(sys.argv) != 3:\n    print("We need a correct number of args : 2 [NUM_BYTES][FILE_NAME].")\n    exit(1)\nn = -1\ntry:\n    n = int(sys.argv[1])\nexcept:\n    print("Error casting number : " + sys.argv[1])\n    exit(1)\n\nrand_string = os.urandom(n)\n\nwith open(sys.argv[2], 'wb+') as f:\n    f.write(rand_string)\n    f.flush()\n    f.close()\n\n</code></p>

<p>I tried taking this as a string literal and echoing it into a new file and see whether I could run it as a python file but it failed.</p>

<p><code>echo '{insert that giant string above here}' &gt; new_unit_test.py</code></p>

<p>I wanted to take this statement above and copy it into my "bash unit test" file so I can just execute the python file within the bash script.</p>

<p>The resulting file looked exactly like {insert giant string here}. What am I doing wrong in my attempt? Are there other, much easier ways where I can hold a python file as a string literal in a bash script?</p>

<p>the easiest way is to only use double-quotes in your python code, then, in your bash script, wrap all of your python code in one pair of single-quotes, e.g.,</p>

<pre><code>#!/bin/bash

python -c 'import os
import sys

#create number of bytes as specified in the args:
if len(sys.argv) != 3:
    print("We need a correct number of args : 2 [NUM_BYTES][FILE_NAME].")
    exit(1)

n = -1
try:
    n = int(sys.argv[1])
except:
    print("Error casting number : " + sys.argv[1])
    exit(1)

rand_string = os.urandom(n)

# i changed ""s to ''s below -webb
with open(sys.argv[2], "wb+") as f:
    f.write(rand_string)
    f.flush()
    f.close()'
</code></pre>

python bash shell makefile