cancel
Showing results for 
Search instead for 
Did you mean: 

bash script array

SOLVED
Go to solution
Kiyoshi Miyake
Frequent Advisor

bash script array

Hello all.

I need help for bash script.

I want to write script like this...

---
EE[0]="foo is FOO"
EE[1]="bars are BAR BAR BAR"
EE[2]=...
EE[3]=...
...

if grep -e ${EE[0]} -e ${EE[1]} -e ${EE[2]} .... file
then
foo...
fi
---

How do I write "grep..."?
EE array size is not fixed.

e.g.) GREP_OPT=${EE[@]://-e/}... this don't work well....

I hope smart script.

Thanks.
6 REPLIES
Stuart Browne
Honored Contributor
Solution

Re: bash script array

So basically you want to fill an array with patterns, then do a multi-pattern-grep with all the listed patterns?

Ok, that's not too hard.

Unfortunately you can't do a:

grep -e ${EE[@]/#/ -e }

As it treats the entire ${} as a single string/pattern. You need to build up your command line with a simple loop:

for ARG in ${EE[@]}
do
MY_ARG="-e $ARG $MY_ARG"
done
grep $MY_ARG file

Give it a shot, see how it does for you.
One long-haired git at your service...
Nicolas Dumeige
Esteemed Contributor

Re: bash script array

If you have a lot of patterns, maybe a pattern file would be simpler ;
uglier but easier to debug.

Perl is also great for pattern matching/replacing, and you'll have the case insensitive option :
perl -pe ' s/pattern1/replacement1/g ; s/pattern1/replacement1/iog ' filename

All different, all Unix
Kiyoshi Miyake
Frequent Advisor

Re: bash script array

Thank you for quick reply.

But, I need more help.

----
EE[0]="foo is FOO"
EE[1]="bars are BAR BAR BAR"

for ARG in ${EE[@]}
do
MY_ARG="-e $ARG $MY_ARG"
done
echo "$MY_ARG"
----
result of this script is:
---
-e BAR -e BAR -e BAR -e are -e bars -e FOO -e is -e foo
---

I need :
-e "foo is FOO" -e "bars are BAR BAR BAR"

EE[] is phrase.
What do I write?

thanks.
Nicolas Dumeige
Esteemed Contributor

Re: bash script array

Try this :

EE[0]="foo is FOO"
EE[1]="bars are BAR BAR BAR"

cpt=0
while [ cpt -lt ${#EE[@]} ]
do
MY_ARG="-e '${EE[cpt]}' $MY_ARG"
(( cpt += 1 ))
done
echo "$MY_ARG"
All different, all Unix
Kiyoshi Miyake
Frequent Advisor

Re: bash script array

Thanks Nicolas.

grep "$MY_ARG" ... don't work well.
but eval "$MY_ARG" (add grep) work well.

$ cat foo.sh
EE[0]="foo is FOO"
EE[1]="bars are BAR BAR BAR"

MY_ARG="grep -v "
cpt=0
while [ $cpt -lt ${#EE[@]} ]
do
MY_ARG="$MY_ARG -e '${EE[cpt]}'"
(( cpt += 1 ))
done

MY_ARG="$MY_ARG foo.sh"
echo $MY_ARG
eval $MY_ARG
$ sh foo.sh
grep -v -e 'foo is FOO' -e 'bars are BAR BAR BAR' foo.sh

MY_ARG="grep -v "
cpt=0
while [ $cpt -lt ${#EE[@]} ]
do
MY_ARG="$MY_ARG -e '${EE[cpt]}'"
(( cpt += 1 ))
done

MY_ARG="$MY_ARG foo.sh"
echo $MY_ARG
eval $MY_ARG
$


# but. script become long...

Thanks a lot.
Francisco J. Soler
Honored Contributor

Re: bash script array

Hi,
if you don't want to use an array you can try this one:

MY_ARG=`(cat << EOF
foo is FOO
bar are BAR BAR BAR
more things
EOF
) | awk '{printf("grep -e \"%s\"\n",$0)}'`

echo $MY_ARG


Be careful with the quotation, cut and paste if you don't want errors.

Frank.
Linux?. Yes, of course.