Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 28, 2001

Shell Scripting 101, Lesson 9

RSS
Subscribe to Windows IT Pro | See More Batch Files Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

By default, the command interpreter (aka the command processor) reads and executes each line in a script, from top to bottom. When you write scripts, you sometimes need to change the order in which the command interpreter executes the code. In other words, you need to change the flow of the script. You can control a script’s flow with the Goto, Call, and Exit commands. To understand these commands, you first need to be familiar with labels.

Labels
A label marks a spot to which you want the command interpreter to jump. After the command interpreter jumps to that spot, it starts executing the code downward again. The label’s syntax is

:Label1

where Label1 is the label’s name preceded by a colon (:). You can specify any name, except for EOF (more on this exception later). As I mentioned in "Shell Scripting 101, Lesson 5,", you can embed spaces in the label but you can’t include commas, semicolons, or other separators. Technically, the label can be any length. However, the command interpreter reads only the first eight characters, which can cause problems if you use several labels that have similar long names. For example, if you have the labels :MyLabel01 and :MyLabel02, the command interpreter reads both labels as :MyLabel0, which can cause errors in a script.

As the code

:Fred

Echo Fred was here.

shows, the label needs to be the sole element on the line. In other words, you can’t have commands on the same line as the label. Code such as

:Fred Echo Fred was here.

causes an error. You can have as many labels as you want in a script. You use labels with the Goto and Call commands.

The Goto Command
The Goto command moves a script’s flow to the specified label. This command’s syntax is

Goto [:]Label1

where Label1 is the name of the label to which you want the command interpreter to jump. For example, in the code that Listing 1 shows, the Goto command tells the command interpreter to jump to the :Fred label. Because the script’s flow jumps from the first line to the third line, the command interpreter never executes the Echo command in the second line.

In the Goto command’s syntax, you might have noted that the colon is in square brackets. Square brackets denote an optional element, which means preceding the label’s name with a colon is optional (except for EOF, which I’ll discuss shortly) in the Goto command. However, because the colon is mandatory in the actual label, including the colon in any label reference is a good habit to get into.

The Call Command
As you’ve just seen, the Goto command tells the command interpreter to go from the current location (point A) to another location (point B), then perform point B’s tasks. The command interpreter doesn’t return to point A when it’s finished with those tasks. Like the Goto command, the Call command tells the command interpreter to go from point A to point B, then perform the tasks. However, unlike the Goto command, the Call command tells the command interpreter to return to point A when it’s finished with those tasks.

For example, in the code that Listing 2 shows, the Call command tells the command interpreter to jump to the :Fred label and display the message Fred was here. The command interpreter then returns to the point from which it made the jump and displays the message Wilma was here.

Another difference between the Call and Goto commands is that you can use the Call command to execute another .bat or .cmd script from within the current script. If you want to call another script, you use the syntax

Call [drive:][path] filename [arguments]

where filename is the name of the second script. If the second script isn’t in the same directory as the current script, you need to specify the drive and path along with the filename. In the arguments parameter, you specify any command-line arguments that the second script needs to run.

If you want to execute code that lies within the current script (e.g., Listing 2), you use the Call command with a label. In this case, the syntax is

Call :Label1 [arguments]

where Label1 is the name of the label that marks the location of the code to execute. Note that the colon preceding the label name isn’t in parentheses. Unlike the Goto command, the Call command requires you to precede the label’s name with a colon. Because the code that follows a label can be a subroutine, the Call command syntax includes the arguments parameter. You use this parameter to provide any command-line arguments that a subroutine might need to run.

If you use the Call command to jump to and execute a subroutine, the command interpreter assumes that the subroutine ends at the end of the file (i.e., with the last command in the script). If the subroutine ends elsewhere, you can use an :EOF label to specify the subroutine’s end.

The :EOF Label and the Exit Command
The :EOF label is a special built-in label that simulates the End of File (EOF). Because :EOF is a built-in label, you can’t use the name EOF for any of your labels.

When you use the :EOF label with the Goto command, you can’t leave out the colon. The Goto :EOF command tells the command interpreter to jump to the end of the script. Let’s look at this command in action. Suppose that you want to delete the file fred.txt, but you’re not sure whether the file is on the C or D drive. As Listing 3 shows, you can write code that uses the If Exist command to test whether the file fred.txt is on the C or D drive. (If you’re unfamiliar with the If Exist command, see "Shell Scripting 101, Lesson 6.") If the file is on the C drive, the command interpreter jumps to the :Delthemc label, deletes the file, and jumps to the end of the script. If the file is on the D drive, the command interpreter jumps to the :Delthemd label, deletes the file, and jumps to the end of the script.

Listing 4 shows another way in which you can write this code. Instead of using the Goto :EOF command, you can use the Exit command. The Exit command quits that session of the command interpreter. Listing 1 and Listing 2 produce identical results.

Homework Assignment
1. Use labels to modify Listing 1 in "Shell Scripting 101, Lesson 5". Move the script’s flow to a new label and section of code when the counter reaches values 6 through 10.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: 4th of July Special Edition

An often irreverent look at some of the week's other news, including a shortened work week thanks to the 4th of July, expensive Windows 7 pricing, Bing's modest monthly gains, IE 8 heading to work, Steve Jobs back at Apple, and so much more ...

Google Wave Emulates Trends of Changing World

As collaboration continues to increase, the world and how individuals view information is evolving. What does that mean for IT? ...


Related Articles Programmatically Change Permissions in Print Clusters

Shell Scripting 101, Lesson 10

Shell Scripting 101, Lesson 8

Shell Scripting 101, Lesson 7

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events WinConnections and Microsoft® Exchange Connections

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Test Drive IT Solutions and Get Free Music Downloads
Solve your toughest IT problems with these free downloads and receive 5 free music downloads!


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home asp.netPRO Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement | Reprints and Licensing