亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Shell腳本傳遞參數(shù)的3種方法比較

 更新時(shí)間:2015年05月08日 09:43:37   投稿:junjie  
這篇文章主要介紹了Shell腳本傳遞參數(shù)的3種方法比較,本文直接給出代碼示例,在代碼中包含詳細(xì)注解,需要的朋友可以參考下
#!/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 "\n### nomal_opts_act ###\n"

  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命令自建的選項(xiàng)解析,可以按照自己的想法實(shí)現(xiàn)
#優(yōu)點(diǎn):自己定制,沒有做不到,只有想不到
#缺點(diǎn):麻煩

getopt_act()
{
  echo -e "\n### getopt_act ###\n"

  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命令解析選項(xiàng)和參數(shù)
#優(yōu)點(diǎn):相對(duì)與getopts來(lái)說(shuō)是個(gè)半自動(dòng)解析,自動(dòng)組織選項(xiàng)和參數(shù),用 -- 符號(hào)將選項(xiàng)與參數(shù)隔開
#缺點(diǎn):相對(duì)于getopts的缺點(diǎn)
#1.需要與set -- 命令配合,不是必須,需要手動(dòng)shift
#2.選項(xiàng)參數(shù)中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就會(huì)解析錯(cuò)誤

getopts_act()
{
  echo -e "\n### getopts_act ###\n"
  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 命令解析選項(xiàng)和參數(shù)
#優(yōu)點(diǎn):可在參數(shù)中包含空格如:-c "earth moon"
#      選項(xiàng)字母和參數(shù)值之間可以沒有空格如:-bdog
#      可將未定義的選項(xiàng)綁定到?輸出
#      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

相關(guān)文章

  • linux shell的輸出效果修改方法(界面顏色)

    linux shell的輸出效果修改方法(界面顏色)

    文本終端的顏色可以使用“ANSI非常規(guī)字符序列”來(lái)生成,有時(shí)候想個(gè)性化輸出,就可以參考下面的方法
    2013-01-01
  • 2022最新vmstate?命令詳解

    2022最新vmstate?命令詳解

    這篇文章主要介紹了vmstate?命令詳解2022,主要包括使用vmstat命令的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • linux目錄管理方法介紹

    linux目錄管理方法介紹

    這篇文章介紹了linux目錄管理的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 一天一個(gè)shell命令 linux文本內(nèi)容操作系列-sed命令詳解

    一天一個(gè)shell命令 linux文本內(nèi)容操作系列-sed命令詳解

    這篇文章主要介紹了一天一個(gè)shell命令 linux文本內(nèi)容操作系列-sed命令詳解,需要的朋友可以參考下
    2016-06-06
  • Linux下Shell編程快捷鍵大全(日常整理)

    Linux下Shell編程快捷鍵大全(日常整理)

    有時(shí)候,我們需要在 Bash 中重復(fù)執(zhí)行先前的命令。所以學(xué)習(xí)shell編程快捷鍵對(duì)工作非常有幫助,下面由腳本之家小編給大家介紹Linux下Shell編程快捷鍵大全,需要的朋友參考下吧
    2016-03-03
  • Linux定時(shí)執(zhí)行任務(wù)at和crontab命令詳解

    Linux定時(shí)執(zhí)行任務(wù)at和crontab命令詳解

    本篇文章主要介紹了Linux定時(shí)執(zhí)行任務(wù)at和crontab命令這兩個(gè)命令的基本用法和區(qū)別,一起學(xué)習(xí)下。
    2017-11-11
  • CentOS 6.0 啟動(dòng)時(shí)出現(xiàn)fstab錯(cuò)誤時(shí)的修復(fù)方法

    CentOS 6.0 啟動(dòng)時(shí)出現(xiàn)fstab錯(cuò)誤時(shí)的修復(fù)方法

    下面小編就為大家?guī)?lái)一篇CentOS 6.0 啟動(dòng)時(shí)出現(xiàn)fstab錯(cuò)誤時(shí)的修復(fù)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-03-03
  • Linux xargs命令詳細(xì)介紹

    Linux xargs命令詳細(xì)介紹

    這篇文章主要介紹了 Linux xargs命令詳細(xì)介紹的相關(guān)資料,xargs是給其他命令傳遞參數(shù)的一個(gè)過濾器,是構(gòu)建單行命令的重要組件之一,需要的朋友可以參考下
    2017-01-01
  • Shell用sed命令刪除特定行的方法

    Shell用sed命令刪除特定行的方法

    這篇文章主要介紹了Shell用sed命令刪除特定行的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • linux系統(tǒng)安裝字體詳細(xì)介紹

    linux系統(tǒng)安裝字體詳細(xì)介紹

    這篇文章主要介紹了linux系統(tǒng)安裝字體詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評(píng)論