H
H
HitGirl2020-11-20 14:01:49
bash
HitGirl, 2020-11-20 14:01:49

Why is grep not working?

I'm trying to find options among script parameters.

while [ -n "$1" ]
do
arg=`echo $1 | grep ^-[A-Za-z]+`
echo $arg
if [ "$arg" != "" ]
then 
echo "опция"
fi
shift
done

Why is grep not finding options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Koryukov, 2020-11-20
@MadridianFox

Because the pattern must be in quotation marks. And in grep, you must explicitly indicate that the pattern is a regular expression with the -E option
Do
grep - E 'ваш паттерн'

G
ge, 2020-12-01
@gedev

As noted in the comments, it is better to work with options differently. For example using case like this . I encourage you to check out the other examples as well.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question