- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- General
- >
- bash script array
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Latin America
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 06:54 PM
04-04-2004 06:54 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 07:14 PM
04-04-2004 07:14 PM
SolutionOk, 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 07:39 PM
04-04-2004 07:39 PM
Re: bash script array
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 08:19 PM
04-04-2004 08:19 PM
Re: bash script array
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 09:43 PM
04-04-2004 09:43 PM
Re: bash script array
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 10:04 PM
04-04-2004 10:04 PM
Re: bash script array
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-04-2004 10:20 PM
04-04-2004 10:20 PM
Re: bash script array
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.
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP