标签: getopts

Bash getopts放弃了最后一个参数

所以,我正在尝试使用bash的内置getopts来处理参数处理,除非我得到一个奇怪的结果.这是我的测试脚本;

#!/bin/sh

HOST=
OWNER=
GROUP=

while getopts "h:o:g" OPTION;
  do
    case $OPTION in
    h)
      HOST=$OPTARG
      ;;
    o)
      OWNER=$OPTARG
      ;;
    g)
      GROUP=$OPTARG
      ;;
  esac
done

echo "$HOST - $OWNER:$GROUP"
Run Code Online (Sandbox Code Playgroud)

然而,当我使用这个运行脚本时;

./test.sh -h test.host.com -o skittles -g whatever
Run Code Online (Sandbox Code Playgroud)

我的最后一个arg从未被拉入或被丢弃.我的回声结果是;

test.host.com - skittles:
                         ^ where's my group value? O.o
Run Code Online (Sandbox Code Playgroud)

有谁知道会造成什么?

谢谢.

unix bash shell scripting getopts

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

getopts 没有提供参数

如何检查是否没有提供必需的参数?我发现 switch case 中的 ":" 选项应该足以达到这个目的,但它永远不会进入那种情况(代码块)。我是否将“冒号”放在开头或其他地方都没有关系。

我的代码:

while getopts :a:b: OPTION;
do
     case "$OPTION" in
         a)
             var1=$OPTARG
             ;;
         b)
             var2=$OPTARG
             ;;
         ?)
             exitScript "`echo "Invalid option $OPTARG"`" "5"
             ;;
         :)
             exitScript "`echo "Option -$OPTARG requires an argument."`" "5"
             ;;
         *)
             exitScript "`echo "Option $OPTARG unrecognized."`" "5"
             ;;
     esac
done
Run Code Online (Sandbox Code Playgroud)

提前感谢。

bash getopts

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

BASH getopts可选参数

我正在尝试创建一个shell脚本,它有两个必需参数(arg1,arg2),然后是一个可选的-b标志加上用户选择使用该标志时需要遵循的参数.

让我解释:

这是一个安装脚本,它利用GIT从Github存储库中获取应用程序.用户键入终端f.ex.:

./shellscript.sh new <app_name> # fetches master branch by default
Run Code Online (Sandbox Code Playgroud)

并且脚本从此存储库中获取主分支的实例.但是,用户是否应该选择使用可选的-b标志,这意味着他/她想要指定要获取的分支,例如开发分支.意味着用户可以这样做:

./shellscript.sh new <app_name> -b develop # or which ever branch there is available
Run Code Online (Sandbox Code Playgroud)

我也很好奇如何使脚本工作,以便用户在'new'参数和'app_name'参数之前键入-b标志+ branch_name并不重要.但这可能不是目前最重要的事情.

要知道我正在尝试构建什么,这里是一个指向我当前脚本的链接,它只接受两个必需参数并且只获取主分支:My Super Cool Artisan Executable Script

PS:我一直在尝试使用getopts的很多例子,我在Stackoverflow和其他博客上都找到了这些例子,但是没有一个帮助我完全使它工作.因此,我在这里向你们所有人寻求帮助.

非常感谢,请务必查看我的Linux Mint/Ubuntu - 仅适用于酷人的安装后脚本(您和那些从Windows/Mac切换到Linux的人)

此致,Villi

unix linux bash shell getopts

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

如何使用 getopts 在命令行中传递 shell 脚本的强制和可选标志?

我想通过 getopts 将 3 个参数传递给我的 shell 脚本。脚本至少需要前2个,第三个参数是可选的。如果未设置,则使用其默认值。这样以下都可以工作:

sh script.sh -a "/home/dir" -b 3
sh script.sh -a "/home/dir" -b 3 -c "String"
Run Code Online (Sandbox Code Playgroud)

我试着像下面那样做,但它总是忽略我输入的参数。

usage() {
 echo "Usage: Script -a <homedir> -b <threads> -c <string>"
  echo "options:"
                echo "-h      show brief help"

  1>&2; exit 1;
}

string="bla"

while getopts h?d:t:a: args; do
case $args in
    -h|\?)
        usage;
        exit;;
    -a ) homedir=d;;
    -b ) threads=${OPTARG};;
    -c ) string=${OPTARG}
        ((string=="bla" || string=="blubb")) || usage;;
    : )
        echo "Missing option argument for -$OPTARG" >&2; exit …
Run Code Online (Sandbox Code Playgroud)

shell getopts command-line-arguments

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

在Bash中,为什么在函数中使用getopts只能工作一次?

我正在尝试在macOS Sierra上创建一个简单的函数来计算字符串中的字符.这工作正常(添加到我的bashrc文件):

function cchar() {
    str=$1
    len=${#str}
    echo "string is $len char long"
}

$ cchar "foo"
string is 3 char long
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用一个-a选项扩展它,所以我将其添加到我的函数中(并将其余部分注释用于测试):

while getopts "a:" opt; do
    case $opt in
        a)
            echo "-a param: $OPTARG" >&2
            ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

在写这篇文章的一些测试之后,我注意到每次运行时cchar -a "test",我都必须运行它而没有选项(cchar)否则下次我使用该-a选项运行它时,它无法识别该选项.

$ cchar

$ cchar -a "foo"
-a param: foo

$ cchar -a "foo"

$ cchar

$ cchar -a "foo"
-a param: foo
Run Code Online (Sandbox Code Playgroud)

bash getopts

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

getopts值类和Template.Substitute不会(立即)一起工作

我有python代码类似于:

from string import Template
import optparse

def main():
  usage = "usage: %prog options outputname"
  p = optparse.OptionParser(usage)
  p.add_option('--optiona', '-a', default="")
  p.add_option('--optionb', '-b', default="")
  options, arguments = p.parse_args()
  t = Template('Option a is ${optiona} option b is ${optionb}')
  print t.substitute(options)
Run Code Online (Sandbox Code Playgroud)

但这给了我

AttributeError: Values instance has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)

因为options是值而不是字典.

我如何巧妙地完成这项工作?

(欢迎任何其他建议,我的pythonic意识仍在培养...)

python templates getopts

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

如何禁止Getopt :: Std :: getopts发出的警告消息?

我有短脚本test.pl

#!/usr/bin/perl
use locale;
use encoding 'utf-8';

use Getopt::Std;

getopts("dei") or print STDERR "TRALALALALA\n"; 
print"@ARGV\n";
Run Code Online (Sandbox Code Playgroud)

我需要抑制Unknown option:getopts生成的消息,并且只在stderr上获取我的TRALALALALA.

perl silent getopts

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

使用长选项正确使用 bash getopts

我写了下面的代码来使用长选项和getopts,但它不起作用(参数对变量的值没有影响)。什么是正确的语法?

while getopts "c:(mode)d:(file1)e:(file2)" opt; do
  case $opt in
  -c|--mode)
      mode=$OPTARG
      ;;  
  -d|--file1)
      file1=$OPTARG
      ;;  
  -e|--file2)
      file2=$OPTARG
      ;;  
  esac
done
Run Code Online (Sandbox Code Playgroud)

bash getopts

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

将多个参数传递给 Bash 脚本

我有一个 bash 脚本,我想这样调用它:

bash curl.sh http://www.google.co.uk/ -d Directory -a "Moz 123" -r http://localhost/
Run Code Online (Sandbox Code Playgroud)

我可以收集第一个参数(http://www.google.co.uk/),内容如下:

url=$1
while getopts p:d:a:r: opt; do
case $opt in
    p) proxy=$OPTARG ;;
    d) dir=$OPTARG ;;
    a) ua=$OPTARG ;;
    r) ref=$OPTARG ;;
esac
done
Run Code Online (Sandbox Code Playgroud)

但是,它没有接受其他参数。如果我删除“ http://www.google.co.uk/ ”作为第一个参数,它会选择 -arguments。

由于后勤原因,我无法设置第一个参数,例如“ http://www.google.co.uk/ ”和 -u 等。

你如何让它发挥作用?

bash getopts

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

带有getopt()的if语句中的C中的true和false

当我使用argv和getopt时,我很难理解"if语句"是如何真实和错误的.

这是简单的代码:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
        int opt;

while ((opt = getopt (argc, argv, "i:l:")) != -1)
        switch (opt) {
        case 'i':
                printf("This is option i");           
                break;
        case 'l':
                printf("This is option l");
                break;
        default:
                fprintf(stderr,"Usage: %s here goes usage\n",argv[0]); 
    }

if (argc == 1) {
    printf("Without options");
}

if ((argc == 2) && (argv[1] != "-l") || (argv[1] != "-i")) {
    printf("Without option -l or -i but with other argument \n"); …
Run Code Online (Sandbox Code Playgroud)

c unix if-statement getopts argv

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