[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

How to concantenate variables to create a file name?

I want to create a file name in a script by concantenating variables.
      EXAMPLE:   myfile062701.txt
 
The "myfile" and the ".txt" are the same every time on the file received.  The six digits represent the current date.  When I create one variable

THEDATE=" date "+%m%d%y""
echo $THEDATE
062701

When I try to concantenate the date to the rest of the name, I get the literals, not the value.  (i.e. date +%m%d%y)
0
clreising
Asked:
clreising
1 Solution
 
ggs54Commented:
this works on my HP-UX system.


echo myfile`date +%m%d%y`".txt"

yields:

myfile062801.txt

be sure to delimit your date function with backquotes.

Good luck!
0
 
ggs54Commented:
this works on my HP-UX system.


echo myfile`date +%m%d%y`".txt"

yields:

myfile062801.txt

be sure to delimit your date function with backquotes.

Good luck!
0
 
griesshCommented:
>> THEDATE=" date "+%m%d%y""
>> echo $THEDATE
>> 062701

That doesn't sound true. You have to evaluate THEDATE to get the date.

THEDATE=" date "+%m%d%y""
echo
date +%m%d%y
echo `$THEDATE`
062801

Like ggs54 said already (just wanted to point out the difference).

======
Werner
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
interiotCommented:
I assume you're using Bash...

Quotes take the string in literally.  To actually execute date, use backticks:

   THEDATE=`date "+%m%d%y"`
   echo myfile${THEDATE}.txt

this works also:

   CATTED="myfile$(date "+%m%d%y").txt"
   echo $CATTED
0
 
chris_calabreseCommented:
OK, let's get really picky... the `` syntax is depricated in POSIX, and you have to be careful of quoting, so the only "correct" answer so far is the CATTED= line above.
0
 
clreisingAuthor Commented:
I am on AIX using ksh.
This is what worked.
Thanks for the input.

THEDATE=`date "+%m%d%y"`
echo $THEDATE
FACILITY="myfile${THEDATE}.txt"
echo $FACILITY    
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now