PDA

View Full Version : Shell String Operations



omrsafetyo
May 5th, 2008, 08:11 PM
I am trying to use the following substring expression:


${string:position:length}

Except that I am trying to pass a variable as the length, and I can't quite figure out how to do it. Instead I get an error message:


sub1=${name1:1:${lffn}}: 0403-011 The specified substitution is not valid for this command.


Anyone know if this is possible, and how to do it?

Many thanks.

omrsafetyo
May 5th, 2008, 08:44 PM
Solved:


sub1=`expr substr $name1 1 $lffn`

WW
May 5th, 2008, 08:45 PM
I just tried a simple version of this, and it worked. Are you sure $lffn is a correct length? Are you sure you are using bash?

In the following I created a short script with no #! in the first line. I'll run the script with bash and with sh:


$ cat strtest.sh
name1=1234567890
p=4
sub1=${name1:0:${p}}
echo $sub1
$ bash strtest.sh
1234
$ sh strtest.sh
strtest.sh: 3: Syntax error: Bad substitution
$

omrsafetyo
May 6th, 2008, 08:06 PM
I'm actually using ksh - and the man pages I have available locally did not address string substitution at all.

As soon as I substituted my second expression it worked. I was also echoing all variables at this point, as I usually do when building a script - so I am quite sure that the variable was the correct length.