学習項目 | 具体例 | Bash | Batch | PowerShell |
コマンド | 現在の日付・時刻を表示 | date '+%Y/%m/%d %T' | echo %DATE% %TIME% | Get-Date -Format G |
ディスク容量 | df -h | fsutil volume diskfree c: fsutil volume diskfree d: |
Get-PSDrive -PSProvider FileSystem | |
カレントディレクトリの情報取得 | ls | dir | Get-ChildItem | |
変数 | 文字列代入 |
HOGE="abcdef" |
SET HOGE="abcdef" |
$HOGE = "abcdef" |
コマンド実行結果代入 | HOGE=`date '+%Y/%m%d'` | for /f "usebackq" %A in (`type hoge.txt ^| find /c /v ""`) do set LINECOUNT=%A | $HOGE = get-date -Format G | |
配列 | 配列に文字列代入 | HOGE=(A B C) | SET /A HOGE=A B C | $HOGE = @(A B C) |
関数 | function MsgOutput() { Info=$0 Job=$1 Msg=$2 Day=`date '+%Y/%m/%d'` Tim=`date '+%T'` Hst=`uname -n` MsgOutput="${Day} ${Tim} ${Hst} ${Job}[$$]: ${Msg}" return ${MsgOutput} } MsgOutput I test "This process was successful." YYYY/MM/DD HH:MM:SS dev01 test[123456]: This process was successful. |
REM === メインシーケンス call :MsgOutput I test "This process was successful." exit /b REM === MsgOutput :MsgOutput SET Info=%1 SET Job=%2 SET Msg=%3 SET Day=%DATE% SET Tim=%TIME% for /f "usebackq" %A in (`hostname`) do set Hst=%A for /f "usebackq" %A in (`powershell "get-wmiobject win32_process -filter processid=$pid | foreach-object {$_.parentprocessid;}"`) do set PID=%A SET MsgOutput="%Day% %Tim% %Hst% %Job%[%PID%]: %Msg%" echo %MsgOutput% exit /b |
function MsgOutput () { PARAM($Info,$Job,$Msg) $Day = get-date -format G $Hst = hostname MsgOutput="${Day} ${Tim} ${Hst} ${Job}[$$]: ${Msg}" return ${MsgOutput} } MsgOutput I test "This process was successful." |
|
条件分岐 | if文 |
hoge="A" fuga="A" if [ $hoge = $fuga ]; then echo "文字列は同じです" else echo "文字列は違います" fi |
set hoge="A" set fuge="A" if %hoge% == %fuge% ( echo "文字列は同じです" ) else ( echo "文字列は違います" ) |
$hoge = "A" $fuga = "A" if ( ${hoge} = ${fuga} ) { echo "文字列は同じです" } else { echo "文字列は違います" } |
case文 | $hoge = "A" case ${hoge} in A) echo "One." ;; B) echo "Two." ;; C) echo "Three." ;; *) echo "Not matched." ;; esac |
ない | $hoge = "A" switch (${hoge}) { A {"One."} B {"Two."} C {"Three."} default {"Not matched."} } |
|
文字列操作 | 特定区切り文字の2つ目表示 | echo A,B,C > test.csv cat test.csv | awk -F, '{print $2}' |
ない | echo A,B,C > test.csv cat test.csv | %{ $_.Split(",")[1] } |
繰り返し処理 | for | for ((i=0; i<5; i++)); do echo [$i] done |
@echo off set num=0 for /L %i in (1, 1, 10) do ( set /a num=num+1 ) @echo on pause |
for ( $i = 0; $i -lt 5; $i++ ) { echo ("[" + $i + "]") } |