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

245
Visualizações
Shell scripting - inputting 4 lines from the user

I have a question. I need to read the user input, but I need the user enter 4 lines. Everytime time user presses enter it terminates the read command, so I did read commands 4 times (not sure if this is correct) - I need help with this. Also how do I output user's input into file? and that output needs to me 4 lines as well. Please see my script. I appreciate any help.

1  #!/bin/bash


2  #Displaying user name, host name, time, and date

3  Time=`date +%r`
4  Date=`date +%m/%d/%Y`
5  echo "$USER is running this script on $HOST at $Time on $Date:"
6  echo""
7  echo  "Please enter 4 lines of text:  "
8  read line1
9  read line2
10 read line3
11 read line4
12 echo "I'm now putting the four lines you entered into a text file called \"mylines.txt\"..."
13 echo $line1\n $line2\n $line3 \n$line4 > lines.txt
14 echo""
15 echo "The lines you entered were:\n$line1\n$line2\n$line3\n$line4"
16 echo""
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To read 4 lines you can use a loop and store it in an array:

#!/bin/bash

lines=()
echo  "Please enter 4 lines of text:  "
for ((i=1; i<=4; i++)); do
   IFS= read -p "Enter line $i: " -r line && lines+=("$line")
done

Then to print and redirect it:

printf "%s\n" "${lines[@]}" > lines.txt
over 4 years ago · Santiago Trujillo Relatório

0

I'm not sure if a get what you want... but see this peace of code

#!/bin/bash

myfile="file"

echo "a line im my file" > $myfile
echo "now a erased my file and create another with the same name" > $myfile
echo "now I'm writing at the end of my file" >> $myfile
echo "



now I put some lines in the end of my file" >> $myfile

if what you want is to put some lines into your file you may do:

echo $line1 > lines.txt
echo "



" + $line2 >> lines.txt
echo "



" + $line3 >> lines.txt
echo "



" + $line4 >> lines.txt

if it's only that, this sould do ;)

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