ในคำตอบนี้ให้มันเป็นที่ชัดเจนผมเข้าใจผู้อ่านสามารถอ่านbashและPOSIXdashสคริปต์เปลือกเช่น
ฉันเชื่อว่าไม่มีคำอธิบายมากมายที่นี่เนื่องจากคำตอบที่ได้รับการโหวตอย่างสูงทำหน้าที่อธิบายได้ดีมาก
แต่ถ้ามีอะไรจะอธิบายเพิ่มเติมอย่าลังเลที่จะแสดงความคิดเห็นฉันจะพยายามอย่างเต็มที่เพื่อเติมช่องว่าง
ปรับbash
โซลูชั่นใหม่:
# bool function to test if the user is root or not
is_user_root () { [ ${EUID:-$(id -u)} -eq 0 ]; }
เกณฑ์มาตรฐาน (บันทึกเป็นไฟล์is_user_root__benchmark)
###############################################################################
##                          is_user_root() benchmark                         ##
##                  Bash is fast while Dash is slow in this                  ##
##          Tested with Dash version 0.5.8 and Bash version 4.4.18           ##
##                     Copyright: 2020 Vlastimil Burian                      ##
##                      E-mail: info@vlastimilburian.cz                      ##
##                             License: GPL-3.0                              ##
##                               Revision: 1.0                               ##
###############################################################################
# intentionally, the file does not have executable bit, nor it has no shebang
# to use it, please call the file directly with your shell interpreter like:
# bash is_user_root__benchmark
# dash is_user_root__benchmark
# bool function to test if the user is root or not
is_user_root () { [ ${EUID:-$(id -u)} -eq 0 ]; }
# helper functions
print_time   () { date +"%T.%2N"; }
print_start  () { printf '%s' 'Start  : '; print_time; }
print_finish () { printf '%s' 'Finish : '; print_time; }
readonly iterations=10000
printf '%s\n' '______BENCHMARK_____'
print_start
i=1; while [ $i -lt $iterations ]; do
    is_user_root
    i=$((i + 1))
done
print_finish
ทางออกเดิม:
#!/bin/bash
is_user_root()
# function verified to work on Bash version 4.4.18
# both as root and with sudo; and as a normal user
{
    ! (( ${EUID:-0} || $(id -u) ))
}
if is_user_root; then
    echo 'You are the almighty root!'
else
    echo 'You are just an ordinary user.'
fi
^^^ ทางออกที่ถูกขีดฆ่าได้รับการพิสูจน์แล้วว่าไม่เร่งความเร็ว แต่มันก็ใช้เวลานานมากดังนั้นฉันจะเก็บมันไว้ที่นี่ตราบใดที่ฉันเห็นว่าจำเป็น
คำอธิบาย
เนื่องจากมันเร็วกว่าที่จะอ่านตัวแปร$EUIDมาตรฐานbashจำนวน ID ผู้ใช้ที่มีประสิทธิภาพมากกว่าเรียกใช้id -uคำสั่งเพื่อPOSIX -ค้นหา ID ผู้ใช้โซลูชันนี้จึงรวมทั้งสองอย่างเข้าด้วยกันเป็นฟังก์ชันที่อัดแน่น หากและถ้าหาก$EUIDไม่ว่าด้วยเหตุผลใดก็ตามid -uคำสั่งจะได้รับการดำเนินการเพื่อให้มั่นใจว่าเราได้รับมูลค่าที่เหมาะสมไม่ว่าจะอยู่ในสถานการณ์ใด
ทำไมฉันโพสต์โซลูชั่นนี้หลังจากหลายปี OP ได้ถาม
ถ้าฉันเห็นอย่างถูกต้องดูเหมือนว่าจะไม่มีโค้ดข้างต้น
คุณจะเห็นว่ามีหลายตัวแปรที่ต้องนำมาพิจารณาและหนึ่งในนั้นคือการรวมประสิทธิภาพและความน่าเชื่อถือ
โซลูชันPOSIXแบบพกพา+ ตัวอย่างการใช้งานฟังก์ชั่นด้านบน
#!/bin/sh
# bool function to test if the user is root or not (POSIX only)
is_user_root() { [ "$(id -u)" -eq 0 ]; }
if is_user_root; then
    echo 'You are the almighty root!'
    exit 0 # unnecessary, but here it serves the purpose to be explicit for the readers
else
    echo 'You are just an ordinary user.' >&2
    exit 1
fi
ข้อสรุป
เท่าที่คุณอาจจะไม่ชอบสภาพแวดล้อม Unix / Linux นั้นมีความหลากหลายมาก หมายความว่ามีคนที่ชอบbashมากพวกเขาไม่ได้คิดถึงการพกพา ( POSIX shells) คนอื่น ๆ เช่นฉันชอบเชลล์POSIX ทุกวันนี้เป็นเรื่องของการเลือกและความต้องการส่วนตัว
               
              
id -uผลตอบแทน0สำหรับรูท