Skip to content

Instantly share code, notes, and snippets.

@muratgozel
Last active February 6, 2019 12:46
Show Gist options
  • Save muratgozel/0e9181fb734ed6ab54686f5b4b3fa451 to your computer and use it in GitHub Desktop.
Save muratgozel/0e9181fb734ed6ab54686f5b4b3fa451 to your computer and use it in GitHub Desktop.
Some useful bash functions to check if something doesn't exist in Linux OS.
# check if user doesn't exist
function user_doesnt_exist {
if id -u "$1" >/dev/null 2>&1; then
return 1
else
return 0
fi
}
# usage. do something if the user abc doesn't exist.
if user_doesnt_exist abc; then
echo "abc user doesn't exist."
fi
# check if program doesn't exist
function program_doesnt_exist {
# set to 1 initially
local return_=1
# set to 0 if not found
type $1 >/dev/null 2>&1 || { local return_=0; }
# return value
return $return_
}
# usage. install node.js if it doesn't exist.
if program_doesnt_exist node; then
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt install -y nodejs
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment