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