Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Some skills of BAT script Writing

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I always thought that bat could not set and call functions like Linux's shell, but in fact I found that it was possible. However, because the batch feature is executed one by one, the function of this label must be written at the end of the text. If you write in front of the batch command, bat will run the command line by line. Write at the end of the document and skip the code before the "function area".

(by the way, this function is written very differently from linux's shell. The command in shell's function must be written in front of the command and let shell run line by line before it can be called in subsequent commands. )

The writing of the function:

In this example, a function I'm trying to write is named testfunction, and its operation is to run an echo command.

:: use the call command, and write the function name after it, and write one in front of the function name: indicates that this is a label.

Call: testfunction

:: this goto command causes the batch to skip all functions at run time.

Goto EOF

: starting with the following line are the parts of the function, which should be the last part of the bat script, and theoretically should not write any more commands below. First of all, like goto's command, first write a lable.

: testfunction

Echo Test function is running.

Rem exit / b causes the program to jump to the command that called the function instead of exiting the program directly.

Exit / b

: EOF

:: that's what the sample script is all about.

Of course, you can not write the content of the function in this bat, but write another bat file, and then call the absolute path of the bat, which is also possible. For my convenience, I temporarily call the script I'm running a.bat and the script I call b.bat.

The content of a.bat is.

Set a=aaa

Call b.bat

Echo% b%

Pause

And the content of b.bat is

Echo a%

Set b=bbb

In these two scripts, script an and script b each define a variable, but the variables defined in the other script are not defined in the other script, but the variables of echo are defined in each other's script. If there are only separate scripts, the echo commands of the two scripts will not return anything. But if you run the a script and call the b script, you can find that the variables between the scripts called by call are interoperable. However, the label of goto between scripts is not interoperable, that is, you cannot goto into a label of b in a script.

And you can't write the call command. If you don't write the path of the b script, bat can still run, but after running the b script, the entire batch exits. For example, if we remove call from call b.bat in script a, the script will only print the value of variable an and then assign the value of variable b, but will not print the value of b again in script a.

Use the set / p command to get the value in another text file

It is written as follows:

Set / p var=

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report