~
~
:wq

Saturday 27 June 2015

Passing STDIN or positional arguments to python code enbedded in bash script (here-documents)

spanish version - all english posts

This is a short post about bash's here-documents and python embedded scripts. Very useful to write quick and dirty scripts.

The issue here is to demonstrate how to pass either positional arguments or stdin input (pipe) to python code inside a bash's here-document block of code

Proposal, write a bash script or function that accepts either pipe input or positional parameters and pass them to a python block of code within the bash script.

First approach: ever wonder how to pass bash positional args to inside python here document? Here is the code:

#! /bin/bash

[... bash stuff ...]

# our python block
python - $@ << EOF
import sys
print sys.argv[:]
EOF

[... more bash stuff ...]

this is how it works:

bash firstapproach.sh one two
['-', 'one', 'two']

Let's go ahead... What if you want to accept ether standard input or positional args? this is IMHO a good approach to the problem (using a function to make the code clearer):

#! /bin/bash

[... bash stuff ...]

# the function which passes to our python here-document either
# positional args or STDIN input
# pipe input is only used if no positional args are present:
myfunct () {
[[ $# > 0 ]] && ARGS=$@ || read ARGS
python - $ARGS << EOF
import sys
print sys.argv[:]
exit
EOF
}

[... more bash stuff ...]

And here is the resulting output of both the STDIN and the positional args:

~$ echo hola amigos | myfunct
['-', 'hola', 'amigos']

~$ myfunct hasta luego
['-', 'hasta', 'luego']

HTH

Friday 26 June 2015

Pasar argumentos posicionales (o STDIN) del script bash a un bloque python en un here-document

english version - all spanish posts

Aquí va un post corto sobre scripts en bash con código incrustado de python en "here-documents". Muy útil para scriping rápido.

El asunto aquí es demostrar cómo pasar tanto argumentos posicionales como desde la entrada estandar a un bloque de código python incrustado en un here-document dentro de un script de bash

Propuesta: escribir un script o una función bash que acepte parámetros posicionales o STDIN y que se los pase al bloque de python dentro del here-document.

Aproximación inicial: ¿nunca has querido saber cómo pasar los argumentos posicionales del script de bash al bloque de python que tienes en tu script? Aquí va el código:

#! /bin/bash

[... código bash ...]

# Nuestro bloque python
python - $@ << EOF
import sys
print sys.argv[:]
EOF

[... más código bash ...]

Así funciona:

bash aproximacioninicial.sh one two
['-', 'one', 'two']

Vamos a avanzar un poco más... ¿Qué pasa si queremos aceptar también un pipe desde el STDIN? En mi «humilde» opinión esta sería una buena aproximación a la resolución del problema (utilizo una función para hacer el código más claro):

#! /bin/bash

[... código bash ...]

# la funciona que pasa a nuestro python tanto 
# argumentos posicionales como input del STDIN
# en este caso si no hay argumentos utilizamos el STDIN
myfunct () {
[[ $# > 0 ]] && ARGS=$@ || read ARGS
python - $ARGS << EOF
import sys
print sys.argv[:]
exit
EOF
}

[... mas código bash ...]

Y aquí está la salida resultante tanto de la STDIN como cuando usamos los argumentos posicionales:

~$ echo hola amigos | myfunct
['-', 'hola', 'amigos']

~$ myfunct hasta luego
['-', 'hasta', 'luego']

HTH