Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
02-02-2007, 10:00 AM
#1
LQ Newbie
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20
Rep:
passing variable from bash to perl in a bash script
Hello,
I need to find the first position of an exact substring (not a regular expression) inside a substring and bash is not adequate.
For example in the following bash snippet, 1 will be echoed because the first position of either "a" or "t" in "adequate" is 1 :
#!/bin/bash
MYVARIABLE="adequate"
POSITION=`expr index "$MYVARIABLE" at`
echo $POSITION
exit
I could embed the following Perl in my bash script:
#!/bin/bash
echo "Index value is..."
# Embedded Perl script.
POSITION=`perl -e 'print index("Once upon a time","im");'`
echo $POSITION
exit
Now the problem:
How do I replace "Once upon a time" and "im" in the embedded Perl line with bash variables? I need to do:
#!/bin/bash
MYVARIABLE="adequate"
MYSUBSTRING="at"
# Embedded Perl script.
POSITION=`perl -e 'print index($MYVARIABLE,$MYSUBSTRING);'`
echo $POSITION
exit
02-02-2007, 11:02 AM
#2
Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 959
Rep:
Missing quotes, no?
Try:
POSITION=`perl -e 'print index("$MYVARIABLE","$MYSUBSTRING");'`
02-02-2007, 11:16 AM
#3
LQ Newbie
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20
Original Poster
Rep:
Nope
Nope, the following will echo "0"
#!/bin/bash
MYVARIABLE="adequate"
MYSUBSTRING="at"
# Embedded Perl script.
POSITION=`perl -e 'print index("$MYVARIABLE","$MYSUBSTRING");'`
echo $POSITION
exit
02-02-2007, 11:25 AM
#4
LQ Newbie
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20
Original Poster
Rep:
single-quote works
The following works, echoing "5":
#!/bin/bash
MYVARIABLE="adequate"
MYSUBSTRING="at"
# Embedded Perl script.
POSITION=`perl -e 'print index('$MYVARIABLE','$MYSUBSTRING');'`
echo $POSITION
exit
02-03-2007, 06:17 PM
#5
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706
Rep:
Hi.
Double quotes will work:
Code:
#!/bin/bash
# @(#) s1 Demonstrate variable evaluation inside double quotes.
MYVARIABLE="adequate"
MYSUBSTRING="at"
# Embedded Perl script.
POSITION=`perl -e "print index($MYVARIABLE,$MYSUBSTRING);"`
echo $POSITION
exit 0
To produce:
I placed the quotes around the perl snippet, and the shell evaluated the variables inside. I recommend the alternate syntax for backquotes:
Code:
POSITION=$(perl -e "print index($MYVARIABLE,$MYSUBSTRING);")
because they nest more easily, and are more visually apparent ... cheers, makyo
( edit 1: add, clarify )
Last edited by makyo; 02-03-2007 at 06:26 PM .
02-05-2007, 02:38 AM
#6
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
why not do the whole problem in perl?
02-21-2011, 04:11 AM
#7
LQ Newbie
Registered: Mar 2006
Posts: 4
Rep:
Inspired by awk code.
For awk who want to use bash variable:
awk -F" " {'if ($2=="'${BASH_VAR}'") print $7'}
For Perl who want to use bash variable:
Quote:
#!/bin/bash
BASH_VAR="10:00:00:00:c9:90:be:2b"
echo "7 1 (0x1) 010600 N 2,3 10:00:00:00:c9:90:be:2b 20:00:00:00:c9:90:be:2b" | \
perl -ne 'print hex($1) if /\) +([0-9]+) +.* '"${BASH_VAR}"' / and /[0-9]{2}([0-9]{2})[0-9]{2}/'
exit 0
###############
Actually, it's bash's job to expand ${BASH_VAR} rather than perl. These code explain a lot.
BTW: I do love 'perl -ne'
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:44 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News