On linux based some commands that you install without root like brew packages and you sometimes need to run them with sudo for that, I made this script
#!/bin/bash
secure_path=("/usr/local/sbin" "/usr/local/bin" "/usr/sbin" "/usr/bin" "/sbin/bin" "/snap/bin") # snap for ubuntu based
if [[ ! $(which $1) ]]; then
$@ # if does not exist, run them and get error of command not exist and get exit 1
exit 1 # just in case program moves to next line, exit 1
fi
global_found=
# some would say just do sudo $@ but what if after exitting the app/command, it return a non-0 status? so we better do this
for item in "${secure_path[@]}"; do
# if found stop
if [[ $(which $1) == $item/$1 ]]; then
echo exist $item/$1
global_found=1
break
fi
done
if [[ ! $global_found ]]; then
sudo -E $(which $1) $(echo $@ | sed 's/'$1' //') # trim 1st argument of our script
else
sudo $@ # $@ means all bash script arguments
fi
Top comments (0)