sudo is the command to allow normal users perform some escalated operations. We can use the following BASH function to check to see if the sudo command is available. This is done via the which command which searches the command in the $PATH path.
1 2 3 4 5 6 7 8 9 | # 0 - ok # 1 - nok is_sudo_available() { local result=1 which sudo 2>&1 1>/dev/null [ $? -eq 0 ] && result=0 return $result } |
# 0 - ok # 1 - nok is_sudo_available() { local result=1 which sudo 2>&1 1>/dev/null [ $? -eq 0 ] && result=0 return $result }
This function works in BASH script on Linux platforms and also the Windows Subsystem for Linux or Cygwin.
BASH Programming/Shell
- How to Get HTTP Response Code using cURL Command?
- Three Interesting/Fun BASH Commands
- One Interesting Linux Command (Steam Locomotive in BASH)
- Simple Bash Function to Repeat a Command N times with Retries
- How to Extract a Domain Name from a Full URL in BASH?
- BASH Function to Compute the Greatest Common Divisor and Least Common Multiples
- BASH Function to Get Memory Information via AWK
- BASH Function to Escape Parameters Argument
- BASH Function to Check if sudo Available
- Full Permutation Algorithm Implementation in BASH
- BASH Function to Install Docker
- A Simple BASH Function/Script to Run a Command in Background
- BASH Script to Compute the Average Ping to a Domain
- A Bash Process Controller to Start, Stop and Restart an Daemon Program
- Linux BASH shell - Echo This if you get angry
- Bash SHELL, Chess Board Printing
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
169 wordsloading...
Last Post: Teaching Kids Programming - Find the Difference of Two Almost Same Strings
Next Post: Teaching Kids Programming - Concatenation of Arrays