Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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!
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"
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
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.