module Dog
def dog
'Bow wow'
end
end
class IncludeDog
include Dog
end
class ExtendDog
extend Dog
end
include_dog = IncludeDog.new
include_dog.cry
=> "Bow wow"
ExtendDog.cry
=> "Bow wow"
module Dog
def cry
'module method'
end
end
class PrependDog
prepend Dog
def cry
'prepend dog'
end
end
prepend_dog = PrependDog.new
prepend_dog.cry
=> "module method"
# PrependDogの親クラス一覧 一番左が自分のクラス、右が親クラス
PrependDog.ancestors
=> [Dog, PrependDog, ..., Object, BasicObject]
--- copyFile.sh
#!bin/bash
#bash記述のため、cd等実行エラー時強制終了するため保護
#想定外の事故をなるべく回避するため記載
set -eu
if [ $# != 2 ]; then
echo "引数不足、または引数過多です。コピー前とコピー後のファイル名を入力してください。"
exit
fi
#シェル自身のディレクトリを格納する。
dirPath=$(dirname $(readlink -f $0))
#半角スペースが含まれるディレクトリの場合は""でくくることで引数の区切りとして認識されなくなる
cd "${dirPath}"
if [ -e "$1" ] && [ ! -e "$2" ]; then
cp -a "${dirPath}/$1" "${dirPath}/$2"
echo "${dirPath}/$1を${dirPath}/$2にコピーしました。"
elif [ ! -e "${dirPath}/$1" ]; then
echo "コピー元のファイルがありません。"
else
echo "コピー先のファイルが既に存在します。上書きを防ぐため、実行を中止しました。"
fi
引数が2つぴったりでないと実行しない、ファイルをコピーするシェルスクリプトです。引数が2つ以外の時は、最初のif文で警告文を表示し、その後exitで実行を終了します。 処理するファイルの基準はシェルの置いてあるディレクトリとします。そのためシェルのディレクトリ位置を取得し、 cd コマンドで移動しています。