LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing variable from bash to perl in a bash script (https://www.linuxquestions.org/questions/programming-9/passing-variable-from-bash-to-perl-in-a-bash-script-524991/)

quadmore 02-02-2007 10:00 AM

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

Guttorm 02-02-2007 11:02 AM

Missing quotes, no?
Try:
POSITION=`perl -e 'print index("$MYVARIABLE","$MYSUBSTRING");'`

quadmore 02-02-2007 11:16 AM

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

quadmore 02-02-2007 11:25 AM

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

makyo 02-03-2007 06:17 PM

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:
Code:

% ./s1
5

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 )

bigearsbilly 02-05-2007 02:38 AM

why not do the whole problem in perl?

cathay4t 02-21-2011 04:11 AM

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 07:03 PM.