![如何从 if 语句中排除 bash 脚本案例](https://linux22.com/image/1017327/%E5%A6%82%E4%BD%95%E4%BB%8E%20if%20%E8%AF%AD%E5%8F%A5%E4%B8%AD%E6%8E%92%E9%99%A4%20bash%20%E8%84%9A%E6%9C%AC%E6%A1%88%E4%BE%8B.png)
我在编写 bash 脚本时遇到了一些问题(用于显示服务器中的所有磁盘 WWN,无论是满的还是短的)。
我需要以 root 身份运行脚本,但因为不建议在脚本本身中使用 root,所以我创建了一个 if 语句来检查脚本是否以 root 身份运行。
从那里,else 命令按预期运行脚本本身。这工作正常,但我的一个案例选项 (-h) 用于显示帮助信息,现在只能在脚本以 root 身份运行时运行,但理想情况下,当不以 root 身份运行时也应该可以使用它。
#! /bin/sh
name=`basename "$0"`
usage() {
cat << EOT
Error: arguments required
Usage: ${name} option
Options:
-h: Show this information
-f: Show full disk name as used in zpool
-s: Show last 8 characters (used as disk labels on the server)
EOT
}
if (( $EUID != 0 )); then
echo "Please run as root"
exit
else
while [ -n "$1" ]; do
case "$1" in
-f) echo | sudo format | awk '/[[:space:]]+c[0-9]+(d|t)[0-9]+/ {print $2}' | egrep '^.{8,25}';
break;;
-s) for disk in $(echo | sudo format | awk '/[[:space:]]+c[0-9]+(d|t)[0-9]+/ {print $2}' | egrep '^.{8,25}'); do
sudo /opt/csw/sbin/smartctl -i -d scsi /dev/rdsk/"$disk" | grep "Logical Unit id:" | sed -r 's/Logical Unit id: 0x//' | grep -Eo '.{8}$' | tr '[a-z]' '[A-Z]'
done ;
break;;
-h) usage
break;;
*) echo "For help: run ${name} -h";;
esac
shift
done
fi
if [[ $# -eq 0 ]] ; then usage
exit 0
fi
非常感谢您的帮助!
答案1
虽然我没有尝试过bac0n的答案,但是在阅读他的评论之前我找到了一个解决方案:
我根据这个检查创建了一个函数,并在需要的情况下调用该函数。我还修复了按钮上没有参数的 if 语句也导致问题的问题。
以下是我对此问题的解决方案:
#! /bin/sh
name=`basename "$0"`
usage() {
cat << EOT
Error: arguments required
Usage: ${name} option
Options:
-h: Show this information
-f: Show full disk name as used in zpool
-s: Show last 8 characters (used as disk labels on the server)
EOT
}
check_root()
{
myuid=$(id -u)
if [ $myuid != '0' ]; then
echo "This script requires root privilege"
exit 1
fi
}
if [[ $# -eq 0 ]] ; then usage
exit 0
fi
while [ -n "$1" ]; do
case "$1" in
-f)
check_root
echo | format | awk '/[[:space:]]+c[0-9]+(d|t)[0-9]+/ {print $2}' | egrep '^.{8,25}';
;;
-s)
check_root
for disk in $(echo | format | awk '/[[:space:]]+c[0-9]+(d|t)[0-9]+/ {print $2}' | egrep '^.{8,25}'); do
/opt/csw/sbin/smartctl -i -d scsi /dev/rdsk/"$disk" | grep "Logical Unit id:" | sed -r 's/Logical Unit id: 0x//' | grep -Eo '.{8}$' | tr '[a-z]' '[A-Z]'
done ;
;;
-h) usage
;;
*)
echo "For help: run ${name} -h";;
esac
shift
done
我使用 id -u 而不是 $EUID 变量,因为必须将其移植到不同的发行版,而且可能无法在任何地方工作
答案2
使用获取选项解析命令选项:
#!/bin/bash
SCRIPT_NAME=$(basename $BASH_SOURCE)
usage() {
cat << EOT
Usage: $SCRIPT_NAME option
Options:
-h: Show this information
-f: Show full disk name as used in zpool
-s: Show last 8 characters (used as disk labels on the server)
EOT
exit 0
}
# check for legal options.
opt=0
out=$(getopt -o hfs -l help,full,show -u -q -n $BASH_SOURCE -- "$@")
ret=$?
set -- $out
for i in $@; do
case $i in
-f|--full)
((opt |= 1)) ;;
-s|--show)
((opt |= 2)) ;;
-h|--help)
usage ;;
--)
break ;;
esac
done
if ((ret > 0 || opt == 0)); then
echo "$0: invalid option"
echo "Try '$0 --help' for more information."
exit 0
fi
if (( $EUID != 0 )); then
echo "This command require root privileges."
exit 0
fi
# code goes here.
if ((opt & 1)); then
echo "full"
fi
if ((opt & 2)); then
echo "show"
fi