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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
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
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,466
|
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: 735
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: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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'
|
|
|
All times are GMT -5. The time now is 06:58 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
|
|