1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.batファイル(Windowsのコマンドプロンプト等から起動するバッチファイル)の書き方メモ

Posted at

定番

  • REM でコメント
  • @echo off で実行するコマンドを非表示
  • cd /d %~dp0 でスクリプトが置かれている場所をカレントディレクトリにする

環境変数

  • 環境変数はset VAR=abc で設定して、%VAR%で参照する
  • 実行結果を変数に入れるには、for文を使う。その中で環境変数を使う場合は、%%VAR%%のように、%を2つ書く
for /f "usebackq delims=" %%a in (`az acr show --name %%VAR%% --query id --output tsv`) do set ACR_ID=%%a

便利な使い方

  • 他のバッチファイルをサブルーチンとして呼び出すには、callを使う。例:call a.bat

  • 複数行にまたがる時は、行末に^を書く

  • 現在時刻の表示は、time /T

  • 指定秒数ウェイトを入れるには、timeout 10 で10秒待つ

  • 明示的にバッチファイルを終了してコマンドプロンプトに戻るには、exit /b 0。単にexit 0と書くと、コマンドプロンプトを終了して閉じてしまう。

  • where コマンドで、プログラム(コマンド)がどこにあるか探す。Linuxの which 。

その他

  • azコマンドは.cmdファイルなので、call az aks get-versions のようにcallで呼ばないと、コマンドプロンプトを終了して閉じてしまう。
  • kubectlやhelmは.exeファイルなので、そのまま書けばOK。
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?