指定參數的shell腳本


通過執行腳本的時候使用 --<parameter> 的形式,把變量傳進腳本,與參數位置無關。

#!/bin/bash
# Tue Dec 17 CST 2019


# help options
usage () {
    cat <<EOF
Usage: $0 [OPTIONS]
  --version=19.0.0    Specify the version
  --file=file         The specified file
  --home=homedir      The specified directory
EOF
  exit 1
}

parse_arguments() {
  # Read the parameter
  for arg do 
  # the parameter after "=", or the whole $arg if no match
  val=`echo "$arg" | sed -e 's;^--[^=]*=;;'`
  # what's before "=", or the whole $arg if no match
  optname=`echo "$arg" | sed -e 's/^\(--[^=]*\)=.*$/\1/'`
  # replace "_" by "-" 
  optname_subst=`echo "$optname" | sed 's/_/-/g'`
  arg=`echo $arg | sed "s/^$optname/$optname_subst/"`   
    case "$arg" in       
      --version=*) version="$val" ;;
      --file=*) file="$val" ;;
      --home=*) home="$val" ;;
      --help) usage ;;
      *) shell_quote_string "$arg" ;;
    esac
  done
}
shell_quote_string() {
  # This sed command makes sure that any special chars are quoted,
  # so the arg gets passed exactly to the server.
  echo "$1" | sed -e 's,\([^a-zA-Z0-9/_.=-]\),\\\1,g'
}

parse_arguments "$@"

if test -n "$version"
then
  echo "version: $version"
fi

if test -n "$file"
then
  echo "file: $file"
fi

if test -n "$home"
then
  echo "home: $home"
fi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM