调用其他程序时的Powershell变量扩展 [英] Powershell variable expansion when calling other programs
问题描述
我在尝试使用 7za
解压缩文件时遇到了一个小问题一>Powershell 中的命令行实用程序.
I have a small problem trying to unzip a file using the 7za
command-line utility in Powershell.
我将 $zip_source
变量设置为 zip 文件的路径和$unzip_destination
到所需的输出文件夹.
I set the $zip_source
variable to the zip file's path and the
$unzip_destination
to the desired output folder.
然而,7za
的命令行使用需要这样指定的参数:
However the command-line usage of 7za
needs arguments specified like this:
7za x -y <zip_file> -o<output_directory>
所以我目前的电话是这样的:
So my current call looks like this:
& '7za' x -y "$zip_source" -o$unzip_destination
由于 -o
和目标之间不能有空格似乎 PowerShell 不会扩展 $unzip_destination
变量,而 $zip_source
已扩展.
Due to the fact that there can be no space between -o
and the destination it
seems that PowerShell will not expand the $unzip_destination
variable, whereas $zip_source
is expanded.
目前程序只是将所有文件解压到C:\
的根目录下一个名为 $unzip_destination
的文件夹.在变量周围设置不同类型的引号也不起作用:
Currently the program simply extracts all the files into the root of C:\
in
a folder named $unzip_destination
.
Setting different types of quotes around the variable won't work neither:
-o"$unzip_destination" : still extracts to C:\$unzip_destination
-o'$unzip_destination' : still extracts to C:\$unzip_destination
-o $unzip_destination : Error: Incorrect command line
有没有什么办法可以在运行命令之前强制展开?
Is there any way to force an expansion before running the command?
推荐答案
试试这个:
-o $($unzip_destination)
编者注:此解决方案仅适用于 -o
后的空格(在这种情况下,只需 -o $unzip_destination
do) - 如果删除它,该命令将无法按预期工作.
因此,这种方法不适合按照 OP 的要求将变量值直接附加到选项名称.
Editor's note: This solution only works with a space after -o
(in which case just -o $unzip_destination
would do) - if you remove it, the command doesn't work as intended.
This approach is therefore not suitable for appending a variable value directly to an option name, as required by the OP.
这篇关于调用其他程序时的Powershell变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!