首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >菜单选择不按选择返回

菜单选择不按选择返回
EN

Stack Overflow用户
提问于 2022-05-24 10:38:22
回答 1查看 48关注 0票数 1

我在下面的简单代码中用脚本显示菜单

代码语言:javascript
复制
        Write-Host "============= Main Category ================================="
            Write-Host " '1' InfoBlox"
            Write-Host " '2' Installation"
            Write-Host " '3' Active Directory"
            Write-Host " '4' LogInventory"
            Write-Host " '5' Configuration"
            Write-Host " '6' New Server Build CMDB Update "
            Write-Host " '7' for Exit"
        Write-Host "========================================================"
        $choice = Read-Host "`nEnter Choice"

if($choice -eq '1')
{
#Menu for choice 
    cls
    Write-Host "=============InfoBlox================================="
    Write-Host " '1' Add CNAME record"
    Write-Host " '2' Delete CNAME record"
    Write-Host " '3' Add A-record and PTR record"
    Write-Host " '4' Delete A-record and PTR record"
    Write-Host " '5' for Main Menu"
    Write-Host "========================================================"
    $subchoice = Read-Host "`nEnter Choice"
#code here
  if($subchoice -eq '5')
        {
        cls
        Write-Host "============= Main Category ================================="
            Write-Host " '1' InfoBlox"
            Write-Host " '2' Installation"
            Write-Host " '3' Active Directory"
            Write-Host " '4' LogInventory"
            Write-Host " '5' Configuration"
            Write-Host " '6' New Server Build CMDB Update "
            Write-Host " '7' for Exit"
        Write-Host "========================================================"
        $choice = Read-Host "`nEnter Choice"
}
if($choice -eq '2')
{
 #code here
}
if($choice -eq '3')
{
 #code here

}

我面临的问题是,当我第一次执行的时候,它运行的很好。例如,当我选择**1,2,3.**时很好,但是当我再次作为选择从3返回到1时,脚本将退出并显示powershell提示符。

请让我知道为什么会发生这种事。我可能无法恰当地解释。如果有什么问题请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-24 14:09:52

Mathias R. Jessen在他的有用注释中给出了指针,每个菜单/选项都应该有自己的函数或脚本块,逻辑应该封装在一个无限循环中,在选择Exit选项时结束。

为了演示目的,我减少了代码。

代码语言:javascript
复制
function Menu {
    Write-Host " '1' Option 1"
    Write-Host " '2' Option 2"
    Write-Host " '3' for Exit"
}

function SelectSomething {
    Read-Host "`nEnter Choice"
}

function FirstOption {
    Write-Host " '1' Add CNAME record"
    Write-Host " '2' Delete CNAME record"
    Write-Host " '3' for Main Menu"
    SelectSomething
}

function SecondOption {
    Write-Host " '1' InfoBlox"
    Write-Host " '2' Installation"
    Write-Host " '3' for Main Menu"
    SelectSomething
}

$firstScriptblock = {
    switch(FirstOption) {
        1 { 'Option 1 - Add CNAME record' }
        2 { 'Option 2 - Delete CNAME record' }
        3 { return }
        Default { Write-Warning 'Invalid Selection' }
    }
    $Host.UI.ReadLine()
    & $MyInvocation.MyCommand.ScriptBlock
}

$secondOption = {
    Write-Warning 'Not implemented! Back to Menu'
    $Host.UI.ReadLine()
}

:outer while($true) {
    Clear-Host
    Menu
    switch(SelectSomething) {
        1 { & $firstScriptblock }
        2 { & $secondOption }
        3 { break outer }
        Default { Write-Warning 'Invalid Selection' }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72361431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档