简体   繁体   English

如何在bash中匹配连字符

[英]How to match a hyphen in bash

Part of my script takes in all parameters and looks for any flag options. 我的脚本的一部分接受了所有参数,并寻找任何标志选项。 I'm trying to save these into my array, but it doesn't seem to be matching. 我正在尝试将它们保存到我的数组中,但似乎不匹配。 I can't figure it out, what am I missing? 我想不通,我想念什么?

#!/bin/bash
ALL_PARAMS=( "$@" )

ARGUMENTS=()

OPTIONS=()

for i in ${ALL_PARAMS[@]}

do

    if [ $i == ^- ]
    then
        ARGUMENTS+=($i)
    else
        OPTIONS+=($i)
    fi
done

echo ${ARGUMENTS[@]}
echo ${OPTIONS[@]}

The test command ( [ ) does not do Regex matching, the bash keyword [[ does. test命令( [ )不执行Regex匹配, bash关键字[[则执行。

You need: 你需要:

[[ $i =~ ^- ]]

Also note that, you need Regex operator =~ instead of equality operator == . 另请注意,您需要Regex运算符=~而不是相等运算符==

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

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