需求:编写PowerShell脚本验证Unix路径语法有效性
PowerShell脚本验证Unix路径语法问题
需求概述
需要编写PowerShell脚本,实现以下功能:
- 验证用户输入的Unix路径语法合法性(无需验证主机上实际路径是否存在)
- 输入支持单条或多条路径,路径可通过逗号或空格分隔
- 路径可以是无引号包裹、单引号包裹或双引号包裹的形式
现有脚本问题
以下是我尝试的脚本(用于GitHub Actions),但无法满足上述验证条件:
- name: Validate Inputs run: | $inputPaths = "${{ inputs.source_files }}" # Check if the input is not empty if (-not $inputPaths) { echo "Error: 'paths' input is required." exit 1 } # Check syntax of each provided path $pathsArray = $inputPaths -split ',| ' foreach ($path in $pathsArray) { if (-not ($path -match "^[a-zA-Z]:\\|\\\\|/'[^'\s].*'$|^[a-zA-Z]:\\|\\\\|/\"[^\\"\s].*\"$|^[a-zA-Z]:\\|\\\\|/[^'\s]+$")) { echo "Error: '$path' is not a valid absolute path syntax." exit 1 } } echo "Inputs have valid syntax.
合法/非法输入示例
合法输入
/tmp/mydir '/tmp/my dir1' "/tmp/my dir2" /tmp/mydir '/tmp/my dir1' '/tmp/my dir2' '/tmp/my dir1','/tmp/my dir2'
非法输入
'/tmp/my dir1,/tmp/my dir2' /tmp/my dir1 '/tmp/my dir1 /tmp/my dir1'
拆分验证时的错误
我尝试另一种方式拆分路径并验证引号,但合法路径也会报错:
$paths = "'/u/marsh/UNX/scripts/testscript/test_maillist.txt' '/pathx with space/file1' '/path,with,commas/file2' ""/double quoted path/file3"" ""/path with space/file4"" 'single quoted path/file5' /pathx with space/file1" # Split paths by whitespace or comma while preserving paths enclosed in quotes $splitPaths = $paths -split "(?<=\S)\s+|(?<=\S)," foreach ($path in $splitPaths) { # Check if the path is enclosed in single or double quotes if (-not (($path -like "'*'") -or ($path -like '"*"'))) { Write-Host "Error: Path '$path' is not enclosed in single or double quotes." exit 1 } # Remove leading and trailing quotes $cleanPath = $path.Trim("'").Trim('"') Write-Host "Cleaned Path: $cleanPath" }
错误输出(本不应报错):
Cleaned Path: /u/marsh/UNX/scripts/testscript/test_maillist.txt Error: Path ''/pathx' is not enclosed in single or double quotes.
解决方案
步骤1:正确拆分输入路径
问题出在拆分逻辑上,普通的-split会把引号内的空格/逗号也拆分,导致路径被截断。可以用正则表达式匹配完整的路径单元:
function Split-Paths { param([string]$InputString) # 匹配规则: # 1. 单引号包裹的路径:'...'(允许内部包含空格、逗号) # 2. 双引号包裹的路径:"..."(允许内部包含空格、逗号) # 3. 无引号的路径:连续非空格/非逗号字符 $regex = "'[^']+'|""[^""]+""|[^,\s]+" return [regex]::Matches($InputString, $regex) | ForEach-Object { $_.Value } }
步骤2:验证路径语法
拆分后,对每个路径进行处理:先去除可能的引号,再验证是否为合法的Unix绝对路径:
function Test-UnixPathSyntax { param([string]$Path) # 去除前后的单/双引号 $cleanPath = $Path.Trim("'""") # 验证Unix绝对路径:必须以/开头,后续可以包含任意字符(除了换行) return $cleanPath -match "^/[^`n`r]*$" }
完整脚本示例
$inputPaths = "${{ inputs.source_files }}" # 检查输入是否为空 if (-not $inputPaths) { Write-Error "Error: 'paths' input is required." exit 1 } # 拆分路径 $pathsArray = Split-Paths -InputString $inputPaths foreach ($path in $pathsArray) { # 验证路径语法 if (-not (Test-UnixPathSyntax -Path $path)) { Write-Error "Error: '$path' is not a valid Unix absolute path syntax." exit 1 } # 可选:输出清理后的路径 $cleanPath = $path.Trim("'""") Write-Host "Valid path: $cleanPath" } Write-Host "All inputs have valid syntax."
关键说明
Split-Paths函数通过正则匹配完整的路径单元,避免拆分引号内的分隔符Test-UnixPathSyntax函数先清理引号,再验证路径是否以/开头(Unix绝对路径的核心特征)- 该逻辑支持路径内包含空格、逗号等特殊字符,只要被正确包裹在引号中
内容的提问来源于stack exchange,提问作者Ashar
相关产品推荐
相关产品推荐

