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

246
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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