「Windows/powershell/基本」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→dir存在判定) |
(→dirpathの親を取得) |
||
| (同じ利用者による、間の40版が非表示) | |||
| 行1: | 行1: | ||
==変数== | ==変数== | ||
| − | $ | + | $hoge = "hogehoge" |
==判定== | ==判定== | ||
| 行8: | 行8: | ||
if ($condition) | if ($condition) | ||
{ | { | ||
| − | Write- | + | Write-Host "OK" |
} | } | ||
</pre> | </pre> | ||
| + | ==比較== | ||
| + | <pre> | ||
| + | $num = 10 | ||
| + | if ($num -eq 10) { | ||
| + | Write-Host 'OK' | ||
| + | } else { | ||
| + | Write-Host 'NG' | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | その他比較 | ||
| + | <pre> | ||
| + | if ($num -ne 10) # 等しくない | ||
| + | if ( $value -gt 5 ) # < | ||
| + | if ( $value -lt 5 ) # > | ||
| + | if ( ($age -gt 13) -and ($age -lt 55) ) # and条件 | ||
| + | if ( $age -le 13 -or $age -ge 55 )# and条件 | ||
| + | </pre> | ||
| + | |||
| + | 参考:https://docs.microsoft.com/ja-jp/powershell/scripting/learn/deep-dives/everything-about-if?view=powershell-7.1 | ||
==dir存在判定== | ==dir存在判定== | ||
| 行16: | 行36: | ||
if (Test-Path .git) | if (Test-Path .git) | ||
{ | { | ||
| − | Write- | + | Write-Host "exists" |
} | } | ||
</pre> | </pre> | ||
.gitのdirがある | .gitのdirがある | ||
| + | |||
| + | ↓存在しないときの判定 | ||
| + | if (!(Test-Path .git)) | ||
==変数で定義したpathを使用== | ==変数で定義したpathを使用== | ||
| 行25: | 行48: | ||
$data = Get-Content $path | $data = Get-Content $path | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==実行ファイルのdirpath== | ==実行ファイルのdirpath== | ||
| 行40: | 行53: | ||
$ScriptDir = $MyInvocation.MyCommand.Path | $ScriptDir = $MyInvocation.MyCommand.Path | ||
echo $ScriptDir | echo $ScriptDir | ||
| − | C:\Users\hoge1\Desktop\hoge.ps1 | + | (windowsの場合) C:\Users\hoge1\Desktop\hoge.ps1 |
| + | (macの場合) /System/Volumes/Data/www/PowerShells/hoge.ps1 | ||
version3以上の場合 | version3以上の場合 | ||
$ScriptDir = $PSScriptRoot | $ScriptDir = $PSScriptRoot | ||
echo $ScriptDir | echo $ScriptDir | ||
| − | C:\Users\hoge1\Desktop | + | (windowsの場合) C:\Users\hoge1\Desktop |
| + | (macの場合) /System/Volumes/Data/www/PowerShells | ||
| + | |||
| + | macで使おうとしたが、初回は使えなかった。数日置いて確認した所使えたので、再起動などが、要因かもしれない。 | ||
参考:https://www.vwnet.jp/Windows/PowerShell/pwd.htm | 参考:https://www.vwnet.jp/Windows/PowerShell/pwd.htm | ||
| + | |||
| + | ==dirpathの親を取得== | ||
| + | Write-Host (get-item $ScriptDir).parent.FullName | ||
| + | |||
| + | ==ログ== | ||
| + | <pre> | ||
| + | function Log($msg) { | ||
| + | echo ($(Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " " + $msg) | ||
| + | } | ||
| + | Log "hogehogeを処理中です" | ||
| + | </pre> | ||
| + | |||
| + | > 2021-03-20 22:27:18 hogehogeを処理中です | ||
| + | |||
| + | ==コマンドの戻り値を取得== | ||
| + | 変数にそのまま入れれば良い。 | ||
| + | |||
| + | 例 | ||
| + | $proc = az storage blob sync | ||
| + | |||
| + | ==dir作成== | ||
| + | New-Item /tmp/hogehoge -ItemType Directory | ||
| + | |||
| + | ==dirがなければdir作成== | ||
| + | <pre> | ||
| + | if (!(Test-Path /tmp/hogehoge)) | ||
| + | { | ||
| + | New-Item /tmp/hogehoge -ItemType Directory | ||
| + | } | ||
| + | </pre> | ||
2021年9月2日 (木) 19:35時点における最新版
目次
変数
$hoge = "hogehoge"
判定
$condition = true
if ($condition)
{
Write-Host "OK"
}
比較
$num = 10
if ($num -eq 10) {
Write-Host 'OK'
} else {
Write-Host 'NG'
}
その他比較
if ($num -ne 10) # 等しくない if ( $value -gt 5 ) # < if ( $value -lt 5 ) # > if ( ($age -gt 13) -and ($age -lt 55) ) # and条件 if ( $age -le 13 -or $age -ge 55 )# and条件
dir存在判定
if (Test-Path .git)
{
Write-Host "exists"
}
.gitのdirがある
↓存在しないときの判定
if (!(Test-Path .git))
変数で定義したpathを使用
$path = "hoge.txt" $data = Get-Content $path
実行ファイルのdirpath
version3未満の場合
$ScriptDir = $MyInvocation.MyCommand.Path echo $ScriptDir (windowsの場合) C:\Users\hoge1\Desktop\hoge.ps1 (macの場合) /System/Volumes/Data/www/PowerShells/hoge.ps1
version3以上の場合
$ScriptDir = $PSScriptRoot echo $ScriptDir (windowsの場合) C:\Users\hoge1\Desktop (macの場合) /System/Volumes/Data/www/PowerShells
macで使おうとしたが、初回は使えなかった。数日置いて確認した所使えたので、再起動などが、要因かもしれない。
参考:https://www.vwnet.jp/Windows/PowerShell/pwd.htm
dirpathの親を取得
Write-Host (get-item $ScriptDir).parent.FullName
ログ
function Log($msg) {
echo ($(Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " " + $msg)
}
Log "hogehogeを処理中です"
> 2021-03-20 22:27:18 hogehogeを処理中です
コマンドの戻り値を取得
変数にそのまま入れれば良い。
例
$proc = az storage blob sync
dir作成
New-Item /tmp/hogehoge -ItemType Directory
dirがなければdir作成
if (!(Test-Path /tmp/hogehoge))
{
New-Item /tmp/hogehoge -ItemType Directory
}
