Bat脚本 -(一)- echo/ echo off/ echo on/ @ / start / pause / rem

1. echo

语法: echo [{on|off}] [message]

ECHO [ON | OFF]   打开回显或关闭回显功能。
ECHO              显示当前回显设置。
ECHO [message]    显示信息。
:: echo回显默认开启,会打印命令行与结果
echo this is test 1
:: echo回显关闭命令,此条命令与结果会被打印
echo off
:: echo回显已关闭,只会打印结果
echo this is test 2
:: 查看回显设置,只会打印结果
echo
:: 开启回显,只会打印结果
echo on
:: 回显已开启,打印命令与结果
echo this is test 3

pause

执行结果如下:

C:\Users\anjin.ma\Desktop>echo this is test 1
this is test 1

C:\Users\anjin.ma\Desktop>echo off
this is test 2
ECHO 处于关闭状态。

C:\Users\anjin.ma\Desktop>echo this is test 3
this is test 3

C:\Users\anjin.ma\Desktop>pause

2. @

加在每个命令行的最前面,表示运行时不显示当前行的命令行(关闭当前命令行的回显)。 与 echo off 相似,区别在于仅影响当前行。

echo this is test 1
:: 不会回显
@echo this is test 2

echo this is test 3

pause

执行结果如下:

C:\Users\anjin.ma\Desktop>echo this is test 1
this is test 1
this is test 2

C:\Users\anjin.ma\Desktop>echo this is test 3
this is test 3

C:\Users\anjin.ma\Desktop>pause
@echo off 这是个非常常用的用法,放在bat文件的开头,关闭所有回显,包括自己

3. start

调用脚本,并可以传递参数

4. pause

脚本执行完后,不退出cmd窗口,这个很有用!

会暂停批处理的执行并在屏幕上显示 Press any key to continue...的提示,等待用户按任意键后继续

5. 注释

bat脚本的注释是比较多的:

1、:: 注释内容(第一个冒号后也可以跟任何一个非字母数字的字符)
2、rem 注释内容(不能出现重定向符号和管道符号)
3、echo 注释内容(不能出现重定向符号和管道符号)〉nul
4、if not exist nul 注释内容(不能出现重定向符号和管道符号)
5、: 注释内容(注释文本不能与已有标签重名)
6、%注释内容%(可以用作行间注释,不能出现重定向符号和管道符号)
7、goto 标签 注释内容(可以用作说明goto的条件和执行内容)
8、:标签 注释内容(可以用作标签下方段的执行内容)

6. echo >  与 echo >>

echo > : 将字符写到 > 后面指示的路径文件中

echo >>:   将字符追加到 >> 后面指示的路径文件中

@echo off
echo 1 > D:/test.txt
echo 1 > D:/test.txt

echo 1 >> D:/test1.txt
echo 1 >> D:/test1.txt
pause

test.txt文件中只会有1个1,而test1.txt会有两个1,是以追加的形式进去的。

7. :标签与goto的神奇配合

:begin 可以定义一个标签, goto可以调到标签处开始执行。

@echo off
:begin
echo 1 >> D:/test.txt
echo this is one test
pause
goto begin

以上的bat脚本执行后,会一直循环往文件中追加1,且会循环打印 "this is one test"。

this is one test
请按任意键继续. . .
this is one test
请按任意键继续. . .
this is one test
请按任意键继续. . .
this is one test
请按任意键继续. . .
this is one test
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/maihilton/article/details/82590220