Shell 스 크 립 트 전달 매개 변수 3 가지 방법 비교


#!/bin/bash
#extracting command text_text_text_line options as parameters

help_info(){
  echo "NAME"
  echo "\t$0"
  echo "SYNOPSIS"
  echo "\t$0 is a shell test about process options"
  echo "DESCRIPTION"
  echo "\toption like -a -b param1 -c param2 -d"
}

if [ $# -lt 0 ]
then
  help_info
fi

nomal_opts_act()
{
  echo -e "
### nomal_opts_act ###
" while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option" ;; -b) echo "Found the -b option" echo "The parameter follow -b is $2" shift ;; -c) echo "Found the -c option" echo "The parameter follow -c is $2" shift ;; -d) echo "Found the -d option" ;; *) echo "$1 is not an option" ;; esac shift done } # shell , # : , , # : getopt_act() { echo -e "
### getopt_act ###
" GETOPTOUT=`getopt ab:c:d "$@"` set -- $GETOPTOUT while [ -n "$1" ] do case $1 in -a) echo "Found the -a option" ;; -b) echo "Found the -b option" echo "The parameter follow -b is "$2"" shift ;; -c) echo "Found the -c option" echo "The parameter follow -c is "$2"" shift ;; -d) echo "Found the -d option" ;; --) shift break ;; *) echo "Unknow option: "$1"" ;; esac shift done param_index=1 for param in "$@" do echo "Parameter $param_index:$param" param_index=$[ $param_index + 1 ] done } # getopt # : getopts , , -- # : getopts #1. set -- , , shift #2. -a -b dog -c "earth moon" -d -f param1 param2 getopts_act() { echo -e "
### getopts_act ###
" while getopts :ab:c:d ARGS do case $ARGS in a) echo "Found the -a option" ;; b) echo "Found the -b option" echo "The parameter follow -b is $OPTARG" ;; c) echo "Found the -c option" echo "The parameter follow -c is $OPTARG" ;; d) echo "Found the -d option" ;; *) echo "Unknow option: $ARGS" ;; esac done shift $[ $OPTIND -1 ] param_index=1 for param in "$@" do echo "Parameter $param_index:$param" param_index=$[ $param_index + 1 ] done } #getopts # : :-c "earth moon" # :-bdog # ? # Unknow option: ? nomal_opts_act -a -b dog -c earth -d -f param1 param2 getopts_act -a -b dog -c "earth moon" -d -f param1 param2 getopt_act -a -b dog -c earth -d -f param1 param2

좋은 웹페이지 즐겨찾기