[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Pasar o recoger salida de comandos a variables.



El 10 de mayo de 2020 11:45:45 CEST, "Camaleón" <noelamac@gmail.com> escribió:
>El 2020-05-10 a las 10:04 +0200, Ramses escribió:
>
>> Buenos días,
>> 
>> Cuando he necesito recoger la salida de un comando para después
>tratarla, lo he hago siempre así:
>> 
>> # PRUEBA=$(ls -la)
>> 
>> No me muestra la salida por Consola pero con un "echo $PRUEBA"
>imprimo el contenido.
>> 
>> Pero me he he encontrado con una curiosidad con NetCat.
>> 
>> Si hago un:
>> 
>> # PRUEBA=$(nc -z -v -w5 80.80.80.80 22)
>> 
>> Me muestra la salida por Consola y si hago un "echo $PRUEBA" me
>muestra la variable vacía.
>> 
>> ¿Sabe alguien por qué ocurre esto con NetCat y cómo tendría que
>hacerlo para recoger la salida en una variable?
>
>Ni idea... ¡esto es linux avanzado! :-P
>
>La explicación la tienes aquí:
>
>****
>passing output of a netcat command to a variable or piping through read
>
>utility
><https://stackoverflow.com/questions/55889713/passing-output-of-a-netcat-command-to-a-variable-or-piping-through-read-utility>
>****
>
>Y ciertamente, enviar a la salida estándar funciona:
>
>sm01@stt008:~$ PRUEBA=$(nc -z -v -w5 80.80.80.80 22 2>&1); echo $PRUEBA
>80.80.80.80: inverse host lookup failed: Unknown host (UNKNOWN)
>[80.80.80.80] 22 (ssh) : Connection timed out
>
>sm01@stt008:~$ PRUEBA=$(nc -z -v -w5 80.80.80.80 22 2>&1); echo $PRUEBA
>> Escritorio/prueba.txt
>sm01@stt008:~$ cat Escritorio/prueba.txt
>80.80.80.80: inverse host lookup failed: Unknown host (UNKNOWN)
>[80.80.80.80] 22 (ssh) : Connection timed out
>
>Saludos,

Sí, sí, ya había encontrado esa solución.

Nos hemos cruzado el correo por el camino...


Gracias,

Ramsés


Reply to: