man4ish
1
I have script data.sh which has following error.
Script Name : data.sh
#!/bin/sh
infile=$1
len=${#infile}
echo $len
texfile=${infile:0:$len-4}
echo $texfile
run command
./data.sh acb.xml
I get following error message:
./data.sh: 7: Bad substitution
May i know where i am wrong? How to fix this pblm?
As a guess: the shebang is calling a POSIX shell that does not support the syntax.
Change the first line of the script to:
#!/bin/bash
So we do not guess, please let us know what UNIX you are using and your shell.
agama
3
Even with a #! change, I think something like this is needed:
${infile:0:$((len-4))}
as long as bin/sh is a bash or kshell.
Yoda
4
Works with bash:-
# cat substr.sh
#!/bin/bash
infile=$1
len=${#infile}
echo $len
texfile=${infile:0:$len-4}
echo $texfile
# ./substr.sh bipinajith
10
bipina
@agama, don' think ksh supports the substring expansion and certainly bash will evaluate the length field so ${infile:0:len-4}
is acceptable.
1 Like
agama
6
I tried in Kshell -- it needed $(( ... )) and assumed that bash required the same. My bad for not testing with bash.
Thanks.