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



我是power shell的新手。我试图在批处理文件中使用像goto这样的功能。

在我的脚本中,我有$input1 = Read-Host "Please select an option 1, 2, 3. "如果用户没有输入1,2,3,我想Clear-Host并重新启动脚本。

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. "
}

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

# 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. "
}

很简单,使用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来包含所有内容。如果你让它变成一个大的小数,如果没有那么多,它就会被0填满。例如,通过{1:n5}格式化的4.23输出为4.23000,因为它必须在小数点后面有5位。

好的,Switch循环被用来代替嵌套的if语句。语法是

Switch(array to parse) {值或scriptblock{对匹配执行的操作}不同的值或scriptblock{对匹配执行的操作}}在你的例子中,我们只需要解析一件事,那就是用户选择。假设选择2来转换成gb。$input2 = 2。Switch从顶部开始比较,并检查值是否为1。它不是这样的,所以它移动到下一行。它检查值是否为2。是的,因此switch命令执行附带的scriptblock中的操作。我们马上就会讲到。在完成scriptblock之后,它移动到下一行并检查值是否为3。它不是,这是最后一个选项,所以它结束了循环。没有其他值需要解析,因为$input2只有一个值,所以它退出了switch循环。Switch在数组上工作,因此可能会有更多的东西与Switch循环中的三个选项进行比较。

这是开关回路。让我们看看当第二个选项的值匹配时运行的scriptblock。我在这里稍微细分一下……

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一起使用,-f是-format的缩写。它的工作原理类似于占位符,表示查看字符串末尾的-f命令并获取提供给它的第一个内容(powershell中的数组从记录0开始,因此通常对项目0的引用正在查找第一个项目)。一旦它得到-f后面的第一个值,它就应用冒号N后面指定的格式,这意味着它将其格式化为每三个数字加一个逗号的数字。

这里有一些文本,然后{1:N4}是另一个格式化占位符。冒号之前的部分指定要获取的项,因为0是第一个要格式化的值,所以1必须是第二个。因此它获取第二个值并应用冒号后的格式。N4同样表示它是一个数字,但指定只显示小数点后的前4个数字。

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

现在你可能会问为什么我第一次指定$_为[double],而第二次没有。原因是我第二次对它进行数学运算时,powershell计算出如果我要除它,它一定是一个数字,所以我不需要在这里声明它。

我希望这使得脚本现在更有意义。

最新更新