【发布时间】:2014-09-13 17:31:56
【问题描述】:
我看过很多关于如何使用getopts 的例子。但是我非常了解 bash,但我无法在我的情况下实现它。如果任何专家可以向我展示模板,我真的很感激。
我有一个最少 6 和最多 10 输入的脚本。以下是简要说明:
script.sh -P passDir -S shadowDir -G groupDir -p password -s shadow
用户必须为-P -S -G 提供参数,否则我必须显示使用情况并关闭程序。如果提供了参数,我需要将它们保存到适当的变量中。
但是-p 和-s 是可选的。但是,如果没有-p 我应该做一些任务,如果没有-s 我应该做一些其他任务,如果它们都不存在我应该做一些其他任务。
以下是我到目前为止所写的内容,但它存在于 for 循环中。
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Only root may add a user to system"
exit 2
else
usage() { echo "Usage: $0 [-P <password file path>] [-S <shadow file path>] [-G <group path>]" 1>&2; exit 1; }
passDir=""
shadowDir=""
groupDir=""
while getopts ":P:S:G:" inp; do
case "${inp}" in
P)
$passDir = ${OPTARG};;
S)
$shadowDir = ${OPTARG};;
G)
$groupDir = ${OPTARG};;
*)
usage;;
esac
done
echo "${passDir}"
echo "${shadowDir}"
echo "g = ${groupDir}"
fi
目前用户没有输入参数,不会显示任何内容,如果有参数,它会进入循环!
【问题讨论】: