Skip to content

Instantly share code, notes, and snippets.

@shouya
Last active December 12, 2015 01:38
Show Gist options
  • Save shouya/4692153 to your computer and use it in GitHub Desktop.
Save shouya/4692153 to your computer and use it in GitHub Desktop.
A wrapper of init script that does cleaning work and correct the mistyped init-level caused by excitement.
#!/bin/sh
##
## This file is for replace the typical init program, do some
## pre-checking work before shutting down or other stuff.
##
## Written by Shou Ya, at 23 June, 2012
##
##
tmp_dir="$HOME/tmp"
original_init="/sbin/init"
default_shell="$SHELL"
[ -z $default_shell ] && default_shell="/bin/bash"
user="shou"
tmp_area_empty_p() {
[ -z "`ls -A $tmp_dir/ 2>/dev/null`" ]
}
new_shell() {
old_pwd=`pwd`
initial_directory="$1"
cd "$initial_directory"
echo ""
sudo -u "$user" "$default_shell"
cd "$old_pwd"
}
run_level_correct() {
run_level="$1"
case $run_level in
9) echo 0;;
o) echo 0;;
*) echo $run_level
esac
}
orig_init() {
$original_init "$@"
}
if ! tmp_area_empty_p; then
echo ">> Note: tmporary area is not empty! <<"
echo -n "Do you want to check them now? [Yn] "
read answer
case $answer in
N*|n*);;
*|Y*|y*)
echo "Okay, now I'll bring you to the directory."
new_shell $tmp_dir
tmp_area_empty_p && orig_init `run_level_correct "$@"`
exit 0
;;
esac
fi
orig_init `run_level_correct "$@"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment