Skip to content

Instantly share code, notes, and snippets.

@moznion
Last active January 2, 2022 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moznion/99b5342bb208beee0e301b39ed5643bc to your computer and use it in GitHub Desktop.
Save moznion/99b5342bb208beee0e301b39ed5643bc to your computer and use it in GitHub Desktop.
A runner script that runs a shell script with putting IMMUTABLE file attribute dynamically.
#!/bin/bash
# usage:
# sh-run.sh script-you-want-to-run.sh args...
set -ue
file="$1"
should_remove_immutable_attr=0
attr=$(lsattr "$file" | awk '{print $1}')
if [ "${attr:4:1}" != "i" ]; then
# given file doesn't have IMMUTABLE attribute, so give that dynamically.
should_remove_immutable_attr=1
sudo chattr +i "$file"
fi
set +e
bash "$file" ${@:2:($#-1)}
ret=$?
set -e
if [ $should_remove_immutable_attr -eq 1 ]; then
# clean up the IMMUTABLE attribute if the original file hadn't had that.
sudo chattr -i "$file"
fi
exit $ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment