elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Curso de javascript por TickTack


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  script que ordena un texto ascendentemente o descendentemente [bash]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: script que ordena un texto ascendentemente o descendentemente [bash]  (Leído 4,780 veces)
minette1988

Desconectado Desconectado

Mensajes: 53


Ver Perfil
script que ordena un texto ascendentemente o descendentemente [bash]
« en: 7 Mayo 2010, 12:38 pm »

Hola, el siguiente script recibe como primer parámetro un archivo de texto y ordena las líneas de dicho archivo ascendentemente al pasarle una "A" como segundo parámetro o descendentemente al pasarle una "Z". Cuando lo ejecuto me sale esto: [: 16: missing ]

Código
  1. #!/bin/bash
  2.  
  3. if [ $# -ge 2 ]
  4. then
  5.     if [ -f $1 ] && [ $2 = "A"]
  6.     then
  7.        `cat $1 | sort -d#`
  8.     else
  9.         if [ -f $1 ] && [ $2 = "Z" ]
  10.         then
  11.            `cat $1 | sort -r`
  12.         fi
  13.     fi
  14. else
  15.    echo "Error: Falta pasar argumentos"
  16. fi  


En línea

minette1988

Desconectado Desconectado

Mensajes: 53


Ver Perfil
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #1 en: 7 Mayo 2010, 12:53 pm »

El archivo de texto a ordenar es este:
ayer fui al estadio.
bonitas ideas las que tú tienes.
entro al segundo palo y gol.

Acabo de corregir un pequeño fallo, pero me da el siguiente error: script1: 16: ayer: not found
Código
  1. #!/bin/bash
  2.  
  3. if [ $# -ge 2 ]
  4. then
  5.     if [ -f $1 ] && [ $2 = "A" ]
  6.     then
  7.        `cat $1 | sort -d`
  8.     else
  9.         if [ -f $1 ] && [ $2 = "Z" ]
  10.         then
  11.            `cat $1 | sort -r`
  12.         fi
  13.     fi
  14. else
  15.    echo "Error: Falta pasar argumentos"
  16. fi  


En línea

leogtz
. . .. ... ..... ........ ............. .....................
Colaborador
***
Desconectado Desconectado

Mensajes: 3.069


/^$/


Ver Perfil WWW
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #2 en: 7 Mayo 2010, 23:10 pm »

Me pregunto para qué utilizas `` si no te lo piden, es decir, no necesitas sustituir variables, solo ejecuta y ya, mira esto:

Tu script funciona bien así:

Código
  1. #!/bin/bash
  2.  
  3. if [ $# -ge 2 ]
  4. then
  5.     if [ -f $1 ] && [ $2 = "A" ]
  6.     then
  7.        cat $1 | sort -d
  8.     else
  9.         if [ -f $1 ] && [ $2 = "Z" ]
  10.         then
  11.            cat $1 | sort -r
  12.         fi
  13.     fi
  14. else
  15.    echo "Error: Falta pasar argumentos"
  16. fi

Código
  1. leo@lein:~/Escritorio$ bash shell.sh file.txt A
  2.  
  3. ayer fui al estadio.
  4. bonitas ideas las que tú tienes.
  5. entro al segundo palo y gol.
  6. leo@lein:~/Escritorio$ bash shell.sh file.txt Z
  7. entro al segundo palo y gol.
  8. bonitas ideas las que tú tienes.
  9. ayer fui al estadio.
  10.  
  11. leo@lein:~/Escritorio$
  12.  
En línea

Código
  1. (( 1 / 0 )) &> /dev/null || {
  2. echo -e "stderrrrrrrrrrrrrrrrrrr";
  3. }
  4.  
http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com
minette1988

Desconectado Desconectado

Mensajes: 53


Ver Perfil
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #3 en: 8 Mayo 2010, 13:55 pm »

Tenías razón, muchas gracias. Una pregunta al ejecutar un script ¿también se cuenta como parámetro el nombre del script?. Gracias.
En línea

leogtz
. . .. ... ..... ........ ............. .....................
Colaborador
***
Desconectado Desconectado

Mensajes: 3.069


/^$/


Ver Perfil WWW
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #4 en: 8 Mayo 2010, 18:57 pm »

No te entiendo qué quieres decir con eso de "se cuenta", explicame, por favor.
En línea

Código
  1. (( 1 / 0 )) &> /dev/null || {
  2. echo -e "stderrrrrrrrrrrrrrrrrrr";
  3. }
  4.  
http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com
cgvwzq

Desconectado Desconectado

Mensajes: 57


Agente P.


Ver Perfil WWW
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #5 en: 8 Mayo 2010, 19:34 pm »

Se refiere a que si el contador "$#" tiene en cuenta el nombre del script.

Puedes comprobarlo facilmente...

Código
  1. if [ $# = 1 ]; then
  2.   echo "Lo cuenta"
  3. fi

Y lo ejecutas sin paso de parametros: "sh prueba.sh".

Edito: Si se tiene en cuenta, es la variable $0.
« Última modificación: 8 Mayo 2010, 19:41 pm por cgvwzq » En línea

Some stuff:

  • www.a] parsed as ]www.a]
  • Bypass elhacker's img filter with ALT attribute!
  • ¿Para cuándo SQLi I y II? WZ


leogtz
. . .. ... ..... ........ ............. .....................
Colaborador
***
Desconectado Desconectado

Mensajes: 3.069


/^$/


Ver Perfil WWW
Re: script que ordena un texto ascendentemente o descendentemente [bash]
« Respuesta #6 en: 8 Mayo 2010, 20:06 pm »

Pues NO, no lo tiene en cuenta:

Código:
leo@lein:~/Escritorio$ cat shell.sh 
#!/usr/bin/bash
echo -e "$#";


leo@lein:~/Escritorio$ bash shell.sh
0
leo@lein:~/Escritorio$

Como  argumento posicional el mismo archivo ($0) no es tomado en cuenta.
« Última modificación: 8 Mayo 2010, 20:29 pm por Leo Gutiérrez. » En línea

Código
  1. (( 1 / 0 )) &> /dev/null || {
  2. echo -e "stderrrrrrrrrrrrrrrrrrr";
  3. }
  4.  
http://leonardogtzr.wordpress.com/
leogutierrezramirez@gmail.com
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[ VBS ] Script para recitar texto en voz castellana?
Scripting
SuperDraco 3 12,201 Último mensaje 28 Marzo 2011, 21:02 pm
por SuperDraco
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines