How can I turn "to" into a bash script

0

I made a small script in Python that transformed a .py file into a bash Linux script, however, the quotes that are inside the Python file end up making the script not work. Is there any way I can transform all% of a text in bash to " ?

The script I did is this:

#!/bin/bash
pyprog=$(cat $1) # Salva nessa variável o texto do arquivo python

echo "#!/bin/bash
echo \"$pyprog\" | python" > $2

# A saída final desse programa seria algo parecido com:
# !bin bash
# echo "print "Exemplo de um programa em python"" | python
# Assim aspas do comando print vão interferir nas aspas do comando echo 

# Neste caso eu precisaria que o script automaticamente transformasse para
# echo "print \"Exemplo de um programa em python\"" | python
    
asked by anonymous 02.12.2018 / 12:23

1 answer

1

write your script as follows:

#!/bin/bash
pyprog=$(cat $1) # Salva nessa variável o texto do arquivo python

echo "#!/bin/bash
echo \"$(echo $pyprog | sed 's/"/\"/g')\" | python" > $2

The sed command will do the magic to replace the characters " with \" .

At the command this substitution is set with \" because we need to escape the backslash so that bash itself does not interpret things the wrong way.

    
10.12.2018 / 17:16