Last active
January 2, 2022 03:23
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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