Loading

shell 传参模板

myscript.sh

#!/bin/bash

org=""
name=""

# Define the usage function
usage() {
  echo "Usage: $0 [-o|--org <org>] [-n|--name <name>] [-h|--help]"
  exit 1
}

# Define the help function
help() {
  echo "This script performs a task using org and name options."
  echo "Options:"
  echo "  -o, --org   Specify the org value"
  echo "  -n, --name  Specify the name value"
  echo "  -h, --help  Show this help message"
  exit 0
}

# Use getopt to parse command line options
opts=$(getopt -o o:n:h --long org:,name:,help -n "$0" -- "$@")
if [ $? != 0 ]; then
  usage
fi
echo "####################"
echo $opts
eval set -- "$opts"

while true; do
  case "$1" in
    -o | --org)
      org="$2"
      shift 2
      ;;
    -n | --name)
      name="$2"
      shift 2
      ;;
    -h | --help)
      help
      ;;
    --)
      shift
      break
      ;;
    *)
      usage
      ;;
  esac
done

# Additional code here based on the parsed options
echo "org: $org"
echo "name: $name"
posted @ 2023-10-25 17:29  小维江湖  阅读(9)  评论(0编辑  收藏  举报