我正在编写一个包含getopts
的脚本,它可以游泳:
#!/bin/bash
# Define help function for options
function help(){
echo
echo -e $YELLOW'--->COMMAND LINE OPTIONS:'$ENDCOLOR
echo
echo "Usage example:";
echo "script_name [(-i|--install) string] [(-u|--uninstall) string] [(-b|--backup) string] [(-r|--restore) string] [(-m|--manualupdate) string] [(-p|--passwordreset) string] [(-a|--accessdetails) string] [(-t|--updatetoolkit)] [(-U|--updateall)]";
echo "Options:";
echo
echo "-i or --install string: Install an app.";
echo "-u or --uninstall string: Uninstall an app.";
echo "-b or --backup string: Backup an the config file for an app.";
echo "-r or --restore string: Restore an app config file from backup.";
echo "-m or --manualupdate string: Manually update a specific app.";
echo "-p or --passwordreset string: Reset the password to an app.";
echo "-a or --accessdetails string: View the access details for an app.";
echo "-t or --updatetoolkit: Update the toolkit.";
echo "-U or --updateall: Update Linux and all apps.";
exit 1;
}
# Reset option vars
updatetoolkit=0;
updateall=0;
# Capture options to getopts
export ARGS=$(getopt -o "i:u:b:r:m:p:a:tUh" -l "install:,uninstall:,backup:,restore:,manualupdate:,passwordreset:,accessdetails:,updatetoolkit,updateall,help" -n "script_name" -- "$@");
if [ $? -ne 0 ];
then
help;
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-i|--install)
shift;
if [ -n "$1" ];
then
install="$1";
source $SCRIPTSOURCE/$1/$1-installer.sh
shift;
fi
;;
-u|--uninstall)
shift;
if [ -n "$1" ];
then
uninstall="$1";
source $SCRIPTSOURCE/$1/$1-uninstaller.sh
shift;
fi
;;
-b|--backup)
shift;
if [ -n "$1" ];
then
backup="$1";
source $SCRIPTSOURCE/$1/$1-constants.sh
source $SCRIPTPATH/inc/app-backup-controller.sh
shift;
fi
;;
-r|--restore)
shift;
if [ -n "$1" ];
then
restore="$1";
source $SCRIPTSOURCE/$1/$1-constants.sh
source $SCRIPTPATH/inc/app-restore-controller.sh
shift;
fi
;;
-m|--manualupdate)
shift;
if [ -n "$1" ];
then
manualupdate="$1";
source $SCRIPTSOURCE/$1/$1-updater.sh
shift;
fi
;;
-p|--passwordreset)
shift;
if [ -n "$1" ];
then
passwordreset="$1";
source $SCRIPTSOURCE/$1/$1-constants.sh
source $SCRIPTPATH/inc/app-password-reset.sh
shift;
fi
;;
-a|--accessdetails)
shift;
if [ -n "$1" ];
then
accessdetails="$1";
source $SCRIPTSOURCE/$1/$1-constants.sh
source $SCRIPTPATH/inc/app-access-details.sh
shift;
fi
;;
-t|--updatetoolkit)
shift;
updatetoolkit="1";
source $SCRIPTPATH/maintenance/update.sh
;;
-U|--updateall)
shift;
updateall="1";
source $SCRIPTPATH/maintenance/distro-update.sh
;;
--)
shift;
break;
;;
esac
done
# Check required arguments
但是,每当我使用参数运行脚本时,它会执行它应该执行的操作,但它会在终端中打印参数,即:
-p
Remaining code...
我该如何防止这种情况?