ProgrammingThis 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.
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.
I am using a line of perl code in a bash script to search for a substring in a string.
MYSUBSTRING="TOP"
# I tried this line:
# POSITION=$(perl -e "print index($FILE_ATTENTE,$MYSUBSTRING);")
# and I also tried:
# POSITION=`perl -e "print index(${FILE_ATTENTE},${MYSUBSTRING});"`
# but with both previous I get the same exact result as with:
POSITION=`perl -e 'print index('$MYSTRING','$MYSUBSTRING');'`
echo "<!-- "
echo $POSITION
echo " "
echo $MYSTRING
echo " -->"
if [ $POSITION -gt 0 ]
then
# ... additiona code
fi
It works fine, giving "-1" when not found and an integer if found. Normal result:
<!--
-1
I get no "-1" value. Plus, I get the following complaints:
Bareword found where operator expected at -e line 1, near "0.MA"
(Missing operator before MA?)
syntax error at -e line 1, near "0.MA"
Execution of -e aborted due to compilation errors.
./getGFAQueues.sh: line 114: [: -gt: unary operator expected
Bareword found where operator expected at -e line 1, near "0.MP"
(Missing operator before MP?)
syntax error at -e line 1, near "0.MP"
Execution of -e aborted due to compilation errors.
./getGFAQueues.sh: line 114: [: -gt: unary operator expected
Don't understand what is causing the error. How do I fix?
Please post scripts and output in [code] tags to improve readability.
First thing: Use of ! in double quotes: bash will attempt to expand this to an event, so I would expect to a message something like:
Code:
bash: !--": event not found
Use single quotes to prevent this:
Code:
echo '<!--'
Second thing: what are the values of MYSTRING and MYSUBSTRING? After the perl line, it would help to add an echo of these values to be sure what is going on. For sure the problem is something to do with quotes. Here's how I'd do it in a way which reduces the mess of quotes:
The perl index function returns a null or blank on the following string values:
SYSTEM.JMS.D..MA
SYSTEM.JMS.D..MP
I am guessing it must be the concatenation operator (dot). All three of the following give the same results:
POSITION=`perl -e "print index(${MYSTRING},${MYSUBSTRING});"`
POSITION=`perl -e 'print index('$MYSTRING','$MYSUBSTRING');'`
POSITION=$(perl -e "print index($MYSTRING,$MYSUBSTRING);")
I guess I am now looking for a Perl funtion to indicate I need to ignore dots, which will wrap the index function.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.