VBScript学习笔记(一)

0x00 简介

VBScript是一种脚本语言,而脚本语言是一种轻量级的编程语言。

VBScript是微软的编程语言Visual Basic的轻量级的版本。

0x01 变量

变量:必须以字母开头,不能包含点号,不能超过255个字符。

变量的生存期:

1.变量的生存期是指它可以存在的时常。

2.当在一个子程序中声明变量时,变量只能在此程序内进行访问。当退出此程序时,变量也会失效。这样的变量称为本地变量。因此可以在不同的程序中使用相同名称的本地变量,因为每个变量只能在声明它的程序内得到识别。

3.如果在程序以外声明了一个变量,那么在页面上的所有的程序都可以对这个变量进行访问,而这类变量的生存期始于它们被声明的时候,而终止于页面关闭的时候。

注:该语言对于大小写不敏感。

在Windows系统中,使用txt文档便能写代码,当然后缀要由txt改为vbs。其中在VBScript中,dim name1,name2,...就是定义变量的,dim会自动识别变量的类型。

demo.vbs

dim name,age
name="an"
age="18"
msgbox name
msgbox age

保存之后,双击该文件便能看到两个弹框。

demo1.vbs

dim name1
name1=inputbox("input your name")
msgbox name1

运行之后,会弹出一个输入框请求输入,然后输出先前输入的内容。

0x02 常量

定义常量的方式:const name1=value,name2=value,...

对于常量就不多bb了,学过编程语言的,都清楚。

demo2.vbs

dim s,r
const pi=3.14
r=inputbox("Please enter the radius")
s=pi*r*r
msgbox s

我这里有点问题就是在inputbox中出现中文便会报错或是乱码,这个问题以后再解决,暂时用英语,权当练习英语。

0x03 注释及数学运算

在VBS中,注释代码或文字有两种方式,一是在代码或文字之前添加rem,二是在代码或文字之前添加一个单引号。

数学运算大体上和python差不多,单纯就是关键字变了而已。

demo3.vbs

dim a,b,c
a=2
b=3
c=a*b+b
msgbox c

输出为9。

补充:

1.在VBS中,取余的方式是用mod,而不是python等语言中的%。

2.在VBS中,将一个分数只取其整数部分,使用int()即可。举一个例子:

demo4.vbs

dim a,b,c
a=2
b=3
c=b/a
msgbox int(c)

那么不使用int()的话,输出的会是1.5,但使用了int()之后,输出的则是1。因为int表示只取其整数部分。

0x04 布尔类型和if判断语句

布尔类型就是false和true的问题。

demo5.vbs

dim a,b
a=10
b=5
msgbox a>b '输出为true
if a>b then
msgbox "a>b" '输出为a>b
end if

demo5中的msgbox a>b就是布尔类型的一种表现。

if的判断语句大致分为

一种可能:

if 条件 then
输出
end if

两种可能:

if 条件 then
输出
else
输出
end if

三种可能:

if 条件 then
输出
else if 条件 then
输出
else
输出
end if
end if

此处有两个end if的原因是因为else if也需要用end if来结束。

demo6.vbs

dim a
a=inputbox("Please enter a number greater than 100")
a=int(a)

if a>100 then
msgbox "correct"
else if a=100 then
msgbox "equal"
else
msgbox "error"

end if
end if

这就是一个简单比大小的程序,只是为了了解if语句。

 

使用if判断语句的易错点:

demo7_1.vbs

dim a,b,c,d
a=inputbox("Please enter a value")
b=inputbox("Please enter a value")
c=inputbox("Please enter a value")

d=a+b

if c=d then
msgbox "correct"
else
msgbox "error"
end if

这串代码,当a=1、b=1、c=11时,会输出correct,也就是正确。因为inputbox默认返回的类型是字符串,而d=a+b便变成了两个字符串的“1”相加,变成了“11”,所以会输出correct。

demo7_2.vbs

dim a,b,c,d
a=inputbox("Please enter a value")
b=inputbox("Please enter a value")
c=inputbox("Please enter a value")

d=a+b
c=int(c)

if c=d then
msgbox "correct"
else
msgbox "error"
end if

这串代码多了一个c=int(c),也就是将c的类型由字符串转变为数值类型。那么demo7_1的输入方式就会报错,因为字符串类型怎么能够和数值类型的作比较,必然是error。

demo7_3.vbs

dim a,b,c,d
a=inputbox("Please enter a value")
b=inputbox("Please enter a value")
c=inputbox("Please enter a value")

d=a*2+b
c=int(c)

if c=d then
msgbox "correct"
else
msgbox "error"
end if

这串代码的区别是d=a+b变成了d=a*2+b,那么如果输入a=2、b=2、c=6时,就会返回correct,因为在vbs中,一旦进行了数学运算,那么返回的类型就是数值类型,而c也被转变为数值类型,所以就会返回correct。

当然在VBS中,if中的判断条件可以用and、or等逻辑运算符,这和python差不多。

0x05 多种条件判断

话不多说,直接例子走起。

demo8.vbs

dim a
a=inputbox("Please enter Numbers")
a=int(a)

select case a
case 1
msgbox "one"
case 2
msgbox "two"
case 3
msgbox "three"
case 4
msgbox "four"

case else
msgbox "error"
end select

这个例子相信只要学过编程的,都大致可以看的懂。

0x06 循环结构do loop

demo9.vbs

do
msgbox "Hello"
loop

运行之后就会循环输出Hello,然后用任务管理器将其结束。

demo10.vbs

dim password
const pass="123"

do
password=inputbox("Please enter your password")
if password=pass then
msgbox "success"
exit do
end if
loop

demo10就是一个简单的密码验证的作用,通过do loop来达到验证的目标。exit do是跳出循环。也就是结束整个的循环。

demo11.vbs

dim password
pass="qwe123"
num=1

do
password=inputbox("Please enter your password")
if password=pass then
msgbox "success"
exit do
else if num=3 then
msgbox "Error 3 times, the program will close"
exit do
else
num=num+1
msgbox "Password mistake"
end if
end if
loop

这串代码的大致作用是验证密码的正确性,一旦密码错误达3次,程序将会退出。

posted @ 2020-01-22 17:24  Paddling  阅读(647)  评论(0编辑  收藏  举报