简体   繁体   English

bash getopts无法识别参数

[英]bash getopts not able to recognize arguments

We have a getopts script to retrieve the arguments as below 我们有一个getopts脚本来检索参数,如下所示

#!/bin/bash
while getopts ":d:l:f:o:" OPT;
do
    echo 'In Test Script - Got Options '$OPT ' with' $OPTIND ' and ' $OPTARG
    case $OPT in
        d)
            echo $OPTARG;;
        f)
            echo $OPTARG;;
        l)
            echo $OPTARG;;
        ?)
            echo $OPTARG;;
    esac
done

We receive an argument which is parsed in another script and passed to the getopts script and it works fine for single entry eg 12345,-d somedesc -l somelabel 我们收到一个参数,该参数在另一个脚本中进行了解析,然后传递给getopts脚本,它对于单个输入有效,例如12345,-d somedesc -l somelabel

#!/bin/bash
INFO="12345,-d somedesc -l somelabel"
ID=`echo "$INFO" | awk -F "," "{ print $"1" }"`
OPTIONS=`echo "$INFO" | awk -F "," "{ print $"2" }"`
sh test.sh $OPTIONS

However, we receive multiple entries eg 12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel and are using loop and awk to split the arguments further in which case the getopts does not get triggered even though the OPTIONS are correctly passed. 但是,我们收到多个条目,例如12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel并且正在使用loopawk进一步拆分参数,在这种情况下,即使选项正确,也不会触发getopts通过。

#!/bin/bash
INFO="12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel"
IFS=":"
set $INFO
echo 'Parsing INFO '$INFO
for item
do
    echo 'Item is '$item
    #parsing each item to separate id and options
    ID=`echo "$item" | awk -F "," "{ print $"1" }"`
    echo 'ID is '$ID
    OPTIONS=`echo "$item" | awk -F "," "{ print $"2" }"`
    echo 'Invoking Test Script with '$OPTIONS
    sh test.sh $OPTIONS
done

Any reason the getopts is not able to recognize the OPTIONS ? 任何原因getopts无法识别OPTIONS吗?

Problem is that you're changing value of IFS on top of your script to colon : and then passing arguments to your script test.sh while IFS is still set to : . 问题是,你在你的脚本的顶部改变IFS的价值冒号:然后将参数传递给你的脚本test.sh而IFS仍设置为: Which in effect is being called as: 实际上被称为:

1st time: 第一次:

sh test.sh "-d somedesc -l somelabel"

and 2nd time: 和第二次:

sh test.sh " -d anotherdesc -l anotherlabel"

Thus making argument list into a single argument and getops fails. 因此,使参数列表成为单个参数和getops失败。

What you need to do is to save original IFS before you set it to colon and restore it after set command like this: 您需要做的是保存原始IFS,然后再将其设置为冒号,并在set命令后将其还原,如下所示:

#!/bin/bash
INFO="12345,-d somedesc -l somelabel:6789, -d anotherdesc -l anotherlabel"
# save IFS value
OLDIFS=$IFS
IFS=":"
set $INFO
# restore saved IFS value
IFS=$OLDIFS

echo 'Parsing INFO '$INFO
for item
do
    echo 'Item is '$item
    #parsing each item to separate id and options
    ID=`echo "$item" | awk -F "," "{ print $"1" }"`
    echo 'ID is '$ID
    OPTIONS=`echo "$item" | awk -F "," "{ print $"2" }"`
    echo 'Invoking Test Script with '$OPTIONS
    sh test.sh $OPTIONS
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM