LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-12-2021, 01:32 PM   #1
Masiel
LQ Newbie
 
Registered: Feb 2021
Posts: 5

Rep: Reputation: Disabled
Loop with user input


Hi! I am very new to this and I am reaching a roadblock that I have no idea how to get around. I am trying to create a script that will prompt the user to enter First Name, Last Name, City until exit is entered in the First Name. Displayed like this

First Name: Jane
Last Name: Doe
City: Dayton

First Name: John
Last Name: Smith
City: Cincinnati

First Name: exit

Jane Doe Dayton
John Smith Cincinnati


So far I have this:

until [[ $Fname = "exit" ]];
do

read -p 'First Name: ' Fname
read -p 'Last Name: ' Lname
read -p 'City: ' Cname

echo $Fname $Lname $Cname


However, its not stopping after First Name: exit, it continues to Last Name, City, then it echo, but only the second pass of the loop. How do I get it to echo both passes? And how do I get it to stop after exit is entered?

This is part an of assignment for class, and we are like 3 weeks in, so I am very new. If that info is needed. Thanks so much for your help!

Last edited by Masiel; 02-12-2021 at 03:58 PM.
 
Old 02-12-2021, 01:48 PM   #2
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
The Fname is the first entry and does not get tested until the loop completes. Put a test of the Fname value with an exit statement right after the Fname is entered. This way it will exit the loop immediately rather than continuing the loop to the end

Also, for every "do" it needs to end with a "done"

One quick reference for use now and later!
https://tldp.org/guides.html

Last edited by computersavvy; 02-12-2021 at 01:52 PM.
 
1 members found this post helpful.
Old 02-12-2021, 02:10 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Code:
while :; do
    read -p "Enter first name :" fname
    if [ "$fname" = "exit" ]; then
        exit
    fi
    read -p "Enter last name :" lname
    read -p "Enter city name :" cname
    echo ""$fname" "$lname" "$cname""
done
 
1 members found this post helpful.
Old 02-12-2021, 03:53 PM   #4
Masiel
LQ Newbie
 
Registered: Feb 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks!

However, that code will echo right after each first name: last name: city: block, instead of at the end and reflecting both sets. With exit set there, it exits automatically when "exit" is entered for first name: and doesn't echo.
 
Old 02-12-2021, 03:57 PM   #5
Masiel
LQ Newbie
 
Registered: Feb 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Oh, I also just noticed I made a mistake with the final output. I edited original post to reflect it. It is supposed to look like this:

First Name: Jane
Last Name: Doe
City: Dayton

First Name: John
Last Name: Smith
City: Cincinnati

First Name: exit

Jane Doe Dayton
John Smith Cincinnati

Last edited by Masiel; 02-12-2021 at 03:59 PM.
 
Old 02-12-2021, 04:40 PM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Replace exit with break and read each set of data into an array element. Then, after break from the while loop, loop over all the array elements and print them.
Code:
#!/bin/bash
declare -a data
while :
do
  read -p 'First name: ' fname
  [[ $fname == 'exit' ]] && break
  read -p 'Last name: ' lname
  read -p 'City: ' cname
  data+=("$fname $lname $cname")
  echo
done
echo
for i in "${data[@]}"
do
  echo "$i"
done
 
1 members found this post helpful.
Old 02-12-2021, 04:58 PM   #7
Masiel
LQ Newbie
 
Registered: Feb 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks! And thanks for the explanation. That worked!
 
Old 02-12-2021, 04:59 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Yes, break is correct.
It ends the loop; the script continues after the loop.

An alternative:
Code:
until
  read -p 'First Name: ' Fname
  [[ $Fname == "exit" ]]
do
  read -p 'Last Name: ' Lname
  read -p 'City: ' Cname

  echo $Fname $Lname $Cname
done
The last statement before the do gives the exit status that matters.
 
2 members found this post helpful.
Old 02-12-2021, 05:39 PM   #9
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by Masiel View Post
Oh, I also just noticed I made a mistake with the final output. I edited original post to reflect it. It is supposed to look like this:

First Name: Jane
Last Name: Doe
City: Dayton

First Name: John
Last Name: Smith
City: Cincinnati

First Name: exit

Jane Doe Dayton
John Smith Cincinnati
You then need to build an array of responses and echo the array at the end. Read the first document in the link I sent earlier on how to do that. I doubt what you echo at the end should contain the exit command.

This might do what you want
Code:
#!/bin/bash

((count=1))

while :; do
    read -p "Enter first name :" fname
    if [ "$fname" = "exit" ]; then
        break
    fi
    data1[$count]=$fname
    read -p "Enter last name :" lname
    data2[$count]=$lname
    read -p "Enter city name :" cname
    data3[$count]=$cname
    (( count++ ))
done


if [ $count -gt 1 ]; then
    num=1
    while [ $num -lt $count ]; do
       echo "${data1[$num]}  ${data2[$num]}    ${data3[$num]}"
       (( num++ ))
   done
else
   echo "No data entered"
fi
Again, read up on what each of those constructions do to really learn. Copying does nothing to assist in learning.
 
1 members found this post helpful.
Old 02-12-2021, 06:00 PM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
A POSIX shell solution (incorporating MadeInGermany's suggestion)
Code:
#!/bin/sh
set --
until
  read -p 'First name: ' fname
  [ 'exit' = "$fname" ]
do
  read -p 'Last name: ' lname
  read -p 'City: ' cname
  set -- "$@" "$fname" "$lname" "$cname"
  echo
done
echo
while [ $# -gt 0 ]
do
  echo "$1" "$2" "$3"
  shift 3
done
 
1 members found this post helpful.
Old 02-12-2021, 06:10 PM   #11
Masiel
LQ Newbie
 
Registered: Feb 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Computersavvy, thanks for your responses, and your link. It’s going to be a great resource. I love the different codes people have suggested so I can compare and learn how it works. Everyone’s codes and suggestions have been so helpful. Thanks everyone.
 
Old 02-12-2021, 06:20 PM   #12
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
You notice there are many ways to do the same thing. Use what works for you and enjoy.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
hang script (trap) for user input in a loop with ctrl and any other letter pedropt Programming 4 12-10-2017 04:06 PM
BASH - read user input into array in loop klmn Programming 2 02-26-2015 10:07 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Stopping a while loop from user input 454redhawk Programming 4 01-15-2008 06:39 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration