Operating System - HP-UX
1827286 Members
1755 Online
109717 Solutions
New Discussion

Re: Variables, using $ and ${ }_"other words"

 
SOLVED
Go to solution
Manuales
Super Advisor

Variables, using $ and ${ }_"other words"

Hi ..
I have a value kept in $file_galaxy variable but i need to use same variable with date format as follows: $file_galaxy_`date +"%Y%m%d"

but it does not work !!!!
i have tested:
$file_galaxy_`date +"%Y%m%d"
$(file_galaxy)_`date +"%Y%m%d"
${file_galaxy}_`date +"%Y%m%d"
`file_galaxy`_`date +"%Y%m%d"

and any of above shown work !!!!

please help 1!!
I'm using sh and ksh ...

I only obtain: _20060612 or 20060612
thanks ....
12 REPLIES 12
Pete Randall
Outstanding Contributor
Solution

Re: Variables, using $ and ${ }_"other words"

It looks like you've got your back ticks reversed to forward ticks. Try
$file_galaxy_`date +"%Y%m%d`


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Variables, using $ and ${ }_"other words"

While I was waiting for the submit it occured to me that the "_" character is probably not part of the original variable file_galaxy. Try

${file_galaxy}_`date +"%Y%m%d`


Pete

Pete
Manuales
Super Advisor

Re: Variables, using $ and ${ }_"other words"



sorry i forget to put "`" at the end of the variables:

$file_galaxy_`date +"%Y%m%d"`
$(file_galaxy)_`date +"%Y%m%d"`
${file_galaxy}_`date +"%Y%m%d"`
`file_galaxy`_`date +"%Y%m%d"`

but any way they do not work ..!!!! :'(

help !!!
SOS !!!!
Sandman!
Honored Contributor

Re: Variables, using $ and ${ }_"other words"

the syntax should be...

# echo ${file_galaxy}_`date +"%Y%m%d"`
or
# echo ${file_galaxy}_$(date +"%Y%m%d")

in each of your cases, you are missing the variable-delimiting braces, ${file_galaxy}, and the terminating single-tick for command substitution i.e. `date +"%Y%m%d"`

~cheers
Ken Grabowski
Respected Contributor

Re: Variables, using $ and ${ }_"other words"

Not sure why your third example did not work, unless you forgot to close the single quote.

I wrote:
#!/usr/bin/sh

file_galaxy="testfile"

print $file_galaxy

date=`date +"%Y%m%d"`

print $date

print ${file_galaxy}_`date +"%Y%m%d"`

and got:
./junk.sh
testfile
20060612
testfile_20060612

when run.
I used 11.11 for the test.
Gregory Fruth
Esteemed Contributor

Re: Variables, using $ and ${ }_"other words"

Try ${file_galaxy}_`date +"%Y%m%d"`

All of your examples are missing the final
backtick (`). Also,

Example 1 doesn't work because the shell
thinks the second "_" is part of the
variable name.

Example 2 doesn't work because $(...) runs
the ... as a command.

Example 4 doesn't work because `...` runs
the ... as a command.
A. Clay Stephenson
Acclaimed Contributor

Re: Variables, using $ and ${ }_"other words"

Do something like this:

NEWVAR=${file_galaxy}_$(date '+%Y%m%d')

echo "NEWVAR = \"${NEWVAR}\""
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Variables, using $ and ${ }_"other words"

Hi Manuales:

These are ambiguous variable names. Is the underscore part of the name or is it just another part of the overall interpreted string?

Consider the first one: $file_galaxy_`date +"%Y%m%d"

If I add the closing backtick:

# echo $file_galaxy_`date +"%Y%m%d"`
sh: file_galaxy_: Parameter not set.

Thus, the shell "thinks" that the variable to be interpreted is named "file_galaxy_". Is that what you meant, or did you mean "file_galaxy"?

# echo ${file_galaxy}_`date +"%Y%m%d"`
sh: file_galaxy: Parameter not set.

NOTE that the second time I enclosed the variable name in braces -- {}

This informs the shell of the exact variable name without ambiguity.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Variables, using $ and ${ }_"other words"

If you get into the habit of enclosing all shell variables in {}'s even in the cases where they are not absolutely required, you will avoid many problems. I always prefer code (even in my head) that does not branch.
If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: Variables, using $ and ${ }_"other words"

THANKS TO ALL:
It worked with this one:

${file_galaxy}_$(date +"%Y%m%d")


i do not know why this do not work with "`" at the beggining and the end of the variable ..


ANY WAY THANKS TO ALL FRIENDS °!!!!!!
Bill Hassell
Honored Contributor

Re: Variables, using $ and ${ }_"other words"

And if you check the man pages for ksh and the HP POSIX shell sh-posix, you'll see that backticks (reverse apostrophes or grave accents) are deprecated (or not recommended). There several reasons:

1. The back tick ` looks too much like a forward tick ' (apostrophe)

2. Back ticks can easily be missed in printed text, especially on a fax.

3. Back ticks cannot be nested.

The preferred coding is to use $(commands). Thde $() construct can easily be nested, something like this:

# lvdisplay $(bdf /tmp|tail -1| cut -f1 -d\ )|grep "^LV"
LV Name /dev/vg00/lvol4
LV Permission read/write
LV Status available/syncd
LV Size (Mbytes) 80

This command finds the mountpoint source for /tmp using bdf, then extracts it with cut, uses the value as an argument to lvdisplay and grep grabs all the LV lines. Not possible with backticks.


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

Re: Variables, using $ and ${ }_"other words"

Ooops, I showed my interim tests. Here was the desired result:

echo $(lvdisplay $(bdf /tmp|tail -1| cut -f1 -d\ )|grep "^LV Size")|awk '{print $NF}'

which shows the Mbyte size for /tmp. The LV Size line contains the Mbytes number at the end. (somewhat impractical example but shows how $(...$(...)) can be nested.


Bill Hassell, sysadmin