标签: getopts

如何使 bash getopts 不将选项识别为选项参数

在我的 bash 脚本中,我想用来getopts解析命令行选项。

我的第一次尝试是学习如何使用它,如下:

#!/bin/bash

v_option_arg=""
r_option_arg=""
h_option_arg=""

function get_opt_args() {
    while getopts ":v:r:h:" opt
    do
        case $opt in
        "v")
            v_option_arg="$OPTARG"
             ;;
        "h")
            h_option_arg="$OPTARG"
            ;;
        "r")
            r_option_arg="$OPTARG"
            ;;
        "?")
            echo "Unknown option -$OPTARG"
            exit 1
            ;;
        ":")
            echo "No argument value for option -$OPTARG"
            ;;
        *)
            # Should not occur
            echo "Unknown error while processing options"
            ;;
        esac
    done
    shift $((OPTIND-1))
}

get_opt_args $@
if [ ! -z "$v_option_arg" ]; then
    echo "Argnument value for option -v: $v_option_arg" …
Run Code Online (Sandbox Code Playgroud)

bash getopts

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

BASH - OPTARG 中的 getopts 正则表达式

我需要使用 getopts 从参数中获取正则表达式

./function -i "d*"
Run Code Online (Sandbox Code Playgroud)
while getopts 'i:n' opt; do
  # check -i and -n arg
    case "$opt" in
      i)
        i=true
        pattern=$OPTARG;;
    esac
  done
echo $pattern
Run Code Online (Sandbox Code Playgroud)

输出是darProVas dirgraf-copy dirgraf-my:以 . 开头的文件列表d。我需要这个输出:d*.

bash quotes glob getopts

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

Bash:使用 getopts 解析参数后的选项

在请求一些参数(arg)和选项(-a)的脚本中,我想让脚本用户可以将选项放置在命令行中他想要的位置。

这是我的代码:

while getopts "a" opt; do
  echo "$opt"
done
shift $((OPTIND-1))

echo "all_end : $*"
Run Code Online (Sandbox Code Playgroud)

通过这个命令,我得到了预期的行为:

./test.sh -a arg
a
all_end : arg
Run Code Online (Sandbox Code Playgroud)

我想用这个命令得到相同的结果:

./test.sh arg -a
all_end : arg -a
Run Code Online (Sandbox Code Playgroud)

bash getopts

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

bash getopts 无法识别第二个参数

这是我最简单形式的整个脚本。

#!/bin/bash
src=""
targ=${PWD}

while getopts "s:t:" opt; do
  case $opt in
    s)
      src=$OPTARG
      ;;
    t)
      targ=$OPTARG
      ;;
  esac
  shift $((OPTIND-1))
done

echo "Source: $src"
echo "Target: $targ"
Run Code Online (Sandbox Code Playgroud)

我运行这个脚本作为getopts_test -s a -t b

然而,它总是pwd在 the 前面打印 theTarget:并且从不打印b

我在这里缺少什么?

bash getopts

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

为什么比赛永远不会使用第二场比赛?

我正在尝试使用来自各种平台的原始数据重新实现前2个最终幻想游戏.我想使用getopts crate获得2个程序参数,并通过使用它们来处理它们,match但它只执行第一个匹配元素.我想我搞砸了类型的东西.

也许有另一种方法可以做到这一点?我迷失了使用正式的Rust文档,互联网上的任何教程都不是非常友好的.

这是代码:

let args: Vec<String> = env::args().map(|x| x.to_string()).collect();

if(args.len() < 3) {
    println!("=====ERROR=====\nInvalid number of parameters\nExpected: <gamename> <gamerom>\nType in: 'help me' to get some help.");
    process::exit(1);
}

let ref game = args[1];
let ref rom = args[2];

match game {
    help => {
        println!("=====HELP======");

        match rom {
            list => println!("Available games: ff1, ff2\nAvailable roms: ff1_j_msx, ff1_j_nes, ff1_u, ff1and2, ff2_j, ff2_u_proto"),
            me => println!("Available help commands:\nlist -> List of available games and roms.\nme -> This help"),
            _ …
Run Code Online (Sandbox Code Playgroud)

match getopts rust

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

用于测试仅存在标志的 Bash 脚本

我有一个 bash 脚本,需要接收带有标志的用户名,然后我希望能够查找 -r 或 -w 来指示这是读取还是写入。

目前我正在使用 get opts,但这需要将实际参数传递给 -r 和 -w。

如何测试是否只有 -r 或 -w 存在而不向这些标志传递某些内容。

目前我的脚本如下所示:

#!/bin/bash
while getopts :u:r:w: opt; do
   case $opt in
     u ) user="$OPTARG"                       ;;
     r ) my_read=1                            ;;
     w ) my_write=1                           ;;
    \? ) echo "${0##*/}" [ -erw ]; exit 1     ;;
  esac
done

if [[ ${my_write} -eq 1 ]] ; then
    echo "write"
fi

if [[ ${my_read} -eq 1 ]] ; then
    echo "read"
fi
Run Code Online (Sandbox Code Playgroud)

bash getopts

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

bash - 如果需要操作数,getopts 仅解析第一个参数

一旦 bash 程序在处理 中的选项时执行getops,循环就会退出。

作为一个简短的例子,我有以下 bash 脚本:

#!/usr/bin/env bash

while getopts ":a:l:" opt; do
  case ${opt} in
    a)
      ls -a $2
      ;;
    l)
      ls -l $2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument" >&2
      exit 1
      ;;
  esac
done

echo -e "\nTerminated"
Run Code Online (Sandbox Code Playgroud)

如果脚本被调用test.sh,当我用这个命令执行脚本时,我得到以下输出,其中只-a处理标志,并被-l忽略:

$ ./test.sh -al .
.  ..  file1.txt  file2.txt  test.sh

Terminated
Run Code Online (Sandbox Code Playgroud)

但是,如果我删除每个参数后的冒号,表示每个参数不需要操作数,那么脚本会按预期执行。如果while循环改为:

while getopts ":al" opt; do
Run Code Online (Sandbox Code Playgroud)

然后,运行我的脚本会给出以下输出(同时处理 …

bash getopts

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

源 Bash 函数中的 Getopts 以交互方式工作,但不在测试脚本中?

我有一个 Bash 函数,其中一个函数在测试时被证明存在问题。prunner是一个函数,旨在提供 GNU Parallel 的一些功能,并避免尝试在 Perl 中使用其他 Bash 函数的范围问题。它支持设置命令以针对参数列表运行-c,并设置与 并发运行的后台作业数-t

在测试它时,我最终得到了以下场景:

  • prunner -c "gzip -fk" *.out- 以test.bash交互方式按预期工作。
  • find . -maxdepth 1 -name "*.out" | prunner -c echo -t 6- 不起作用,看似无视-c echo

测试在使用 Bash 4.3 的 Ubuntu 16.04 和使用 Bash 4.4 的 Mac OS X 上进行。

这似乎与后者中发生的事情test.bash是,getopts拒绝处理-c,从而prunner将尝试直接执行论据没有它被赋予的前缀命令。奇怪的是,我能够观察到它接受该-t选项,因此getopts至少部分工作。Bash 调试set -x无法说明为什么我会发生这种情况。

这是有问题的函数,稍作修改以echo代替 …

bash shell getopts

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

使用 bash 使用 getopts 调用不同的函数

我想弄清楚如何在一个脚本中拥有多个函数并选择带有参数的函数。问题似乎是,如果我选择一个函数,optarg 似乎不会与脚本一起运行。在这个例子中,我会这样运行脚本 ~# ./script.sh -a -c wordlist.txt 只运行第一个函数,选择的 wordlist 与 ~# ./script.sh -b -c wordlist 相同。文本

#!/bin/bash

one()
{
for i in $(cat $wordlist); do
  wget http://10.10.10.10/$i
}

two()
{
for i in (cat $wordlist); do
  curl http://10.10.10.10/$i
}

while getopts "abc:" option; do
 case "${option}" in
    c) wordlist=${OPTARG} ;;
    a) one;;
    b) two;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

bash sh getopts

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

标签 统计

getopts ×9

bash ×8

glob ×1

match ×1

quotes ×1

rust ×1

sh ×1

shell ×1