1825789 Members
2093 Online
109687 Solutions
New Discussion

expr: Syntax Error

 
SOLVED
Go to solution
panchpan
Regular Advisor

expr: Syntax Error

Hello.
I use below commands in my script:
fic=$(basename $extent_full)
long=`expr length $fic`
lgext=`expr $long - 1`
ext=`expr substr $fic $lgext 2`

Is there something incorrect with syntax as the error is coming everytime:


expr: Syntax error

expr: Syntax error

expr: Syntax error


Thanks for your advice.
5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: expr: Syntax Error

Hi,
is fic variable null ?

insert an
echo "[$fic]"
between your fic= and long= lines

fic=$(basename $extent_full)
echo "[$fic]"
long=`expr length $fic`

Possible fix is to use quotes around the $fic:
long=`expr length "$fic"`

Matti_Kurkela
Honored Contributor

Re: expr: Syntax Error

What is the value of $extent_full when your commands are executed?

Add some debugging commands like:
echo "/$fic/"
to display the contents of the variables.
The slashes are there to reveal any spaces before or after the value.

MK
panchpan
Regular Advisor

Re: expr: Syntax Error

I still get this error once rather than 3 times after modification:

fic=$(basename $extent_full)
long=`expr length "$fic"`
lgext=`expr "$long" - 1`
ext=`expr substr "$fic" "$lgext" 2`
echo "[$fic]"
echo "[$long]"
echo "[$lgext]"
echo "[$ext]"

Values are:

[]
[0]
[-1]
[]

James R. Ferguson
Acclaimed Contributor

Re: expr: Syntax Error

Hi:

> I still get this error once rather than 3 times after modification:

Values are:

[]
[0]
[-1]
[]

...and that's exactly what you get if $extent_full is empty or undefined!

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: expr: Syntax Error

Hi,

are you shure, your expr command understand the extend (non standard) operators?


A standard conform solution very close to your attempt may be:
fic=$(basename $extent_full)
long=$(expr $fic : '.*')
lgext=$(expr $long - 2)
ext=$(expr $fic : '.\{'$lgext'\}\(.*\)')

If I read correctly, you want to get the last two characters of $extend_full. You can get that much easier; try one of these:
expr $extend_full : '.*\(..\)'
print $extend_full | sed 's/.*\(..)/\1/'
print $extend_full | awk '{print substr($0,length($0)-2)}'
or in Posix shell:
typeset -R2 str=$extend_full
print $str

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"