假设我将以下 powershell 代码存储在文件中:
## file1.ps1
$myvar = "i am here"
if ($myvar -ne $null) {
"($myvar) variable is Full"
} else {
"($myvar) variable is Empty"
}
Run Code Online (Sandbox Code Playgroud)
然后这段代码被存储在一个变量中:
$code_in_var = cat file1.ps1
Run Code Online (Sandbox Code Playgroud)
如何通过管道执行变量中的代码?
我已经尝试过以下方法:
PS C:\Mrm> $code_in_var = cat file1.ps1
PS C:\Mrm>
PS C:\Mrm> cat file1.ps1 | powershell -
PS C:\Mrm>
PS C:\Mrm> cat file1.ps1 | Invoke-expression
Invoke-expression ; At line:1 char:23
+ if ($myvar -ne $null) {
+ ~
Missing closing '}' in statement bllock or type definition
At …Run Code Online (Sandbox Code Playgroud) 我知道脚本可以检索通过 ARGV 传递给它的所有命令行参数,即:
# test.pl
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
## perl ./test.pl one two three
one
two
three
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,传递给test.pl脚本的命令行参数是“一”、“二”和“三”。
现在,假设我运行以下命令:
## perl -d:DumpTrace test.pl one two three
or
## perl -c test.pl one two three
Run Code Online (Sandbox Code Playgroud)
如何从test.pl脚本的操作中判断选项-c或-d:DumpTrace已传递给 perl 解释器?
我正在寻找一种方法来确定在脚本执行期间选项何时传递给 perl 解释器:
if "-c" was used in the execution of `test.pl` script {
print "perl -c option was used in the execution of this script";
}
Run Code Online (Sandbox Code Playgroud)