LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2015, 08:42 AM   #1
klmn
LQ Newbie
 
Registered: Oct 2014
Posts: 3

Rep: Reputation: Disabled
BASH - read user input into array in loop


Dear all,

Please help/advice - I want to limit user input errors when providing needed information, so I'm trying to write a script that works like a dialog:

Code:
Script asks: Tell me how many hosts you have ?
User answer: 5
Then script should ask user for input and store it into variable/array, depending on how many hosts there are:

Code:
Script: "Please provide host name nr 1:" hostname1 <enter>
Script: "Please provide host name nr 2:" hostname2 <enter>
[...]
Script: "Please provide host name nr 5:" hostname5 <enter>
To ensure that everything is OK, script should print short summary like:

Code:
Script: "OK - you have provided <number of hosts> hosts: <hostname1>, <hostname2>.[...].<hostname5>".
Any ideas ? :-)
 
Old 02-26-2015, 09:10 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Code:
  echo 'Tell me how many hosts you have ?'
  echo -n ' : '
  read COUNTHOSTS
then you need a loop from 0 to $COUNTHOSTS
(check the while sample)
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

and put them into an array
http://tldp.org/LDP/Bash-Beginners-G...ect_10_02.html
 
Old 02-26-2015, 10:07 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I am curious ... you went to google (or your favourite search engine) and typed in 'ask user for input in bash' and 'how to loop in bash' (or similar) and absolutely none
of the replies you looked at could tell you any of this information???

At least try and make some effort.
 
  


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
bash script to read multiple user input prasunjit Programming 1 03-31-2013 02:09 PM
[SOLVED] Perform an action on a list/array of variables from user input in bash eamesj Programming 1 12-30-2012 10:22 PM
Using read to prompt for editable user input in Bash 3 woodson2 Programming 4 11-28-2012 11:18 AM
Bash - Read user input: URLs without the enter key stroke make Programming 2 09-23-2010 04:29 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

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