Unix script to PING a list of devices and output the result to a txt file


Im very new to Unix scripting and I'm having trouble running a basic script.  Id like to create a Unix script to PING a list of devices and output the result to a txt file.

Im trying to pull the list of devices from a txt file and then output the result to a txt file.

here is what I have so far.
#! /bin/bash

for $i in 'cat serverlist'
ping $i >> output.txt

I have tested this on a Cygwin installed on my windows box but i get the following error.

$ ./_Ping.sh
./_Ping.sh: line 2: $'\r': command not found
./.sh: line 4: syntax error near unexpected token `$'do\r''
'/.sh: line 4: `do

Any ideas?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need backticks (not single quotes) to execute a command.  Also ping will continue forever in linux unless you put a count on it.

for i in `cat serverlist`;
ping -c 2 $i >> ping

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Several: problems:

1) Use backticks or $( ) around the "cat" command and don't use "$i" (that's the content of the variable "i") in the "for" line, use "i" alone:

for i in $(cat serverlist)
ping -c 1 $i >> output.txt

2) Your script file contains "carriage return + line feed" at the line ends. This is DOS standard and is wrong in Unix, which uses "linefeed" alone.
If you transferred the script file from Windows via FTP transfer it again using the "ASCII" transfer method, or edit the file in Unix and remove the extra carriage return characters.

3) Limit the number of ping attempts, so it will not run endlessly (see above)
To explain further:

To execute a command in the for loop you have to use backticks (under the tilde on the keyboard) not single quotes.

The command "ping -c 2" tells the system to only ping it twice, otherwise it will go on forever and the script will never stop.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.


If you expect error messages from the "ping" command take care to store these messages in the log file or in a separate error log file instead of displaying them on your terminal:

1) One file:

ping -c 1 $i >> output.txt 2>&1

2) Separate error log:

ping -c 1 $i >> output.txt 2>>errors.txt
tjoconnorAuthor Commented:
Thanks for the responses. I seem to be ok with the file now as there is no error on the command line.

However my output file looks like this

Bad option -c.

Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name
Your ping command does not support the "-c" flag. Which is your OS? HPUX?
It seems that you'll have to use "-n" instead:

ping -n 1 $i >> output.txt 2>>errors.txt
tjoconnorAuthor Commented:

Yes I used -n 3 $I>>..... and it worked

Thanks a million for your help today guys, really appreciated it
You're always welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.