August 01, 2001 08:01 PM

Shell Scripting 101, Lesson 8

Rating: (0)
Windows IT Pro
InstantDoc ID #21984

The For command is a powerful shell scripting command because it’s versatile. Let’s look at some common uses of the For command. Specifically, let’s look at how to use the For command with the /d switch and with the /f switch. Along the way, you’ll learn about the Ping command.

Using the For /d Command
Suppose you want to test the connections to five file servers (i.e., fileserv1, fileserv2, fileserv3, fileserv4, and fileserv5). If you’ve installed the TCP/IP protocol in your network, you can use the Ping command to verify the connections. The Ping command’s syntax is

Ping computername

where computername is the name of the server you want to ping. By default, the Ping command sends up to four ping packets to the computer. Pinging five file servers up to four times can waste time and bandwidth, so let’s ping each server only once. To change the default number of ping packets, you use the –n parameter, which lets you specify the number of ping packets to send. To ping each server once, you can use the code

Ping -n 1 fileserv1
Ping -n 1 fileserv2
Ping -n 1 fileserv3
Ping -n 1 fileserv4
Ping -n 1 fileserv5

To shorten this code from five lines to one line, you can use the For command. The For command has different syntaxes for different purposes. In this case, the syntax to use is

For [/d] %variable in (set) Do command

You use this particular syntax to iterate through a set of files or directories and perform a command for each file or directory in that set. Let’s look at the individual segments in this syntax.

For [/d]. When you use the For command, the command processor assumes you want to iterate through files by default. To tell the command processor you want to iterate through directories, you include the optional /d switch. (In syntax, placing a switch or parameter in square brackets means it’s optional.) In the For command for the ping test, you need to include the /d switch.

ARTICLE TOOLS

Add a Comment

FYI: Sometime the input file contains commas and spaces and other natural delimiters. I needed to set the carriage return as my "delim=". What I found is that this can be done by setting any character that is NOT present in the input file as the delimiter will cause the CR to act as the delim.

Anonymous User 3/3/2005 3:31:40 PM


does anyone know how to set the delimiter to the sign " ?

drohbeck2/21/2005 8:06:17 AM


Superb.. Has really helped but I wanted to know if it is possible if I have a file like below...
xxxxxx,x
xxxxxx,x
xxxxxx,x
Is it possible to test for the value of the ',x' in the file so that I can run an if comment on it


0dyssey10/12/2004 3:38:13 AM


The tutorial was great..
But I had a quick question. How do you implement a for loop with multiple statements inside it.

Udit Budhia 6/11/2004 2:03:34 PM


Great tutorial... fast, quick and dirty. Has enough detail and code to speed learning, but not enough to make the articles too heavy. I have done all 8 and am very impressed.

J. Steegar 8/14/2001 2:42:11 AM


You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS

eBooks

Playbook for a Virtualized Datacenter | During challenging times, optimizing the IT infrastructure becomes imperative. Many organizations are looking to extend their virtualization efforts to encompass the entire datacenter. Get a step-through of your technology options and more.

White Papers

As the advances in USB devices have made them invaluable to most business users’, they have also exposed organizations to enormous risks. Learn how to easily enforce device/port control and data encryption policies without requiring new infrastructure and additional admin overhead.

WEB SEMINARS

Is Flexible Lockdown Possible? Join Darren Mar-Elia for this free web webinar exploring the various methods you should consider to protect and control your desktops while also understanding the impact on the end user community.
View Seminar On-Demand.

eLearning Series

We bring the experts direct to you to share their real-world perspective, experience, and expertise. During each event, three sessions stream in real time, allowing you to learn, to ask questions, and to get solutions.
Upcoming event: Windows 7 Deployment

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.