What is idempotency?
Idempotent scripts can be called multiple times and each time it’s called, it will have the save effects on the system. This means, a second call will exit with the same result and won’t have any side effects.
Examples
Creating an empty file
touch example.txtCreating a directory
mkdir -p mydirCreating a symbolic link
ln -sfn source targetRemoving a file
rm -f example.txtCheck if variable, file or dir exists
if [ ! -f "/etc/conf/foo.txt" ]; then
echo "ttt" > /etc/conf/foo.txt
fiOther flags:
-ffile-ddirectory-zstring of zero length-ppipe-xfile and has execute permission,-x "$(command -v op)"checkopbinary is or not installed in your machine
Use if for conditional choice
if [ "$Name" != $USER] ...Use "$Name" rather than $Name.
If $Name is empty, bash sees the above condition as
if [ != $USER] ...Thus add "" is the safe way to use variable in condition.