Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to display the longest line from user input

I have a shell script that need to ask the user for 4 lines of input. Then I need to display the longest line that was entered, and then whole input is gotta go into the file. That is what i got so far:

#!/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

This is not happening for me, can you tell me what am I doing wrong? Thanks

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can figure out longest line and length in first for loop:

#!/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 Relatório

0

Using your exact Example you would just need to actually loop through the array by specifying an index $i when setting your len and long variables.

#!/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

Outputs:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda