Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable name substitution

Status
Not open for further replies.

gotchausa

Programmer
Jan 24, 2002
64
0
0
CA
I'm using ksh with the following script snippet:

SERVER_PREFIX="Server:"
read role <someFile
name=${role}#$SERVER_PREFIX}

This works fine but when I run it in Bourne shell, I get an error.. 'bad subsitution' for the last line of script.
Does anyone know what the correct syntax is?

 
looks like you might want to get ride of the last curly bracket.
 
!! Sorry, the syntax that works is:

name=${role#$SERVER_PREFIX}

not

name=${role}#$SERVER_PREFIX}

 
Bourne shell doesn't have this functionality - removing prefix/suffix from the variable value.

Use sed/cut/awk to chop off the prefix instead.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Based on some reference, bsh does not seem to have different syntax on this specific command: ${parameter#string} for smallest match
${parameter##string} for smallest match

But I do not have a bsh to test it.

To add to vlad's idea, here is something you might find it useful in case you have not run into it:
${#SERVER_PREFIX} would give you the length if $SERVER_PREFIX has variable length.
Not sure if bsh has the same feature.
 
Thx all. I've changed my script to use ksh instead...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top