「Debian 11」シェルスクリプトにtestコマンドAND条件式のサンプル

2022年1月25日

環境
Debian 11.2
書式
if test 条件式1 -a 条件式2; then
処理コード
fi
**-a は「AND(かつ)」を意味し、前後の条件式が両方成り立つ場合のみ「真」**となります。

使用例
1.スクリプトを作成します
root@debian:/data# vi 100.sh
以下の内容を入力します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
if test 22 = 22 -a 56 -eq 56
then
echo "tokyo"
else
echo "oosaka"
fi
#!/bin/bash if test 22 = 22 -a 56 -eq 56 then echo "tokyo" else echo "oosaka" fi
#!/bin/bash

if test 22 = 22 -a  56 -eq 56
 then
  echo "tokyo"
 else
  echo "oosaka"
fi

2.スクリプトファイルの権限を付与します
root@debian:/data# chmod u+x 100.sh

3.スクリプトファイルを実行します
root@debian:/data# ./100.sh
tokyo

shellscript

Posted by arkgame