LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-30-2011, 11:18 AM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
For each keyboard input


Is there way that we can do like
Quote:
for each "keyboard input variable"
do
something
done
I have keyboard inputs stored in variables and I wanted to something for each variable.
for example
Quote:
echo -n "Enter IP1: "
raed ip1
echo -n "Enter username: "
read uname
echo -n "Enter IP2: "
read ip2
for ip1&2 do
ping
done
Here I have 3 variables and I wanted to pick only certain set like ip1 and ip2 and then ping those servers.
Can we do that without saving those variables in a file?

Thanks
 
Old 03-30-2011, 11:42 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You can have multiple inputs in a loop. That's what it's designed for. It doesn't matter if the input is a fixed string, a variable, or a command substitution. The shell expands everything on the line before running the main command.
Code:
for ip in "$ip1" "$ip2"; do 
   ping $ip
done
The loop just takes everything after the word "in", does word splitting on them, places each "word" it finds in the first variable one at a time and processes the subsequent commands.

Note that this can cause problems if a variable contains spaces. Use quotes around anything that needs to be grouped together as a single unit.

Here are a few useful bash scripting references.
I highly recommend the first one for shell beginners:

http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html

Last edited by David the H.; 03-30-2011 at 11:49 AM. Reason: removed mistake
 
Old 03-30-2011, 01:17 PM   #3
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks that did work. I was not sure if we can do like
Quote:
for ip in "$ip1" "$ip2"; do
Thanks much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
xf86-input-keyboard and xf86-input-mouse masked CollieJim Gentoo 4 11-09-2009 09:57 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
No keyboard input under gnome. gatdrydock Linux - Software 1 07-10-2005 03:15 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
No keyboard input seen neo77777 Linux - General 5 07-12-2002 09:32 PM

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

All times are GMT -5. The time now is 04:29 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