标签: getopt

Shell 脚本 getopts optarg 无值

有人可以检查此代码片段并告诉我为什么当我使用 -p abcdef 调用此脚本时 $OPTARG 从未具有传入的参数值?

# Process command-line options passed as switches to this script
while getopts "ph:" option; do
  case "$option" in
    p)
       {
         if [ -n "$OPTARG" ]; then
           echo
           echo "##### SCRIPT ERROR: You failed to provide a host prefix. #####"
           echo
           usage
           break
         else
           echo "Setting host prefix to '$OPTARG'"
           echo
           HOST_PREFIX=$OPTARG
         fi
       } ;;
    h) usage ;;
    '?') usage ;;
    *) break ;;
  esac
done
shift "$((OPTIND-1))" # Shift off the options and optional …
Run Code Online (Sandbox Code Playgroud)

shell getopt getopts

2
推荐指数
1
解决办法
4372
查看次数

在 bash 中的自定义 getopts 脚本中将参数作为选项传递

我想将选项作为参数传递。例如:

mycommand -a 1 -t '-q -w 111'
Run Code Online (Sandbox Code Playgroud)

该脚本无法识别引号中的字符串。即它只获取字符串的一部分。

getopts工作原理是一样的 - 它只看到-q.

对于自定义 getopts,我使用类似的脚本(示例):

while :
do
    case $1 in
        -h | --help | -\?)
            # Show some help
            ;;
        -p | --project)
            PROJECT="$2"
            shift 2
            ;;
        -*)
            printf >&2 'WARN: Unknown option (ignored): %s\n' "$1"
            shift
            ;;
        *)  # no more options. Stop while loop
            break
            ;;
        --) # End of all options
        echo "End of all options"
            shift
            break
            ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

bash getopt getopts

2
推荐指数
1
解决办法
2309
查看次数

在 Unix 中将 json 作为命令行参数传递

我正在尝试将 json 字符串作为命令行参数传递给 Unix 环境中的 C++ 应用程序。

.\SampleApp -j {\"speed\":\"15\",\"rpm\":\"100\",\"loc\":[\"-83.11\",\"42.11\"]}
Run Code Online (Sandbox Code Playgroud)

我在示例应用程序中使用 getopt() 函数来解析参数。在输出处我只收到速度:15。但是当我运行应用程序时

.\SampleApp -j \"speed\":\"15\",\"rpm\":\"100\",\"loc\":[\"-83.11\",\"42.11\"]
Run Code Online (Sandbox Code Playgroud)

有用。我的问题是如何将带有大括号的 json 字符串正确传递给应用程序。我尝试使用转义序列,\{但它不起作用。

c c++ unix json getopt

2
推荐指数
1
解决办法
2960
查看次数

getopt 在具有短选项的 MacOS 下无法按预期工作

我有以下命令:command.sh bar -b=FOO

我试图用以下内容来解析它:getopt "m::b::" "$@"

在Linux下,结果是:-b =FOO -- command.sh bar

在 MacOS 下,结果是:-- command.sh bar -b=FOO。所以根本没有解析它。我尝试过-bFOO-b FOO但结果相同,它没有被解析。

如何解决这个问题?我需要一个可以在 Mac 和 Linux 上运行的跨平台 bash 解决方案。

macos bash getopt

2
推荐指数
1
解决办法
830
查看次数

运行 perl 脚本并收到“选项规范错误:”

我对运行 perl 脚本非常陌生,并且遇到了我怀疑是由于变量分配引起的问题。

这是原始脚本的开头:

#!/usr/bin/perl -w

#####################################
# gbstrim.pl 
# John Garbe
# June 2016
# 
#
#####################################

=head1 NAME

gbstrim.pl - 
 - Trim padding and/or cut site residue from beginning of reads
 - Trim sequencing adapter from ends of reads
 - Trim all reads to length

NOTE: this doesn't handle paired-end data

=head1 SYNOPSIS

gbstrim.pl --enzyme1 bamhi --enzyme2 psti --read R1 [--threads 1] [--minlength 20] --fastqfile file.fastq --outputfile out.fastq 

=head1 DESCRIPTION

Options:
    --fastqfile sample.fastq : fastq file of reads …
Run Code Online (Sandbox Code Playgroud)

perl bioinformatics getopt

2
推荐指数
1
解决办法
84
查看次数

那么GNU getopt只支持中间的选项?

我正在尝试getoptAPI:

http://www.gnu.org/s/hello/manual/libc/Example-of-Getopt.html#Example-of-Getopt

但我觉得它只支持中间的选项?

当我发现它的判断argv[optind],以argv[argc-1]非选择的参数.

是这样的吗?

c gnu getopt

1
推荐指数
1
解决办法
171
查看次数

在C中使用getopt的问题

我想使用getopt()来解析命令行提供的参数,但是我遇到了非常简单的测试用例的问题.我有以下代码(几乎,但不完全相同,作为POSIX标准定义中的示例提供).

int main(int argc, char *argv[]) {
    int c;
    int rmsflg = 0, saflg = 0, errflg = 0;
    char *ifile;
    char *ofile;

    //Parse command line arguments using getopt
    while (((c=getopt(argc,argv, ":i:rso:")) != 1) && (errflg == 0))  {
        switch(c){
            case 'i':
                ifile="optarg";
                break;
            case 'o':
                ofile="optarg";
                break;
            case 'r':
                if (saflg)
                    errflg++;
                else {
                    rmsflg++;
                    printf("Root Mean Square averaging selected\n");
                }
                break;
            case 's':
                if (rmsflg)
                    errflg++;
                else {
                    saflg++;
                    printf("Standard Arithmetic averaging selected\n");
                }
                break;
            case …
Run Code Online (Sandbox Code Playgroud)

c getopt

1
推荐指数
1
解决办法
905
查看次数

Perl GetOpt ::长模块错误

我正在使用GetOpt :: long模块从命令行获取参数并将其分配给相应的变量.但是我在打印时遇到错误.代码和错误如下:

#!usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

GetOptions(
    "mount_path=s" => \my $old_path,
    "var=s"        => \my $var,
    "log_path=s"   => \my $log_path,
) or die "Error in input variables\n";

print <<"END_INPUTS";
 These are your inputs: 
 old_path= $old_path 
 var = $var 
 log_path=$log_path 
 Press enter twice if all looks GOOD 
 *********************************************************
END_INPUTS
Run Code Online (Sandbox Code Playgroud)

命令行参数如下:

 perl getvar.pl --mount_path=/var/sslvpn --var=7.0.0.2_va-SSLVPN-!7.0.0.2.16sv+.jpn-05!j+g_554863- --log_path=log.txt  
Run Code Online (Sandbox Code Playgroud)

我在运行时遇到以下错误

-bash: !7: event not found
Run Code Online (Sandbox Code Playgroud)

请有人帮我这个.谢谢.

regex perl getopt getopt-long

1
推荐指数
1
解决办法
97
查看次数

Scala中的简单函数getopt

def main(args: Array[String]) {
  if (args.length == 0) println(usage)
  val argList = args.toList
  type OptionMap = Map[Symbol, Any]

  def nextOption(map: OptionMap, list: List[String]): OptionMap = {
    list match {
      case Nil => map
      case "-h" | "--help" :: tail => usage(); sys.exit(0)
      case "-p" | "--port" :: option :: tail => nextOption(map ++ Map('port -> option.toInt), tail)
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法捕获更多的头部价值List?此代码生成

type mismatch;
   found   : String("-h")
   required: List[String]
        case "-h" | "--help" :: tail => usage(); sys.exit(0)
             ^
Run Code Online (Sandbox Code Playgroud)

可能重复: …

scala getopt

1
推荐指数
1
解决办法
846
查看次数

`-V`标志如何既是`--verbose`又是`--version`?

来自https://github.com/karelzak/util-linux/blob/master/disk-utils/mkfs.c#L94-L113

/* Check commandline options. */
opterr = 0;
while ((more == 0)
       && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL))
       != -1))
    switch (i) {
    case 'V':
        verbose++;
        break;
    case 't':
        fstype = optarg;
        break;
    case 'h':
        usage();
    case VERSION_OPTION:
        print_version();
    default:
        optind--;
        more = 1;
        break;  /* start of specific arguments */
Run Code Online (Sandbox Code Playgroud)

mkfs的文档说这-V是版本和详细的短标志.我无法理解这是如何可能的,我正在寻找清晰度.

VERSION_OPTION被定义为enum { VERSION_OPTION = CHAR_MAX + 1 };所以我不确定是什么字符.

c flags getopt

1
推荐指数
1
解决办法
175
查看次数

标签 统计

getopt ×10

c ×4

bash ×2

getopts ×2

perl ×2

bioinformatics ×1

c++ ×1

flags ×1

getopt-long ×1

gnu ×1

json ×1

macos ×1

regex ×1

scala ×1

shell ×1

unix ×1