- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: bash script array
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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.