LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   String manipulation won't work in script but will if extracted (http://www.linuxquestions.org/questions/slackware-14/string-manipulation-wont-work-in-script-but-will-if-extracted-4175432217/)

waddles 10-14-2012 09:21 PM

String manipulation won't work in script but will if extracted
 
Using Slackware 13.37 w/ bash 4.1.10.
Using the following commands in a bash script fails with:
syntax error near unexpected token `}'

bash-4.1$ pknt=789 <-----added for interactive mode
bash-4.1$ jj="00000$pknt"
bash-4.1$ jj=`echo ${jj:(-3)}` <-----error??
bash-4.1$ echo $jj
789
but as U can C it works fine when cut and pasted in interactive mode.
Tried as ${jj: -3} but gets same error.
Did I leave out some command to tell bash I would use string manipulation or is this a bug?
If there is a sad face it is covering ":("

willysr 10-14-2012 09:23 PM

Do you mind pasting the code here? It would make debugging easier

T3slider 10-14-2012 09:30 PM

Are you using
Code:

#!/bin/sh
instead of
Code:

#!/bin/bash
at the start of the script? I think parameter expansion like this may be a bashism...though I could be wrong.

wildwizard 10-15-2012 12:52 AM

Quote:

Originally Posted by T3slider (Post 4805741)
Are you using
Code:

#!/bin/sh
at the start of the script? I think parameter expansion like this may be a bashism...though I could be wrong.

Here is the list of everything that does change when you do (call it as sh) :-

http://www.gnu.org/software/bash/man...OSIX-Mode.html

larryhaja 10-15-2012 09:47 AM

Quote:

Originally Posted by waddles (Post 4805734)
bash-4.1$ pknt=789 <-----added for interactive mode
bash-4.1$ jj="00000$pknt"
bash-4.1$ jj=`echo ${jj:(-3)}` <-----error??
bash-4.1$ echo $jj
789

I don't have a 13.37 computer to work with but it works on Slackware64 14.0 no matter what the shebang I use, as well as in interactive mode. I'm not sure why you would want to use the first echo statement. It is pretty much redundant in that usage.

T3slider 10-15-2012 12:51 PM

It also works on Slackware64-13.37 using /bin/sh and /bin/bash, but it does not work when using /bin/ash (or in interactive mode using ash). The link wildwizard posted mentions parameter expansion but not *which* uses are valid. Obviously bash doesn't discriminate, but other shells might (see `man ash`, which doesn't list
Code:

${parameter:offset}
as valid). I have no idea why it isn't working for you though, assuming /bin/sh is linked to /bin/bash.

waddles 10-17-2012 07:57 AM

String manipulation won't work in script but will if extracted
 
willysr, when I went to get that segment of the script I found a missing preceding "if"-- Dah! working too long
Apologies


All times are GMT -5. The time now is 10:14 PM.