|
Home > Archive > Unix Shell > March 2004 > [KSH] How can I print an underscore between two variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[KSH] How can I print an underscore between two variables
|
|
| Andrew Falanga 2004-03-25, 11:36 am |
| Ok, as stated in the subject I'm working KSH. It's ksh '88 (don't know
if that matters, but I thought I should throw it out).
Basically, in my script I'm trying to do the following:
somefunc () {
exec 5>/tmp/out.goes.here
datestamp=$(date +%Y%m%d)
# there is a variable called prod that changes based upon other
# criteria in the script, outside this function
# many other print lines, but this is the one giving grief
print -u5 "/path/to/file/to/examine/$var_$datestamp"
unset datestamp
exec 5>&-
}
Basically, this function is dynamically building a configuration to be
used by another program. Now, when the above function is executed, only
the datestamp variable contents are output. This is because ksh
believes I'm referencing a variable by the name var_. Rather, I want an
underscore between the contents of the two variables.
I'm tried a few different things. If I precede the '_' with a '\', the
'\' character is output as well. Not desireable. What options do I
have available to fix this?
Thanks,
---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.
| |
| joe@invalid.address 2004-03-25, 11:36 am |
| Andrew Falanga <falandrew@hp.com> writes:
> Ok, as stated in the subject I'm working KSH. It's ksh '88 (don't
> know if that matters, but I thought I should throw it out).
>
> Basically, in my script I'm trying to do the following:
>
> somefunc () {
> exec 5>/tmp/out.goes.here
> datestamp=$(date +%Y%m%d)
> # there is a variable called prod that changes based upon other
> # criteria in the script, outside this function
>
> # many other print lines, but this is the one giving grief
> print -u5 "/path/to/file/to/examine/$var_$datestamp"
>
> unset datestamp
> exec 5>&-
> }
>
> Basically, this function is dynamically building a configuration to be
> used by another program. Now, when the above function is executed,
> only the datestamp variable contents are output. This is because ksh
> believes I'm referencing a variable by the name var_. Rather, I want
> an underscore between the contents of the two variables.
If var is a variable name, then you have to use {} to bracket the
name, since '_' is a valid character in a variable name. You have to
tell the shell not to consider that part of the variable name:
print -u5 "/path/to/file/to/examine/${var}_$datestamp"
Joe
--
The Dutchman still wears wooden shoes, his cap and coat are patched
with the love that Magaret sewed there...
- Steve Goodman
| |
| William Park 2004-03-25, 2:35 pm |
| Andrew Falanga <falandrew@hp.com> wrote:
> # many other print lines, but this is the one giving grief
> print -u5 "/path/to/file/to/examine/$var_$datestamp"
${var}_${datestamp}
--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data processing and document management.
| |
| Andrew Falanga 2004-03-25, 3:35 pm |
| William Park wrote:
> Andrew Falanga <falandrew@hp.com> wrote:
>
>
>
> ${var}_${datestamp}
>
Thanks both to Joe and William. I knew it was simple, but it was
kicking my butt.
--
---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.
| |
| Mark Daniels 2004-03-25, 3:35 pm |
| ....
> Basically, this function is dynamically building a configuration to be
> used by another program. Now, when the above function is executed, only
> the datestamp variable contents are output. This is because ksh
> believes I'm referencing a variable by the name var_. Rather, I want an
> underscore between the contents of the two variables.
>
> I'm tried a few different things. If I precede the '_' with a '', the
> '' character is output as well. Not desireable. What options do I
> have available to fix this?
>
> Thanks,
>
Which two variables do you want separated by an underscore? The data
variable is the only one defined in your function.
Mark
| |
| Gerhard Sprenger 2004-03-25, 5:41 pm |
| Andrew Falanga <falandrew@hp.com> wrote in message news:<406301c0$1@usenet01.boi.hp.com>...
> Ok, as stated in the subject I'm working KSH. It's ksh '88 (don't know
> if that matters, but I thought I should throw it out).
>
> Basically, in my script I'm trying to do the following:
>
> somefunc () {
> exec 5>/tmp/out.goes.here
> datestamp=$(date +%Y%m%d)
> # there is a variable called prod that changes based upon other
> # criteria in the script, outside this function
>
> # many other print lines, but this is the one giving grief
> print -u5 "/path/to/file/to/examine/$var_$datestamp"
>
> unset datestamp
> exec 5>&-
> }
>
> Basically, this function is dynamically building a configuration to be
> used by another program. Now, when the above function is executed, only
> the datestamp variable contents are output. This is because ksh
> believes I'm referencing a variable by the name var_. Rather, I want an
> underscore between the contents of the two variables.
>
> I'm tried a few different things. If I precede the '_' with a '', the
> '' character is output as well. Not desireable. What options do I
> have available to fix this?
>
> Thanks,
>
> ---------------------------------------------
> Andrew R. Falanga (a non-HP employee)
> Hewlett-Packard Company
> 11311 Chinden Blvd.
> Boise, Idaho
> ---------------------------------------------
> Please note: The e-mail address is purposely
> mangled. I do not wish my account at HP to
> become a spam haven.
Try to replace
print -u5 "/path/to/file/to/examine/$var_$datestamp"
with
print -u5 "/path/to/file/to/examine/${var}_$datestamp"
or
print -u5 "/path/to/file/to/examine/${var}_${datestamp}"
so that the '_' is taken literally and not interpreted as part of
a variable name.
Greetings - Gerhard.
|
|
|
|
|