Posted by: ukengb
bizarre shell problem - 16/03/2005 19:43
I have a script that includes the following line:-
[ ${@:0-2:1} = '.' ] || echo
but after displaying the text I get
Segmentation fault
If I run the same code on the command line I don't get the error.
If I change the substr code in the script to
${@:0-2}
it's OK - no error, but of course isn't doing what I want.
So what can be the problem that causes the error only when in a shell script, but not otherwise?
Come to think of it, what's a segmentation fault?
Posted by: mlord
Re: bizarre shell problem - 16/03/2005 21:13
What shell is that for, anyway? Bash doesn't like it as-is.
But bash does accept this version:
[ ! ! "${@:2:1}" = "." ] || echo
Cheers
Posted by: wfaulk
Re: bizarre shell problem - 17/03/2005 12:52
ISTR that if the first line of your shell is "#!/bin/sh" then bash falls back to some sort of Bourne pseudo-compatibility. If you've got that in there, then try changing it to "#!/bin/bash".
This is just a guess, though.
Posted by: mlord
Re: bizarre shell problem - 17/03/2005 13:29
I take it you want code to detect the final character of the final arg, and tell whether it was a period or not?
Code:
#!/bin/bash
all="$@"
if [ "${all##*.}" = "" ]; then
echo "ended with a dot"
else
echo "did not end with a dot"
fi
Posted by: wfaulk
Re: bizarre shell problem - 17/03/2005 15:16
Hmm. I wonder if that's the problem -- that his test case had less than two characters. Reading past the end of an array would be a common cause for a seg fault and I can believe that there was a version of bash that failed to check for the front end of the array. Seems unlikely, but possible, especially if there was a different bug that failed to put the right thing in the variable being checked.
Posted by: mlord
Re: bizarre shell problem - 17/03/2005 22:27
The last version I posted here runs correctly from a script on my empeg player.
Cheers
Posted by: ukengb
Re: bizarre shell problem - 18/03/2005 06:27
OK, I've already re-written my script so that the error doesn't occur, but in the interests of our quest for knowledge and truth, I've doen some more checking:-)
My original line was
[ "${@:0-2:1}" = "." ] || echo
and this causes the seg. fault - always. But if I replace this with:-
anyvarname="$@"
[ "${anyvarname:0-2:1}" = "." ] || echo
there is no error. IOW the problem only relates to when $@ is used and seems to confirm the existence of a bash internal bug.
It's no longer a problem for me, but interesting to know what not to do in the future.
Posted by: ukengb
Re: bizarre shell problem - 18/03/2005 11:11
A quick question for Mark.
Hijack seems to eliminate multiple spacing from text strings before displaying them in a popup, am I right about this, or is there some other reason why my double spaces are not appearing in the displayed popup message?