Solved

How to concantenate variables to create a file name?

Posted on 2001-06-28
6
284 Views
Last Modified: 2013-12-06
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
Comment
Question by:clreising
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:ggs54
ID: 6235677
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
 
LVL 1

Expert Comment

by:ggs54
ID: 6235679
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
 
LVL 11

Expert Comment

by:griessh
ID: 6235731
>> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
interiot earned 100 total points
ID: 6235890
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6235944
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
 

Author Comment

by:clreising
ID: 6238798
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question