How to display the longest line from user input

Anastasia Netz

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

anubhava

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can someone explain how to store and display a line count with user input?

From Dev

How to display input button from two different form in single line

From Dev

How to get longest line from a file?

From Dev

How to get longest line from a file?

From Java

How to display message based on user input from scanned file

From Dev

How to display a calculation from user input in android studio

From Dev

Unsure how to get user input from a multiple line loop - Python

From Dev

How to display the user input in a panel

From Dev

Function to Find longest word in wordlist created from user input

From Dev

How can I display the longest line entered into a Bash prompt?

From Dev

Display second longest line in MATLAB?

From Dev

Display input from user, using toString() Method

From Dev

If line from file equals user input

From Dev

How to display label, input and image in a single line?

From Dev

How to display input buttons on one line in Bootstrap?

From Dev

How to display input from TinyMCE?

From Dev

How to display input back to the user on an HTML page?

From Dev

How to display user input with a Javascript function

From Dev

How to display user input in an Alert? (SWIFT)

From Dev

How to get user input in same line

From Dev

Take In Input from User and Display the same input on the same page

From Dev

edit a file line by line interactively from user input in python

From Dev

How am I able to display the largest and the smallest number from the array of the user's input?

From Dev

how to display the sum of 5 input numbers from user using c language

From Dev

C++: How to redirect from a file to cin and display as if user typed the input

From Dev

How do I get a cookie from user input, and display it on an html element

From Dev

How do I efficiently read in a single line of user input from the console?

From Dev

How do I efficiently read in a single line of user input from the console?

From Dev

How to use Scanner to get an object input from user through command line

Related Related

  1. 1

    Can someone explain how to store and display a line count with user input?

  2. 2

    How to display input button from two different form in single line

  3. 3

    How to get longest line from a file?

  4. 4

    How to get longest line from a file?

  5. 5

    How to display message based on user input from scanned file

  6. 6

    How to display a calculation from user input in android studio

  7. 7

    Unsure how to get user input from a multiple line loop - Python

  8. 8

    How to display the user input in a panel

  9. 9

    Function to Find longest word in wordlist created from user input

  10. 10

    How can I display the longest line entered into a Bash prompt?

  11. 11

    Display second longest line in MATLAB?

  12. 12

    Display input from user, using toString() Method

  13. 13

    If line from file equals user input

  14. 14

    How to display label, input and image in a single line?

  15. 15

    How to display input buttons on one line in Bootstrap?

  16. 16

    How to display input from TinyMCE?

  17. 17

    How to display input back to the user on an HTML page?

  18. 18

    How to display user input with a Javascript function

  19. 19

    How to display user input in an Alert? (SWIFT)

  20. 20

    How to get user input in same line

  21. 21

    Take In Input from User and Display the same input on the same page

  22. 22

    edit a file line by line interactively from user input in python

  23. 23

    How am I able to display the largest and the smallest number from the array of the user's input?

  24. 24

    how to display the sum of 5 input numbers from user using c language

  25. 25

    C++: How to redirect from a file to cin and display as if user typed the input

  26. 26

    How do I get a cookie from user input, and display it on an html element

  27. 27

    How do I efficiently read in a single line of user input from the console?

  28. 28

    How do I efficiently read in a single line of user input from the console?

  29. 29

    How to use Scanner to get an object input from user through command line

HotTag

Archive