有时需要检查哪个用户正在执行bash脚本,以及用户是否提供了所有必需的参数:
#!/bin/bash display_usage() { echo "This script must be run with super-user privileges." echo -e "\nUsage: $0 [arguments] \n" } # if less than two arguments supplied, display usage if [ $# -le 1 ] then display_usage exit 1 fi # check whether user had supplied -h or --help . If yes display usage if [[ ( $# == "--help") || $# == "-h" ]] then display_usage exit 0 fi # display usage if the script is not run as root user if [[ "$EUID" -ne 0 ]]; then echo "This script must be run as root!" exit 1 fi echo "All good !!!"
输出:
$./script.sh 1 This script must be run with super-user privileges. Usage: ./script.sh [arguments] $su Password: # ./script.sh 1 2 All good !!!
日期:2020-07-07 20:54:29 来源:oir作者:oir