0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

2025 1/2 学習記録

Posted at

bash if

記述方法

if test 条件文; then 実行処理(条件文が正の場合) fi

又は、
if [条件文]; then 実行処理(条件文が正の場合) fi

又は、
if [[条件文]]; then 実行処理(条件文が正の場合) fi

比較演算子が特殊。

  • -eq=
  • -ne!=
  • -lt<
  • -le<=
  • -gt>
  • -ge>=
    非常に分かりづらい印象。

and, or

複数の条件文を全て満たす場合に実行

  • if [条件文1] && [条件文2]
  • if [ 条件文1 -a 条件文2 ]
  • if [[ 条件文1 && 条件文2 ]]
  • if test 条件文1 && test 条件文2;

orで複数の条件式を満たす場合に実行

  • if [条件文1] || [条件文2]
  • if [ 条件文1 -o 条件文2 ]
  • if [[ 条件文1 || 条件文2 ]]
  • if test 条件文1 || test 条件文2;

否定を表す条件文

  • if ! test 条件文
  • if [!条件文]
  • if [[!条件文]]

実行の条件

  • cmd1 && cmd2
    cmd1が正しく実行されたらcmd2を実行
  • cmd1 || cmd2
    cmd1がエラーならcmd2を実行
  • cmd1 && cmd2 || cmd3
    cmd1が正しく実行されたらcmd2が実行され、cmd1がエラーならcmd3を実行

viエディタでキーボード操作

選択

  • 行単位でコピーする場合:yyコマンドを使用
  • 複数行をコピーする場合:コピーしたい行数を数値で入力
    → その後、yy と入力
  • 文字単位でコピーする場合:vキーを押して文字選択モードに入り、カーソルキーで範囲を選択
    → その後、y キーの入力で、コピーを実行
    → その後、p キーと入力すると、現在のカーソル位置の後ろにペーストされる

操作の取り消し

  • undo操作; u キーの入力で、一つ前の状態に戻す
  • redo操作; Ctr+rキーの入力で、undo操作を取り消し、再適用する

ファイルの存在チェック

  • if [-e ファイル名 ];
    ファイルもしくはディレクトリの存在確認
  • if [ -f ファイル名 ]
    ディレクトリではなくファイルが存在するか
  • if [ -d ファイル名 ]
    ディレクトリが存在するか
  • if [ -s ファイル名 ]
    ディレクトリ or 中身のあるファイルか
  • if [ -w ファイル名 ]
    書き込み権限があるか
  • if [ -x ファイル名 ]
    実行権限があるか
  • if [ ファイルA -nt ファイルB ]
    ファイルAがファイルBより新しいか
  • if [ ファイルA -ot ファイルB ]
    ファイルAがファイルBより古いか

Macでは、ファイルの日付を指定する -d オプションは使用できないらしい。

→ 代わりに -t オプションを使用する。

  • 例:touch -t YYYYMMDDhhmm.ss sample2

今日はここまで!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?