允许您启动和停止应用程序的 Linux shell 脚本

允许您启动和停止应用程序的 Linux shell 脚本

(编者注:以下是谷歌翻译的文本)

向你问好。我想创建一个允许启动或停止应用程序的 shell 脚本。 shell 脚本将采用启动或停止参数来分别启动或停止应用程序。显然,所涉及的应用程序必须在我们的 shell 脚本中指出。

预先感谢您..我对所有建议持开放态度


法语原文:

你好。请使用 shell 脚本来创建应用程序或下载应用程序。脚本 shell 会根据参数启动或停止相应的应用程序或停止应用程序。 bien évidemment l'application en question doit être indiquée dans notre script shell..

Merci d'avance .. je suis ouvert à toutes 提议

答案1

鉴于下面的 bash-script-template,您可以

  • 添加长选项--start--stop设置变量并在 if 语句中使用它们。
  • 您可以检查startstop是否在${remaining_args[@]},然后使用该信息:
if printf '%s\n' ${remaining_args[@]} | grep -Pq '^start$' ; then
  start your app
elif printf '%s\n' ${remaining_args[@]} | grep -Pq '^stop$' ; then
  stop your app
fi

这是模板:

#!/bin/bash - 
#===============================================================================
#
#          FILE: <filename here>
#
#   DESCRIPTION: 
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: 
#  ORGANIZATION: 
#       CREATED: 
#       LICENSE: 
#      REVISION:  ---
#===============================================================================

#=== Init ======================================================================
set -o nounset   # exit on unset variables.
set -o errexit   # exit on any error.
set -o errtrace  # any trap on ERR is inherited
#set -o xtrace    # show expanded command before execution.

unalias -a       # avoid rm being aliased to rm -rf and similar issues
LANG=C           # avoid locale issues
VERBOSE=         # Don't be verbose, unless given '-v'-option

ScriptVersion="1.0"

trap "cleanup" EXIT SIGTERM SIGINT

#=== Functions =================================================================
usage () {
  echo "

  Usage :  ${0##/*/} [options] [--]

  Options:
  -h|--help     Display this message
  -V|--version  Display script version
  -v|--verbose  Print informational text

  "
  exit 0
} # ----------  end of function usage  ----------

option_handling () {
  # see /usr/share/doc/util-linux/examples/getopt-parse.bash
  OPTS=$(getopt --name "$0" \
    --options 'hVv' \
    --longoptions 'help,version,verbose' \
    --shell bash \
    -- "$@") \
    || (echo; echo "See above and try \"$0 --help\""; echo ; exit 1)

  eval set -- "$OPTS"
  unset OPTS

  while true ; do
    case "$1" in
      -h|--help)
        usage
        ;;
      -V|--version)
        echo "$0 -- Version $ScriptVersion"; exit 0
        ;;
      -v|--verbose)
        VERBOSE=true
        shift
        ;;
      --)
        shift ; break
        ;;
      *)
        echo 'This should not have happened.  Probably getopt is misconfigured.'
        exit 2
        ;;
    esac
  done
  remaining_args=( "$@" )
} # ----------  end of function option_handling  ----------

_verbose () { # printf '%s\n' if VERBOSE, be silent otherwise
  if [[ ${VERBOSE} ]]; then
    _verbose() {
      printf '%s\n' "$@"
    }
    _verbose "$@"
  else
    _verbose() {
      :
    }
  fi
} # ----------  end of function _verbose  ----------

cleanup () { # Will be called by the trap above, no need to call it manually.
  :
} # ----------  end of function cleanup  ----------

# here you could source your scripting-libraries
# and make use of flatten.sh later.
# see https://github.com/markgraf/flatten.sh.git

#=== Main ======================================================================
main () {
  option_handling "$@"

  # Your script goes here...

} # ----------  end of function main  ----------

main "$@"

#=== End =======================================================================


相关内容