LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-27-2007, 10:54 AM   #1
bogidu
LQ Newbie
 
Registered: Feb 2007
Posts: 1

Rep: Reputation: 0
Unhappy Newbie shell script question


I'm wanting to create a command line script that runs ping against each line of the /etc/hosts file.

I think I've got the basics of it but don't have something quite right.

$ for each $i in /etc/hosts
> do
> ping $i
> done

Yes, I know my syntax is wrong, I'm just trying to figure out where I'm screwing this thing up. Any help is appreciated, Thanks.

Bill
 
Old 02-27-2007, 11:25 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
why dont you actually try to run it?

I just did because i was bored.

1) there is no "for each" in shells.
2) the "$" is not needed
3) you cant use it to look into files

back to the drawing board for you! May i recommend using "cat" to extract the lines?
 
Old 02-27-2007, 12:24 PM   #3
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
Quote:
Originally Posted by bogidu
I'm wanting to create a command line script that runs ping against each line of the /etc/hosts file.

I think I've got the basics of it but don't have something quite right.

$ for each $i in /etc/hosts
> do
> ping $i
> done

Yes, I know my syntax is wrong, I'm just trying to figure out where I'm screwing this thing up. Any help is appreciated, Thanks.

Bill
for i in $(cat /etc/hosts)

should work for reading the hosts file, but then you will need to use cut/grep/sed to get rid of the excess information (i.e. comments, hostnames and so on). I think you'll also need to set IFS like this before the for-loop:

IFS=$'\x0A'$'\x0D'

Remember to unset it after the loop.

Last edited by alienDog; 02-27-2007 at 12:29 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script newbie question hold_breal Programming 2 09-13-2006 08:25 AM
newbie help on small shell script ripcurl Programming 5 09-20-2005 03:17 PM
Shell script newbie needs help! allididntwantto Programming 2 04-21-2005 05:53 AM
veprory simple but frustrating for a newbie shell script question marigb Linux - Newbie 11 04-28-2004 12:11 PM
shell script newbie question yoshi95 Programming 1 03-11-2004 02:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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