String 如何将两个字符串传递给PowerShell函数?
如何将String 如何将两个字符串传递给PowerShell函数?,string,function,powershell,oop,concatenation,String,Function,Powershell,Oop,Concatenation,如何将$dir和$file传递到单个路径 输出: posh> posh> pwsh ./worker.ps1 dir is /home/nicholas/powershell/regex a.log file is full is /home/nicholas/powershell/regex a.log/ posh> 图书馆: function SearchFile($dir,$file) { "dir is $dir" "file is
$dir
和$file
传递到单个路径
输出:
posh>
posh> pwsh ./worker.ps1
dir is /home/nicholas/powershell/regex a.log
file is
full is /home/nicholas/powershell/regex a.log/
posh>
图书馆:
function SearchFile($dir,$file)
{
"dir is $dir"
"file is $file"
$full = [string]::Concat("full is ",$dir,"/",$file)
$full
#$pattern='(?=.*?foo)(?=.*?bar)'
#$string = Get-Content $full
#$result = $string | Select-String $pattern
#$result
}
工人:
. /home/nicholas/powershell/functions/library.ps1
$dir = "/home/nicholas/powershell/regex"
$file = "a.log"
SearchFile($dir,$file)
我似乎正在为
$dir
传递一个排序数组,而搜索文件中的$file
没有按预期分配任何内容。可以用一种更简单的方法完成:
function SearchFile ($var_1, $var_2) {
$full_path = "$var_1/$var_2"
$full_path # Output --> C:/temp/test.txt
}
$my_dir = "C:/temp"
$my_file = "test.txt"
SearchFile $my_dir $my_file # Do not use coma when calling a Function
调用函数时,不要在函数参数周围使用()
。[咧嘴笑]只要把它们放在像SearchFile$dir$file
这样的地方就行了。@Lee\u Dailey谢谢你非常欢迎!很高兴能帮上一点忙。。。[咧嘴笑]是的,信不信由你,我的kindle上其实有一本powershell的书。它涵盖了我不需要知道的一切..哈哈。我只是偶然发现了如何像上面那样声明函数。