linux에서 동작하는 powershell script를 만들 때, ubuntu인 지, centos인 지에 따라서 다른 처리를 해야하는 경우가 있습니다. 이 때에 사용하는 함수를 만들어 보았습니다.
어떤 Distribution인 지 확인하는 방법은 여러가지 방법이 있습니다만, 이 스크립트에서는 /etc/os-release
파일을 열어서 그 내용을 확인합니다.
key=value
의 포맷으로 되어 있는 문자열은 convertfrom-stringdata 함수로 그 Parsing할 수 있습니다.
구현
매 번 사용하지 않도록 전역변수 IsUbuntu
와 IsCentOS
에도 값을 넣어 보았습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| Function Test-Ubuntu {
if ($IsWindows) {return $False}
$os_info_file = "/etc/os-release"
if (Test-Path -Path $os_info_file) {
$hash = ConvertFrom-StringData(Get-Content -Path $os_info_file -Raw)
if ($hash.ContainsKey("ID")) {
If ("ubuntu" -eq $hash.ID) {
$global:IsUbuntu = $True
$global:IsCentOS = $False
return $True
} ElseIf ("centos" -eq $hash.ID){
$global:IsUbuntu = $False
$global:IsCentOS = $True
return $False
}
}
}
return $False
}
|
결과
1
2
3
4
5
6
7
| PS /root> Test-Ubuntu
True
PS /root> $IsUbuntu
True
PS /root> $IsCentOS
False
PS /root>
|
의도된 대로 실행된 것을 확인하였습니다. 이제는 이것으로 분기할 수 있겠네요.
레퍼런스