LoginSignup
6
8

More than 5 years have passed since last update.

【学習メモ】Bashとコマンドプロンプトの対応表

Last updated at Posted at 2019-01-22

はじめに

バッチファイルを書くときの忘備録。
Powershellも呼び出します。
bash: 4.2.46
cmd:10.0.17134.1

コマンド

内容 Bash コマンドプロンプト
実行結果を標準出力に出さない 行末に>/dev/null 行末に>nul
コマンドを非表示に (表示されない) 行頭に@ または @echo off
時刻 echo `date +"%Y%m%d".%H%M` echo %date:~0,4%%date:~5,2%%date:~8,2%.%time:~0,2%%time:~3,2%
引数 $1 $2 $3 $* %1 %2 %3 %*
改行 echo echo .
改行エスケープ \ ^
スリープ sleep N timeout N /nobreak
パイプでgrep |grep A | findstr A
grep A or B grep -E "A|B" findstr "A B"
grep "A B C" grep "A B C" findstr /C:"A B C"
サービスの操作 systemctl (start/stop/status) hoge sc (start/stop/query) "hoge"
コメント # rem
関数の宣言 function functionname(){} :functionname
関数の呼び出し functionname call :functionname
コマンドの実行(終了を待つ) ./hoge.sh call hoge.bat
コマンドの実行(別窓) ./hoge.sh & start hoge.bat
子スクリプト/ラベルの終了 (なし) exit /b
子スクリプト/ラベルの終了ステータスコード (なし) exit /b 0
  • ラベルはcall :check1 && :check2みたいな使い方ができるので関数に分類。

比較演算子とか

内容 Bash コマンドプロンプト
直前の終了コードで分岐 [ "$?" = 0 ] && CMD if %errorlevel% == 0 CMD
ファイルの有無 [ -e $filepath ] && CMD if exist filepath CMD

ネットワーク

内容 Bash コマンドプロンプト
curl curl hoge.hoge powershell Invoke-RestMethod -Uri "http://hoge.hoge/" -Method GET
ifup ifup eth0 netsh interface set interface "Ethernet0" enable
ifdown ifdown eth0 netsh interface set interface "Ethernet0" disable

参考
Windowsにcurlが無い?いいえ、Invoke-RestMethodがあります。GrailsアプリへのPOSTリクエストを試す

Windows特有のもの

内容 Bash コマンドプロンプト
vbsの実行 (なし) cscript hoge.vbs
画面のロック (なし) rundll32 user32.dll, LockWorkStation
バッチファイルの場所にカレントを移動 (なし) cd %~dp0

vbsでメール送信

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8