1
RAII를 에뮬레이트하기위한 모범 사례를 찾고 있는데, bash에서 기능으로 'finally'를 찾고 있습니다.RAII 또는 bash의 'finally'에뮬레이션
트랩 메커니즘을 사용해도 괜찮지 만 모든 것이 서브 쉘에서 이루어져야합니다. 나는 스크립트를 스스로 정리하는 함수로 분해 할 수있는 방법을 찾고있다.
예를 들면, 다양한 오류 상황이 발생할 수있는 동안 디렉토리를 만들고 파일 시스템을 마운트하고 싶다고 말하면됩니다.
my_func() {
local mnt=$(mktemp -d)
$MAGIC 'rmdir $mnt' # RIAA-style cleanup
mount /dev/disk $mnt
$MAGIC 'umount $mnt' # another RIAA-style cleanup
if [ $foo blash ]; then
echo "Nah, $foo is blash, please blargh!"
return 1
fi
...
if [ ! -f $mnt/file ]; then
echo "File does not exist, blargh!"
return 1
fi
...
if ! grep -q 'blargh!!!' $mnt/file; then
echo "Blargh!!! not found, blargh!"
return 1
fi
...
echo "Success, nice!"
...
}