Solved

How to concantenate variables to create a file name?

Posted on 2001-06-28
6
281 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now