标签: getopts

如何使用getopt(s)作为在bash中传递参数的技术

有人能告诉我一个如何正确使用getopts的例子,或者我能够在参数中传递的任何其他技术吗?我试图在unix shell/bash中写这个.我看到有getopt和getopts,不知道哪个更好用.最后,我将构建它以添加更多选项.

在这种情况下,我想将文件路径作为输入传递给shell脚本,并在未正确输入的情况下放置描述.

export TARGET_DIR="$filepath"
Run Code Online (Sandbox Code Playgroud)

例如:(调用命令行)

./mytest.sh -d /home/dev/inputfiles
Run Code Online (Sandbox Code Playgroud)

如果以这种方式运行错误消息或提示正确使用:

./mytest.sh -d /home/dev/inputfiles/
Run Code Online (Sandbox Code Playgroud)

unix bash shell getopt getopts

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

你如何使用getopts?

bash脚本中使用getopts的最简单,最直接的方法是什么.

如果我有一个名为脚本:MyScript可CAN取的参数:-p -r -s -x

if argument x then exit
if argument p then echo "port 10"
if argument s then add 2+2
if argument r then echo env 
Run Code Online (Sandbox Code Playgroud)

这是一个假设的脚本,但我想看一个如何做到这一点的例子.

bash getopts

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

使用getopt_long(C++)如何为两个需要参数的长短选项编码?

#include <iostream>
#include <getopt.h>

#define no_argument 0
#define required_argument 1 
#define optional_argument 2


int main(int argc, char * argv[])
{
  std::cout << "Hello" << std::endl;

  const struct option longopts[] =
  {
    {"version",   no_argument,        0, 'v'},
    {"help",      no_argument,        0, 'h'},
    {"stuff",     required_argument,  0, 's'},
    {0,0,0,0},
  };

  int index;
  int iarg=0;

  //turn off getopt error message
  opterr=1; 

  while(iarg != -1)
  {
    iarg = getopt_long(argc, argv, "svh", longopts, &index);

    switch (iarg)
    {
      case 'h':
        std::cout << "You hit help" << std::endl;
        break;

      case …
Run Code Online (Sandbox Code Playgroud)

c++ getopts getopt-long

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

为什么我的shell脚本在第二次尝试失败?

此脚本应接受一组搜索字词,并返回格式化的网址以搜索Google.

$ ./google_search.sh albert einstein
https://www.google.com/search?q=albert+einstein
Run Code Online (Sandbox Code Playgroud)

它做得很好,所以我决定添加一个选项来搜索特定网站,或者使用-s-S标记忽略该网站.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein+site%3Awikipedia.org
Run Code Online (Sandbox Code Playgroud)

这在您第一次运行脚本时有效,但在每次后续尝试时都会失败.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
Run Code Online (Sandbox Code Playgroud)

打开新的终端窗口或重新启动终端都可以清除此问题,并在失败前再尝试一次.

剧本:

#!/bin/bash

# original source of concatenate_args function by Tyilo:
# http://stackoverflow.com/questions/9354847/concatenate-inputs-in-bash-script
function concatenate_args
{
    string=""
    ignorenext=0
    for a in "$@" # Loop over arguments
    do
        if [[ "${a:0:1}" != "-" && $ignorenext = 0 ]] # Ignore flags (first character is -)
        then
            if [[ …
Run Code Online (Sandbox Code Playgroud)

bash darwin getopts

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

必需的选项getopts linux

我必须编写一个bash脚本:

    schedsim.sh [-h] [-c #CPUs ] -i pathfile
Run Code Online (Sandbox Code Playgroud)

h和c是可选选项.我是必需的,当运行脚本时,如果它没有i选项 - >错误消息.

如何在getopts中创建一个必需的选项?谢谢!

另一个问题:如何为选项的参数设置默认值?比方说,如果没有提供c参数 - > c的参数默认值为1.

linux bash getopts

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

使用不带参数的 getopts 来获取帮助输出

您好,我正在创建一个使用 getopts 的 bash 脚本。现在我想创建一个“-h”参数来获取帮助。但每次我都必须给参数一个参数。

Now

test.sh -h test

What I want

test.sh -h
help
help
help



while getopts :c:s:d:h:I:p:r FLAG; do
  case $FLAG in


        s)
                SOURCE=$OPTARG
                ;;
        d)
                DESTINATION=$OPTARG
                ;;
        I)
                ISSUE=$OPTARG
                ;;
        c)
                CUSTOMER=$OPTARG
                test -e /etc/squid3/conf.d/$CUSTOMER.conf
                customer_available=$?
                ;;
        p)
                PORT=$OPTARG
                ;;
        h)      HELP=$OPTARG
                echo help
Run Code Online (Sandbox Code Playgroud)

bash arguments manpage getopt getopts

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

为 getopts 中解析的参数启用自动完成

我有一个 bash 脚本,用于getopts解析命令行参数。其中一个参数-l <name>针对if确定某些设置的语句。是否可以在命令行中进行自动完成工作以输入<name>参数的工作?

这是我的脚本的命令行解析部分 ( getopts):

while getopts 'l:r:m:?h' c
do
  case $c in
    l) 
        library=$OPTARG 
        ;;
    r)  
        rename_config=$OPTARG 
        ;;
    m)  
        align_mm=$OPTARG
        ;;  
    h|?) usage 
        ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

库选项 ( -l) 指的是脚本的这一部分:

if [ $library = "bassik" ];
    then
        read_mod="clip"
        clip_seq="GTTTAAGAGCTAAGCTGGAAACAGCATAGCAA"
        echo "Bassik library selected"
elif [ $library = "moffat_tko1" ];
    then
        read_mod="trim"
        sg_length=20    
        echo "Moffat TKO1 library selected"
elif [ $library = "sabatini" ];
    then
        read_mod="trim"
        sg_length=20    
        echo …
Run Code Online (Sandbox Code Playgroud)

bash autocomplete getopts

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

如何使用 getopts 处理多个输入文件参数

我正在尝试为多个输入数据文件制作脚本。最好的方法是什么,如何处理这些争论?脚本的用法应该是:

./script.sh -a sth -b sth - c sth -d sth input1 input2 input3    
Run Code Online (Sandbox Code Playgroud)

我可以使用 getopts 处理参数和参数,但我不知道如何处理这些输入文件,因为它们没有标志。谢谢

bash arguments getopts

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

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
查看次数

标签 统计

getopts ×10

bash ×8

getopt ×4

arguments ×2

shell ×2

autocomplete ×1

c++ ×1

darwin ×1

getopt-long ×1

linux ×1

manpage ×1

unix ×1