基本
- 注释
- 单行注释:
# - 多行注释:
<# #>#this is single-line comment <# this is multiple-line comment #>
- 单行注释:
- 帮助命令:
get-help [param]- param 可选,为具体的powershell命令
#e.g. get-help dir
- param 可选,为具体的powershell命令
- 分号:每条语句可以使用一个分号结束,当然也可以不写。如果一行中存在多条语句,那就在每一句后加个分号用以分隔。
- 代码块:和C语系的大多数语言类似,PowerShell是一种块结构的语言,这些块用
{和
}来界定。 - 缩进:powershell中的缩进不是强制的。
- 大小写:powershell不区分大小写。
- 脚本:powershell像python一样,可以交互式编程,也可以编写脚本运行,powershell的脚本扩展名为
.ps1,注意是数字1不是字母l,运行脚本时只需要在终端键入./NAME.ps1即可,注意这里的./是必须的,当然也可以键入绝对路径 - 脚本执行策略:系统默认禁止运行脚本,故直接运行powershell脚本会报错,需要更改执行策略,脚本执行策略如下
策略 解释 unrestricted 最高权限,可以不受限制执行任何脚本 default 为powershell默认的策略,即restricted restricted 不允许任何脚本执行 allsigned 所有脚本都必须经过签名才能运行 remotesigned 本地脚本无限制,但是对来自网络的脚本必须经过签名 - 管理员身份打开powershell
- 键入
get-executionpolicy查看当前执行策略,默认为restricted - 键入
set-executionpolicy remotesigned更改执行策略 - 确认更改执行策略,然后即可以运行脚本
- 重定向:可以使用重定向来输出同时创建脚本
#将hello powershell!输出到info.ps1中,没有则创建文件 echo "hello powershell!" > info.ps1 - Here-String:Here-String以
@'开头,以'@结束,类似于python中的"""或''',powershell中可以结合重定向将其中的内容输出到脚本中
数据类型及操作
- 变量
- 用
$变量名=初值的方法定义变量 - 解释器会根据所赋的初值判断变量类型
- 在声明后使用变量的过程中有时也需要加上
$ - 对于包含特殊字符的变量可以将变量名本身用{}括起来
- 不能定义和保留变量名称相同的变量,可以通过
ls variable:查看保留变量 - 可以通过
$NAME.gettype()查看变量NAME的类型等信息 - 可以通过
del variable:NAME删除变量NAME,这里不用加上$
$info = "hello powershell!" ${ mother's day} = 5.23 - 用
- 转义字符:powershell的转义字符是 ` 而不是 \(英文输入模式下tab上面那个键)
- 动态类型:powershell的变量是动态类型的,同python,即初始化后的变量可以通过之后的赋值而改变其类型
- 数值转换:powershell从浮点类型向整数转换时,默认四舍五入
[math]::floor()用于向下取整,即直接截断浮点数,但不改变类型[convert]::toint32()用于改变类型为int32- 可以结合上面二者实现c和Java中的
int()强制类型转换 [convert]::todouble(),[convert]::tosingle(),[convert]::toboolean,[convert]::tostring
- 运算符:
+、-、*、/、%加减乘除取余++、--自增自减(同c支持前置后置)-and、-or、-not逻辑与或非-eq、-ne、-ge,-le、-gt、-lt等于、不等于、大于等于、小于等于、大于、小于-is判断变量是否为兼容类型
#基本运算 $a + $b $a - $b $a * $b $a / $b $a % $b #自增自减 ++$a $a++ --$a $a-- #逻辑运算符、比较运算符 $a -gt $b -and $b -ge $c # <==> a>b && b >= c #类型判断 $a -is [double] -not $b -is [int] - 数组
- 定义
#空数组 $a = @() #单元素数组 $ar = ,1 #一般定义 $arr = 1,2,3,4,5 $arrr = 1..10 $arrrr = 10..0- 遍历:直接输出,循环遍历
$arr = 1..5 # <==> $arr = 1,2,3,4,5 #直接输出 echo $arr #for循环遍历 for ($i = 0; $i -lt 5; $i++){ echo $arr[$i] # 数组支持取下标运算 } #foreach循环遍历 foreach ($i in $arr){ echo $i }- 元素访问
$arr = 5..0 #下标访问 $arr[2] #多元素访问 $arr[0,2,4] $arr[0..3] # 类似python切片- 元素新增
$arr = 1,2,3,4,5 $arr += 6 # 6将新增至末尾- 元素删除
$arr = 1,2,3,4,5 $arr = $arr[0..2]+$arr[4]
流程控制
- if判断:同cpp类似,e.g.
$a = 3.14 if ($a -lt 3){ echo "hello" } elseif ($a -gt 3){ echo "world" } else { echo "other!" } - switch分支:无需case,无需break,e.g.
$info = "this" switch($info){ "that" { echo "that" echo "there" } "this" { echo "this" echo "there" } }可支持条件语句,执行所有语句值为true的内容,如下将同时满足第二和第三分支,这在cpp中是不被允许的
$num = 5.56 switch($num){ { $num -lt 5}{ write-output "num<5" # write-output同echo } { $num -ge 5}{ write-output "num>=5" } 5.56{ write-output "num=5.56" } } - for循环:同cpp
for ($i = 1; $i -lt 10; $i++){ echo $i } - foreach循环:同python
$arr = 'a','b','c','d' # arr为数组 foreach ($each in $arr){ echo $each } - while循环:同cpp
$n = 1 while ($n -lt 10){ echo $n $n++ } - do-while循环:同cpp
$n = 1 do { echo $n $n++ }while($n -lt 10) - 循环控制:同cpp,
continue和break
函数
- 函数定义
function fun(a,b,c){ <#statement#> } #e.g. function times($a){ return $a*5 } - 函数调用
# powershell中的函数存在以下两种调用方式 times(5) times 5 # 同理,内置函数也可 write-output("hello") write-output hello # 当函数调用没有参数时,不要采用第一种方式,即不要给出括号 fun() # × fun # √ # 对于类方法而言,则正好相反 $info = "this is info." $info.gettype() # √,返回变量类型信息 $info.gettype # ×,返回gettype方法定义信息 - 形参默认值
# powershell支持形参默认值 function fun($b = 5){ return $b } # 有参调用 fun(55) # 返回55 # 无参调用 fun # 返回5 - 多个参数调用
# powershell的多参数传递 function f($a, $b){ return $b } # 此时两种调用方式结果不同 f(1,2) # 1,2将被作为数组传给$a,$b将不会得到值 f 1 2 # 1将传给$a,2将传给$b # 第一种方式以下写法可以达到目的,形式上类似函数柯里化 f(1)(2) # 1将传给$a,2将传给$b # 同理,以下两种调用等价,1,2作为数组传给$a,3,4作为数组传给$b f(1,2)(3,4) f 1,2 3,4 # 多余的参数将被忽略,如下1传给$a,2传给$b,3将被忽略 f(1)(2)(3) f 1 2 3 # 附例 function add($a, $b){ return $a + $b } # 考虑以下各种调用函数的返回值 add(1,2) # 返回数组1,2,$a == 1,2,$b没有值 add(1)(2) # 返回3,$a == 1, $b == 2 add 1 2 # 同上 add(1)(2)(3) # 返回3,$a == 1, $b == 2, 3被忽略 add 1 2 3 # 同上 add(1,2)(3) # 返回数组1,2,3,$a == 1,2,$b == 3,$b被视为数组,$a+$b为数组拼接 add 1,2 3 # 同上 - powershell函数不支持定义重载,函数会以最后一次定义执行,但类方法可以定义重载,且类方法不需要
function关键字声明。
类
- 作为面向对象的编程语言,类是不可或缺的,powershell的类定义方式如下
class person{ # 类成员 $name = "zhang"; $sex = "male"; $country = "China"; # 类方法 info(){ [console]::write("my name is " + $this.name + ", I come from " + $this.country); } } # 类实例化 $pers = new-object person # 调用类方法 $pers.info() # 将输出:my name is zhang, I come from China. - 构造函数与实例化,同cpp,构造函数与类同名
class person{ $name = "zhang"; $sex = "male"; $country = "China"; person(){ } # 未声明构造函数时会隐式声明该无参构造函数 person($name, $sex, $country){ # 自定义构造函数 $this.name = $name; $this.sex = $sex; $this.country = $country; } } # 类实例化方式有两种 # 1. new-object $pers1 = new-object person # 需要有无参构造函数可供调用,实例化后手动赋值 $pers1.name = "a" $pers1.sex = "male" $pers1.country = "China" # 2. [CLASSNAME]::new() $pers2 = [person]::new("wang","male","Russia") # 实例化同时赋值 - 继承
class person{ # 父类 $name; $sex; $country; info(){ [console]::write("my name is " + $this.name + ", I come from " + $this.country); } } class student : person{ # 子类,包含父类成员和方法 $school; info(){ # 方法覆写 [console]::write("my name is " + $this.name + ", I come from " + $this.country + ", I study in " + $this.school); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229131.html原文链接:https://javaforall.net
