解析包含bash脚本中的空格的输入选项

时间:2021-04-27 15:43:22

I have a bash script parsing input option with a block of code like the following

我有一个bash脚本解析输入选项与一个代码块,如下所示

for WORD in "$@" ; do
 case $WORD in
  --*) true ;
    case $WORD in
      --opt1=*) 
          OPT1=${WORD/--opt1=/}
          shift ;;
      --opt2=*) 
          OPT2=${WORD/--opt2=/}
          shift ;;
      *) echo "Unrecognized argument $WORD"      
          ;; 
    esac ;;
  *) echo "Option $WORD not starting with double dash."  
  ;; 
 esac
done

The script is invoked by another parent program which creates the entire command line. The output created by this parent program looks like

该脚本由另一个父程序调用,该程序创建整个命令行。此父程序创建的输出如下所示

./childscript.sh "--opt1=value1 --opt2=value2"

The problems appear when the generated line looks like

生成的行看起来时会出现问题

./childscript.sh "--opt1='value11 value12' --opt2=value2"

The scripts complains saying

脚本抱怨说

Option value12 not starting with double dash.

How can I modify the child bash code to make it understand white spaces inside the input options?

如何修改子bash代码以使其理解输入选项中的空格?

1 个解决方案

#1


1  

I don't think the generated line is what you think it is.

我不认为生成的行是你认为的。

Your code works completely fine for me if I simply invoke it directly. With added echoes to check that the values are being stored in the right place:

如果我直接调用它,你的代码对我来说完全没问题。添加回声以检查值是否存储在正确的位置:

$ ./child.sh --opt1='v1 v2' --opt2='v3 v4'
OPT1='v1 v2'
OPT2='v3 v4'

You should be able to confirm this. Your problem isn't in making the child script accept arguments like these, it's in having the parent script invoke it correctly.

你应该能够证实这一点。你的问题不是让子脚本接受这样的参数,而是让父脚本正确地调用它。

And by the way, you don't actually want to run something like this:

顺便说一句,你实际上并不想运行这样的东西:

./childscript.sh "--opt1=value1 --opt2=value2"

That will cause that entire string (--opt1=value1 --opt2=value2) to be read as a single argument. I suspect that you haven't told us the full story on the way the parent script is calling this. If you show us those details, we can probably help out more - or maybe this is enough of a hint.

这将导致整个字符串(--opt1 = value1 --opt2 = value2)被读取为单个参数。我怀疑你没有告诉我们关于父脚本调用它的方式的完整故事。如果你向我们展示这些细节,我们可能会提供更多帮助 - 或者这可能是一个暗示。

#1


1  

I don't think the generated line is what you think it is.

我不认为生成的行是你认为的。

Your code works completely fine for me if I simply invoke it directly. With added echoes to check that the values are being stored in the right place:

如果我直接调用它,你的代码对我来说完全没问题。添加回声以检查值是否存储在正确的位置:

$ ./child.sh --opt1='v1 v2' --opt2='v3 v4'
OPT1='v1 v2'
OPT2='v3 v4'

You should be able to confirm this. Your problem isn't in making the child script accept arguments like these, it's in having the parent script invoke it correctly.

你应该能够证实这一点。你的问题不是让子脚本接受这样的参数,而是让父脚本正确地调用它。

And by the way, you don't actually want to run something like this:

顺便说一句,你实际上并不想运行这样的东西:

./childscript.sh "--opt1=value1 --opt2=value2"

That will cause that entire string (--opt1=value1 --opt2=value2) to be read as a single argument. I suspect that you haven't told us the full story on the way the parent script is calling this. If you show us those details, we can probably help out more - or maybe this is enough of a hint.

这将导致整个字符串(--opt1 = value1 --opt2 = value2)被读取为单个参数。我怀疑你没有告诉我们关于父脚本调用它的方式的完整故事。如果你向我们展示这些细节,我们可能会提供更多帮助 - 或者这可能是一个暗示。