循环用户输入类似于批处理文件中的goto

时间:2014-05-01 20:25:35

标签: loops powershell goto

我是动力贝壳的新手。我试图在批处理文件中使用像goto这样的函数。

在我的脚本中$input1 = Read-Host "Please select an option 1, 2, 3. " 如果使用不输入1,2,3,我想清除主机并重新启动脚本。

Write-Host 1. Convert Byte to Megabyte.
Write-Host
Write-Host 2. Convert Byte to Gigabyte.
Write-Host
Write-Host 3. Conver Byte to Terabyte.

#
#
# Assign value to the variables
#

$b2MB = '1048576'
$b2GB = '1073741824'
$b2TB = '1099511627776'

$input1 = ""

#
#
# prompts user for input from the main screen.
#
$input1 = Read-Host "Please select an option 1, 2, 3. "

#
#
# Depending on what the user input will execute a certain conversion.
#
#

if ( $input1 -eq 1 ) {
$mb2 = Read-Host " Enter how many bytes you want to convert to Megabytes? "
$bytesToMb = $mb2 / $b2MB
Write-Host $mb2 'is equal to '$bytesToMb' Megabytes'
} elseif ($input1 -eq  2) {
$mb3 = Read-Host " Enter how many bytes you want to convert to Gigabytes? "
$bytesToGb = $mb3 / $b2GB
Write-Host $mb3 'is equal to '$bytesToGb' Gigabytes'
} elseif ( $input1 -eq 3) {
$mb4 = Read-Host " Enter how many bytes you want to convert to Terabytes? "
$bytesToTb = $mb4 / $b2TB
Write-Host $mb4 'is equal to '$bytesToTb' Terabytes'
} else {
Write-Host " You have entered an invalid option. "
}

2 个答案:

答案 0 :(得分:0)

我相信你想要这样的东西:

# Start a continuous loop
while ($true) {

    # Write messages
    Write-Host 1. Convert Byte to Megabyte.
    Write-Host
    Write-Host 2. Convert Byte to Gigabyte.
    Write-Host
    Write-Host 3. Conver Byte to Terabyte.

    # Get the input
    $input1 = Read-Host "Please select an option 1, 2, 3. "

    # See if the input equals 1, 2, or 3.  If so, break the loop.
    if (1..3 -contains $input1) { break }

    # If we get here, then the input was bad.
    # So, we clear the host and let the loop start-over
    Clear-Host
}

#
#
# Assign value to the variables
#

$b2MB = '1048576'
$b2GB = '1073741824'
$b2TB = '1099511627776'

#
#
# Depending on what the user input will execute a certain conversion.
#
#

if ( $input1 -eq 1) {
    $mb2 = Read-Host " Enter how many bytes you want to convert to Megabytes? "
    $bytesToMb = $mb2 / $b2MB
    Write-Host $mb2 'is equal to '$bytesToMb' Megabytes'
} elseif ($input1 -eq  2) {
    $mb3 = Read-Host " Enter how many bytes you want to convert to Gigabytes? "
    $bytesToGb = $mb3 / $b2GB
    Write-Host $mb3 'is equal to '$bytesToGb' Gigabytes'
} elseif ( $input1 -eq 3) {
    $mb4 = Read-Host " Enter how many bytes you want to convert to Terabytes? "
    $bytesToTb = $mb4 / $b2TB
    Write-Host $mb4 'is equal to '$bytesToTb' Terabytes'
} else {
    Write-Host " You have entered an invalid option. "
}

答案 1 :(得分:0)

很简单,使用Do / Until循环。

#
#
# Assign value to the variables
#

$input1 = ""

# Start the loop to get the user's choice (with validation)
Do{
    cls
    Write-Host 1. Convert Byte to Megabyte.
    Write-Host
    Write-Host 2. Convert Byte to Gigabyte.
    Write-Host
    Write-Host 3. Conver Byte to Terabyte.

    #
    #
    # prompts user for input from the main screen.
    #
    $input1 = Read-Host "Please select an option 1, 2, 3. "
}Until(@(1,2,3) -contains $input1)

Switch($input1){
    1 {Read-Host "Enter how many bytes you want to convert to Megabytes? "|%{$Out = "{0:N} bytes is equal to {1:n4} Megabytes" -f [double]$_,($_/1MB);Write-Host "$Out"}}
    2 {Read-Host "Enter how many bytes you want to convert to Gigabytes? "|%{$Out = "{0:N} bytes is equal to {1:n4} Gigabytes" -f [double]$_,($_/1GB);Write-Host "$Out"}}
    3 {Read-Host "Enter how many bytes you want to convert to Terabytes? "|%{$Out = "{0:N} bytes is equal to {1:n4} Terabytes" -f [double]$_,($_/1TB);Write-Host "$Out"}}
}

我还使用了Switch而不是一堆If / Then语句,并使用### / 1MB或/ 1GB或/ 1TB进行了数学计算,因为Powershell可以为您进行计算。

编辑:哦,是的,我也格式化了这些数字。添加逗号可以更容易地读取大数字,并且我将计算的值截断为小数点后的4位。这可以通过将{1:n4}中的4更改为您希望小数转到的任何值来更改,或者删除4以包含所有内容。如果你使它成为一个大的十进制,如果很多不存在,它将填充零。例如4.23格式化为{1:n5}时出现为4.23000,因为它必须有超过小数的5位。

好的,使用Switch循环而不是像你使用的嵌套if语句。语法是

Switch(要解析的数组){         值或脚本块{在匹配时执行的操作}         不同的值或scriptblock {匹配时要执行的操作}     } 所以在你的情况下,我们只解析一件事,那就是用户选择。所以,让我们说他们选择2转换为千兆字节。 $ input2 = 2.开关在顶部开始比较,并检查值是否为1.不是这样,它移动到下一行。它检查值是否为2.它是,所以switch命令在那里执行附带的scriptblock中的操作。我们马上就会谈到这一点。完成该脚本块之后,它将移动到下一行并检查该值是否为3.它不是,这是最后一个选项,因此它完成了该循环。没有其他值要解析,因为$ input2只有一个值,所以它退出了switch循环。 Switch可以在数组上运行,因此可能有更多东西可以与switch循环中的三个选项进行比较。

那是开关循环。让我们看一下当第二个选项的值匹配时它运行的脚本块。我会在这里分解一下......

Read-Host "Enter how many bytes you want to convert to Gigabytes? "|
%{
    $Out = "{0:N} bytes is equal to {1:n4} Gigabytes" -f [double]$_,($_/1GB);
    Write-Host "$Out"
}

如果我没记错的话,那第一行几乎就是你的,但是不是将值存储在变量中以供以后使用,而是将它管道传输给每个循环(%{...}是foreach的缩写{... })。然后,由于write-host实际上不允许格式化,我会做一些格式化并将字符串保存在$ out中。所以这就是它可能会变得令人生畏的地方。 " {0:N}"使用-f这是-format的缩写。它就像一个占位符,它说要查看字符串末尾的-f命令并获得提供给它的第一个东西(powershell中的数组从记录0开始,所以通常对项目0的引用正在寻找第一个项目)。一旦它获得了-f之后的第一个值,它就会应用冒号N之后指定的格式,这意味着它将它格式化为一个数字,每三个数字都放一个逗号。

好的,那里有一些文本,然后是{1:N4},这是另一个格式化占位符。冒号前面的部分指定要获取的项目,并且由于0是要格式化的第一个值,因此1必须是第二个。因此它获取第二个值并在冒号后应用格式。 N4再次表示它是一个数字,但指定仅显示小数点后的前4个数字。

然后它有-f表示有一些东西需要格式化。第一项是$ _,这是用户输入的用于传送到foreach循环的值。它之前的[double]指定对象的类型。 Double是一种数字,如int32或hex,但它有非常宽松的指导,因此用户可以输入非常大的值而不会出现问题。这会直接传递给第一个占位符。然后是$ _ / 1GB,这是用户输入的数字除以千兆字节,因为powershell具有内置的功能。它找到结果并将其传递给第二个占位符。所以现在我们要将文本传递回用户,数字格式良好且易于阅读,并将该字符串存储到$ out中。该scriptblock的最后一件事是将$ out写入主机。

现在你可能会问为什么我必须首次将$ _指定为[double],而不是第二次。原因是我第二次对它进行数学运算,所以powershell认为如果我将它分开,它必须是一个数字,所以我不必在那里声明它。

我希望现在让脚本更有意义。

相关问题