国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PowerShell > 正文

PowerShell實現統計函數嵌套深度

2020-10-29 20:57:48
字體:
來源:轉載
供稿:網友

當你調用某個函數時,PowerShell會增加一次嵌套層次。當一個函數調用了另一個函數,或著腳本,也會增加嵌套層次。今天分享一個函數,它能告訴你的腳本嵌套的層次:

function Test-NestLevel{$i = 1$ok = $truedo{try{$test = Get-Variable -Name Host -Scope $i}catch{$ok = $false}$i++} While ($ok) $i}

當你調用的函數具有遞歸調用時,上面的函數非常有用,來看一個調用的示例:

function Test-Diving{param($Depth) if ($Depth -gt 10) { return } "Diving deeper to $Depth meters..." $currentDepth = Test-NestLevel"calculated depth: $currentDepth" Test-Diving -depth ($Depth+1)} Test-Diving -depth 1 


當你運行Test-Diving時,函數會調用自己10次。函數使用一個參數來控制嵌套層次,而Test-NestLevel負責返回確切的深度數。

注意這里有個區別:Test-NestLevel返回絕對的嵌套層次,參數會紀錄這個函數調用自己多少次。如果Test-Diving被嵌入到另外一個函數中,絕對深度和相對深度會不同。

 

PS C:/> Test-Diving -Depth 1diving deeper to 1 meters...calculated depth: 1diving deeper to 2 meters...calculated depth: 2diving deeper to 3 meters...calculated depth: 3diving deeper to 4 meters...calculated depth: 4diving deeper to 5 meters...calculated depth: 5diving deeper to 6 meters...calculated depth: 6diving deeper to 7 meters...calculated depth: 7diving deeper to 8 meters...calculated depth: 8diving deeper to 9 meters...calculated depth: 9diving deeper to 10 meters...calculated depth: 10 PS C:/> & { Test-Diving -Depth 1 }diving deeper to 1 meters...calculated depth: 2diving deeper to 2 meters...calculated depth: 3diving deeper to 3 meters...calculated depth: 4diving deeper to 4 meters...calculated depth: 5diving deeper to 5 meters...calculated depth: 6diving deeper to 6 meters...calculated depth: 7diving deeper to 7 meters...calculated depth: 8diving deeper to 8 meters...calculated depth: 9diving deeper to 9 meters...calculated depth: 10diving deeper to 10 meters...calculated depth: 11 PS C:/>


Test-NestLevel總會返回從當前代碼的作用域到全局作用域的嵌套深度。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭锦旗| 东海县| 红安县| 大兴区| 开封县| 泽普县| 浙江省| 巴楚县| 星子县| 四川省| 靖西县| 墨脱县| 普宁市| 天峨县| 海伦市| 林芝县| 拜泉县| 商丘市| 临漳县| 南溪县| 孟村| 普陀区| 禄劝| 农安县| 乐山市| 大同市| 静安区| 岢岚县| 海宁市| 建湖县| 乌恰县| 横山县| 盐源县| 夹江县| 科技| 稻城县| 寻甸| 拉孜县| 镇宁| 石狮市| 老河口市|