Shammer's Philosophy

My private adversaria

ShellScript条件分岐ーif文-20101010

ShellScript条件分岐ーif文-20100921 - Shammerismのアップデート版。ファイルの比較を追加。

#!/bin/bash

echo "IF example!";

if [ $# -ne 1 ];then
  echo "Usage: $0 [123]";
  exit 1;
fi

#
# Compare numbers
#
if [ $1 -eq 1 ];then
  echo "You are number#1.";
elif [ $1 -eq 2 ];then
  echo "You are number#2.";
elif [ $1 -eq 3 ];then
  echo "You are number#3.";
else
  echo "Invalid argument.";
  echo "Usage: $0 [123]";
fi

#
# Compare Strings
#
if [ $1 = "XYZ" ]; then
  echo "You type XYZ.";
fi

if [ $1 != "ABC" ]; then
  echo "You don't type ABC.";
fi

#
# Check Files
#
if [ -d "$HOME/bin" ];then
  echo "$HOME/bin is a directory.";
elif [ -e "$HOME/exist.txt" ]; then
  echo "$HOME/exist.txt is exist.";
fi

#
# Compare File size
#
FILE_A_SIZE=`stat -c %s $FILE_A`
FILE_B_SIZE=`stat -c %s $FILE_B`
if [ $FILE_A_SIZE -gt $FILE_B_SIZE ];then
  echo "$FILE_A is bigger than $FILE_B.";
elif [ $FILE_A_SIZE -lt $FILE_B_SIZE ];then
  echo "$FILE_A is smaller than $FILE_B.";
else 
  echo "$FILE_A and $FILE_B is same size.";
fi

exit 0;