Subscribe to Windows IT Pro
August 01, 2001 12:00 AM

Shell Scripting 101, Lesson 8

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

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Mar 03, 2005

    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.

  • drohbeck
    7 years ago
    Feb 21, 2005

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

  • 0dyssey
    8 years ago
    Oct 12, 2004

    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

  • Udit Budhia
    8 years ago
    Jun 11, 2004

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

  • J. Steegar
    11 years ago
    Aug 14, 2001

    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.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

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