Is this the correct way to check for sudo access?

has_sudo() {
  if [[ "$EUID" = 0 ]]; then
      exit 0
  else
      sudo -k # make sure to ask for password on next sudo
      if sudo true; then
          exit 0
      else
          exit 1
      fi
  fi
}

load_zshenv_on_startup() {
  if has_sudo; then
    append_zshenv_to_global_files
  else
    link_zshenv_to_home_directory
  fi
}

It’s typically frowned upon to do “magic” like this.

Your program would have more predictable behavior if you implemented a --global argument to explicitly request the special actions instead.

Ask specific questions about how to code something in sh, bash, zsh, etc

General bash discussion on lemmy.ml

Create Post From:
lemmy.ml

  • 0 users online
  • 2 users / day
  • 2 users / week
  • 2 users / month
  • 5 users / 6 months
  • 17 subscribers
  • 9 Posts
  • 17 Comments
  • Modlog