首页 > 系统服务 > 详细

指定参数的shell脚本

时间:2020-04-23 20:02:19      阅读:67      评论:0      收藏:0      [点我收藏+]

通过执行脚本的时候使用 --<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;^--[^=]*=;;`
  # whats 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

 

指定参数的shell脚本

原文:https://www.cnblogs.com/outsrkem/p/12762935.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!