Solved

How to concantenate variables to create a file name?

Posted on 2001-06-28
6
283 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

786 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