Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Cómo mostrar la línea más larga de la entrada del usuario

Tengo un script de shell que necesita pedirle al usuario 4 líneas de entrada. Luego, necesito mostrar la línea más larga que se ingresó, y luego toda la entrada debe ir al archivo. Eso es lo que tengo hasta ahora:

 #!/bin/bash lines=() echo "Please enter 4 lines of text: " for ((i=1; i<=4; i++)); do IFS= read -p "" -r line && lines+=("$line") done echo "The longest line you entered was: " max=0 for((i=0;i<4;i++)); do len=${#lines} if [[ len -gt max ]] ; then max=$len long="${lines}" fi done echo longest line="${long}" length="${max}" echo "I'm now putting the four lines you entered into a text file called \"mylines.txt\"..." printf "%s\n" "${lines[@]}" > lines.txt

Esto no me está pasando, ¿puedes decirme qué estoy haciendo mal? Gracias

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Puede averiguar la línea más larga y la longitud en el primer bucle for:

 #!/bin/bash lines=() max=0 echo "Please enter 4 lines of text: " for ((i=1; i<=4; i++)); do IFS= read -r line lines+=("$line") [[ ${#line} -gt $max ]] && { max=${#line}; long="$line"; } done echo longest line="${long}" length="${max}" echo "I'm now putting the four lines you entered into a text file called \"mylines.txt\"..." printf "%s\n" "${lines[@]}" > lines.txt
over 4 years ago · Santiago Trujillo Denunciar

0

Usando su ejemplo exacto, solo necesitaría recorrer la matriz especificando un índice $ i al configurar sus variables len y long.

 #!/bin/bash lines=() echo "Please enter 4 lines of text: " for ((i=1; i<=4; i++)); do IFS= read -p "" -r line && lines+=("$line") done echo "The longest line you entered was: " max=0 for((i=0;i<4;i++)); do #See how I added the [$i] this will allow you to get the length of each item in the array len=${#lines[$i]} if [[ len -gt max ]] ; then max=$len #This gets the item in the array to set the value of $long to it. long="${lines[$i]}" fi done echo longest line="${long}" length="${max}" echo "I'm now putting the four lines you entered into a text file called \"mylines.txt\"..." printf "%s\n" "${lines[@]}" > lines.txt

Salidas:

 Please enter 4 lines of text: one two three four The longest line you entered was: longest line=three length=5 I'm now putting the four lines you entered into a text file called "mylines.txt"...
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda