Substr throws an ERROR. Any alternatives?

Can somebody please help me to remove the last character of a string.??

I have a string variable, in which I dynamically put values in a for loop.I want to remove the last character from the string.

But, the problem is I will not know which character can come in the string (Its inside for loop). So I tried using substr. substr does not work here.

Is there any alternative to do this?? Here my code goes.

....
for files in $rawSourceFiles
loop
MID_INFILE=`cat �${files}� | grep �Message-Id:"`
len=`expr �${MID_INFILE}� : �.*'`
len=`expr $len - 1`
trimmedMID_INFILE=`expr substr �${MID_INFILE}� 1 ${len}`

.....
...
done

But, it does not seem working. It throws an error �expr: syntax error� ..

What all I want here is in trimmedMID_INFILE should be the string MID_INFILE with last character trimmed. (last char could be anything like "\n", "/", etc.)

Can anybody please enlighten me?? I would be so grateful. Thanks in advance.

Considering the ''expr' error that you are getting is from the code where you are trying to 'trim', than use the following:


trimmedMID_INFILE=`echo $MID_INFILE | cut -b 1-$len`

  • MID_INFILE is you string.
  • cut -b (cut by byte)
  • 1-$len (from 1st byte till $len - considering $len is already length - 1)

:b:

Thanks Hemangjan.. !!! Thanks a lot.. It worked.. Yay...!!!
:slight_smile:

One more question,

if [ $a = $b ]
then
echo SUCCESS
fi

If I want to case-insensitive-compare these two variables, what should I use?/

Sorry to ask these dumb questions. I'm a newbie to SHELL SCRIPTING..

:frowning:

This link may help