标签: getopts

如何在 bash 中使用 getopts

我是 bash 脚本编写的新手,我正在尝试使用 getopts 编写一个脚本,以便在script.sh -sp调用时打印出 URL 以及列和行计数。当script.sh -r调用选项时,仅打印文件类型。

脚本 :

#!/bin/bash

#define the URL 
URL='https://github.com/RamiKrispin/coronavirus/tree/master/data_raw'
#define file name
filename=$(basename "$URL")
#dowload the file
wget -q "${URL}"

while getopts ":sp:r" o; do
   case $o in
   
   sp ) echo URL: $URL #print the URL address
       awk 'BEGIN{FS=","}END{print "COLUMN NO: "NF " ROWS NO: "NR}' $filename #print the column count and row count
   r ) file $filename #print the file type 
       exit 1
        
   esac
done 
Run Code Online (Sandbox Code Playgroud)

谁能帮助我了解如何正确使用 getopts?

scripting bash shell-script getopts

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

如何正确捕获可选和非可选参数?

我想编写一个 shell 脚本,它将接受一些带有一些选项的参数并打印这些参数。假设该脚本的名称是abc.ksh. 该脚本的用法是 -
./abc.ksh -[a <arg>|b <arg>|c|d] <some_string>
现在我编写一个 shell 脚本,它将接受选项和参数

#!/bin/ksh

# Default Values
vara=","
varb=false
varbname=""
varc=false

# Scanning inputs
while getopts :a:b:cd option
do
        case $option in
                a) vara=$OPTARG;;
                   #shift $((OPTIND-1));;
                b) varb=true
                   varbname=$OPTARG;;
                   #shift $((OPTIND-1));;
                c) varc=true;;
                   #shift $((OPTIND-1));;
                d) echo "Usage $0 \-[a|b|c|d] <filename>"
                   exit 0;;
                \?) echo "Invalid option -$OPTARG. Please run '$0 -h' for help"
                    exit 1;;
                :) echo "Option -$OPTARG requires an argument. Please run '$0 -d' for …
Run Code Online (Sandbox Code Playgroud)

ksh options input shell-script getopts

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

如何使用任意字符串解析命令行参数

我正在尝试创建一个脚本,该脚本具有一个选项,该选项将包含用引号括起来的任意文本(包括空格),这证明很难搜索和实现。

基本上我想要的行为是docker_build_image.sh -i "image" -v 2.0 --options "--build-arg ARG=value",这将是一个帮助脚本,用于使用我们的构建服务器简化 docker 图像的版本控制。

我最接近成功获取--options值的方法给了我 getopt 的错误,“无法识别的选项 '--build-arg ARG=value'。

完整脚本如下

#!/usr/bin/env bash
set -o errexit -o noclobber -o nounset -o pipefail
params="$(getopt -o hi:v: -l help,image:,options,output,version: --name "$0" -- "$@")"
eval set -- "$params"

show_help() {
cat << EOF
Usage: ${0##*/} [-i IMAGE] [-v VERSION] [OPTIONS...]

Builds the docker image with the Dockerfile located in the current directory.

    -i, --image         Required. Set the name of the image.
    --options           Set …
Run Code Online (Sandbox Code Playgroud)

bash shell-script getopts

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

`getopts` 遇到 `--` 时的行为是什么?

按照惯例--,这表明它之后没有更多选择。在我看来,使用getoptswithcase子句时,-)模式子句与--. 那么getopts当它遇到时的行为是什么--?它--是作为一个选项,一个非选项参数,还是两者都不是?谢谢。

bash getopts

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

getopts 似乎不起作用

我正在尝试运行以下脚本getopts来解析选项,但它似乎不起作用:

#!/bin/bash
set -x
echo $@
while getopts "rf" opt
do
  case "${opt}" in
    r)
        ropt=${OPTARG}
        ;;
    f)
        fopt=${OPTARG}
        ;;
  esac
done

shift $((OPTIND -1))

echo $fopt $ropt
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

$ ./myscript.sh -f opt2 -r opt1
+ echo -f opt2 -r opt1
-f opt2 -r opt1
+ getopts rf opt
+ case "${opt}" in
+ fopt=
+ getopts rf opt
+ shift 1
+ echo

+ set +x
Run Code Online (Sandbox Code Playgroud)

你对我做错了什么有任何想法吗?

bash getopts

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

getopts 的意外行为

考虑:

#!/bin/sh

while getopts ":h" o; do
  case "$o" in
    h )
    "Usage:
    sh $(basename "$0") -h      Displays help message
    sh $(basename "$0") arg     Outputs ...

     where:
    -h   help option
        arg  argument."
    exit 0
    ;;
    \? )
    echo "Invalid option -$OPTARG" 1>&2
    exit 1
    ;;
    : )
    echo "Invalid option -$OPTARG requires argument" 1>&2
    exit 1
    ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

这个调用返回not found为什么?

$ sh getopts.sh -h
getopts.sh: 12: getopts.sh: Usage:
    sh getopts.sh -h        Displays help message
    sh getopts.sh arg …
Run Code Online (Sandbox Code Playgroud)

shell-script getopts

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

在 bash 脚本中使用命令行参数作为 cp 和 mv 的目标

我试图通过运行带有标志/参数的脚本来复制文件(或重命名文件)以提供源文件名和目标文件名:

#!/bin/bash/

while getopts s:d flag
do
        case "${flag}" in
                s) copy_source=${OPTARG};;
                d) copy_dest=${OPTARG};;
        esac
done

echo "Copy a file input with argument to another file input with argument"
cp $copy_source $copy_dest
Run Code Online (Sandbox Code Playgroud)

输出是一个错误:

sh test_cp.sh -s  file1.txt -d file2.txt
Copy a file input with argument to another file input with argument
cp: missing destination file operand after ‘file1.txt’
Try 'cp --help' for more information.
Run Code Online (Sandbox Code Playgroud)

是否cp(和mv)不接受参数化的目的地是哪里?我究竟做错了什么?

bash shell-script getopts

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

带帮助参数的 shell getopt

我正在准备一个脚本getopt。我想添加帮助部分。因此,如果他们使用--helpor-h它应该执行该功能(只需打印指令)并返回它。

示例代码:


log_type='unset'
state='unset'
date='unset'
help='unset'


mode="$1"
usage()
{
  echo "Usage: LogRotator [ -t | --log_type  - Allowed values: [access error] ] 
                          [ -s | --state - Allowed values: [archive or backup] ]
                          [ -d | --date 1-10-2020] 
                          [ -h | --help]

        Modes:
            1) list - list the files
            2) restore  - restore the files"
  exit 2
}


ARGUMENT_LIST=(
    "log-type"
    "state"
    "date"
)



# read arguments
opts=$(getopt \
    --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")",help \
    --name …
Run Code Online (Sandbox Code Playgroud)

shell getopts

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

getopts 参数可以与其他输入结合使用吗?

我正在编写一个脚本,该脚本将字符串作为输入以及用户可以通过使用参数作为指示符来选择的其他选项。换句话说,是这样的:

./script "My input string" -pxz
Run Code Online (Sandbox Code Playgroud)

或者

./script -pxz "My input string"
Run Code Online (Sandbox Code Playgroud)

但我遇到了以下问题。输入非 getopts 样式的参数后, getopts 命令似乎停止工作。检查一下,例如:

#!/bin/bash

while getopts "ab" arg; do
    echo $arg received
done
Run Code Online (Sandbox Code Playgroud)

当我们运行它时,我们得到:

$ ./example.sh -a -b -a string -b
a received
b received
a received
$
Run Code Online (Sandbox Code Playgroud)

它停在“string”处并且不会继续。getopts 命令返回非零退出状态,因为“string”不是它期望读取的参数类型,并且 while 循环结束。我尝试添加第二个,while getopts但这没有任何作用。getopts 的“读头”仍然停留在该“字符串”参数上,并且将再次以非零退出状态退出。

这意味着,看起来,我无法让用户先输入字符串,然后输入选项,因为 getopts 永远无法读取字符串并到达那里。所以我不能这样做:

./script "My input string" -pxz
Run Code Online (Sandbox Code Playgroud)

另一方面,如果我告诉用户首先输入他们的选项,然后输入字符串,我就会遇到如何检索字符串的问题。通常,如果没有选择,我会这样做:

string="$1"
Run Code Online (Sandbox Code Playgroud)

但现在由于有选项,而且我不知道有多少个,我不再知道字符串占据什么位置。那么我怎样才能找回它呢?

现在,理想情况下,我的脚本实际上应该能够以这两种方式甚至两者的组合来工作。它应该能够处理如下输入:

./script -p "My input string" -xz
Run Code Online (Sandbox Code Playgroud)

那么我该如何解决这个问题呢?

bash shell-script getopts

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

getopts 超过 4 个解析器参数

我想使用下面的代码getopts在脚本中解析多个参数。bash

while getopts b:B:m:M:T flag
do
    case "${flag}" in
        b) rbmin=${OPTARG};;
        B) rbmax=${OPTARG};;
        m) mbmin=${OPTARG};;
        M) mbmax=${OPTARG};;
        T) sigType=${OPTARG};;
    esac
done
echo $rbmin,$rbmax,$mbmin,$mbmax, $sigType
Run Code Online (Sandbox Code Playgroud)
[amit@amitk]$ sh pass.sh -b 0.1 -B 0.3  -m 10 -M 11 -T sig 
0.1,0.3,10,11,
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我不能通过超过四个参数。有什么建议么?

bash getopts

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

标签 统计

getopts ×10

bash ×7

shell-script ×6

input ×1

ksh ×1

options ×1

scripting ×1

shell ×1