LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-02-2007, 10:00 AM   #1
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Rep: Reputation: 0
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
 
Old 02-02-2007, 11:02 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,141

Rep: Reputation: 233Reputation: 233Reputation: 233
Missing quotes, no?
Try:
POSITION=`perl -e 'print index("$MYVARIABLE","$MYSUBSTRING");'`
 
Old 02-02-2007, 11:16 AM   #3
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 02-02-2007, 11:25 AM   #4
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 02-03-2007, 06:17 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
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 )

Last edited by makyo; 02-03-2007 at 06:26 PM.
 
Old 02-05-2007, 02:38 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
why not do the whole problem in perl?
 
Old 02-21-2011, 04:11 AM   #7
cathay4t
LQ Newbie
 
Registered: Mar 2006
Posts: 4

Rep: Reputation: 0
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'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - passing command as string lenzj Programming 3 08-24-2006 11:36 AM
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM


All times are GMT -5. The time now is 09:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration