乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      R語(yǔ)言,Perl語(yǔ)言, Shell傳遞參數(shù)的方法

       育種數(shù)據(jù)分析 2021-11-18

      1, R語(yǔ)言

      思路:
      1, 加載optparse包
      2, 設(shè)置參數(shù), 為list
      3, 解析參數(shù)
      4, 如果參數(shù)為空, 打印幫助文檔

      #!/usr/bin/Rscript library(optparse) option_list <- list(  make_option(c("-a", "--aa"), type = "integer", default=FALSE,              help="Input a number"),  make_option(c("-b", "--bb"), type="integer", default=FALSE,              help="Input a number") ) opt_parser = OptionParser(option_list=option_list); opt = parse_args(opt_parser); a = opt$a b = opt$b if (opt$a==0&&opt$b==0){  print_help(opt_parser)  stop("請(qǐng)輸入?yún)?shù)", call.=TRUE)  } c = a+b cat(" a is:",a,"\n","b is:",b,"\n","a plus b is:",c,"\n")

      打印幫助文檔:
      幫助文檔打印, 參數(shù)為空, 或者參數(shù)為-h或者—help時(shí), 打印出幫助文檔.

      [dengfei@bogon qunti]$ Rscript plus.R Usage: plus.R [options] Options:    -a AA, --aa=AA        Input a number    -b BB, --bb=BB        Input a number    -h, --help        Show this help message and exit Error: 請(qǐng)輸入?yún)?shù) Execution halted

      正確輸出結(jié)果:

      [dengfei@bogon qunti]$ Rscript plus.R -a 1 -b 2 a is: 1 b is: 2 a plus b is: 3

      2, Perl語(yǔ)言

      參考: 在Perl、Shell和Python中傳參與輸出幫助文檔https://zhuanlan.zhihu.com/p/53067406)

      一個(gè)簡(jiǎn)單的示例, 一個(gè)perl程序, 計(jì)算a+b的值

      有兩個(gè)參數(shù)
      -a
      -b

      代碼如下:

      #!/usr/bin/perl use strict; use Getopt::Long; =head1 Description    This script is calculate the sum of a and b =head1 Usage    $0 -a <input number> -b <input number> =head1 Parameters    -a  [int]   Input raw number    -b  [int]   Input raw number =cut my($a,$b); GetOptions(    "a:s"=>\$a,    "b:s"=>\$b    ); die `pod2text $0` if ((!$a) or (!$b)); print("The result of a plus b is:",$a+$b,"\n");

      邏輯如下:
      1, 使用perl 包 Getopt::Long進(jìn)行參數(shù)書(shū)寫(xiě)
      定義b, 然后使用GetOptions函數(shù)
      使用die函數(shù), 如果沒(méi)有相關(guān)參數(shù), 就die

      my($a,$b); GetOptions(    "a:s"=>\$a,    "b:s"=>\$b    ); die `pod2text $0` if ((!$a) or (!$b));

      2, 使用POD文檔, 編寫(xiě)指南
      包括:
      描述
      使用方法
      參數(shù)介紹

      =head1 Description    This script is calculate the sum of a and b =head1 Usage    $0 -a <input number> -b <input number> =head1 Parameters    -a  [int]   Input raw number    -b  [int]   Input raw number =cut

      打印幫助文檔:
      如果沒(méi)有添加參數(shù):

      [dengfei@bogon qunti]$ perl 1.pl Description        This script is calculate the sum of a and b Usage        $0 -a <input number> -b <input number> Parameters        -a  [int]   Input raw number        -b  [int]   Input raw number

      正確輸出結(jié)果:

      [dengfei@bogon qunti]$ perl 1.pl -a 1 -b 2 The result of a plus b is:3

      3, Shell腳本傳遞參數(shù)

      思路:
      1, 定義一個(gè)名為helpdoc的函數(shù), 如果沒(méi)有參數(shù), 就返回這個(gè)函數(shù), 打印出幫助信息
      2, 定義參數(shù), 使用getopts, 參數(shù)放在頓號(hào)里面, 可以將參數(shù)賦值給a和b, 下面直接調(diào)用b即可
      3, 執(zhí)行加號(hào), 打印結(jié)果.

      #!/bin/bash # 定義幫助文檔 helpdoc(){    cat <<EOF Description:    This is the description. Usage:    $0 -a <input number> -b <input number> Option:    -a    a number    -b    b number EOF } # 如果沒(méi)有參數(shù), 輸出幫助文檔 if [ $# = 0 ] then    helpdoc    exit 1 fi while getopts ":a:b:" opt do    case $opt in        a)            a=$OPTARG            ;;        b)            b=$OPTARG            ;;        ?)            echo "Unknown option: $opt"            helpdoc            exit 1            ;;    esac done sum=$(expr $b + $a)     echo "a is $a" echo "b is $b" echo "a plus b is:$sum"

      打印幫助文檔:

      [dengfei@bogon qunti]$ bash plus.sh Description:    This is the description. Usage:    plus.sh -a <input number> -b <input number> Option:    -a    a number    -b    b number

      正確輸出結(jié)果:
      [dengfei@bogon qunti]$ bash plus.sh -a 1 -b 2
      a is 1
      b is 2
      a plus b is:3

        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多