zoukankan      html  css  js  c++  java
  • 指定参数的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


    作者:Outsrkem
    出处:https://www.cnblogs.com/outsrkem/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    经典入门_排序
    经典入门_贪心算法
    经典入门_Hash的应用
    经典入门_排序
    uva 839
    uva 327
    uva 699
    uva 712
    uva 297
    uva 548
  • 原文地址:https://www.cnblogs.com/outsrkem/p/12762935.html
Copyright © 2011-2022 走看看