If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > variable not inserted as filename

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-15-09, 05:55
freeBatjko freeBatjko is offline
Registered User
 
Join Date: Mar 2008
Posts: 89
Question variable not inserted as filename

Hi folks.

I ran into a problem with trying to create a filename in a ksh script (on SunOS 5.9), which has a variable at the beginning.

Like this:
Code:
myvar=Hoppla
cp original_file.log $myvar_copy.log
The resulting filename is then:
Quote:
_copy.log
However, this works:
Code:
myvar=Hoppla
cp original_file.log copy_$myvar.log
Resulting filename now:
Quote:
copy_Hoppla.log
I noticed the same issue also when redirecting output to a file this way:
Code:
myvar=Hoppla
echo "Test" > $myvar_testfile.log
Result:
Quote:
_testfile.log
What's the deal with the position of the parameter?
__________________
"My brain is just no good at being a relational Database - my relations suck real bad!"
Reply With Quote
  #2 (permalink)  
Old 01-15-09, 07:26
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
myvar=Hoppla
cp original_file.log $myvar_copy.log
The shell is looking for a variable called myvar_copy and can't find it so you're getting wierd results. I suggest you use {} around your variables to make things clearer ie
Code:
myvar=Hoppla
cp original_file.log ${myvar}_copy.log
Reply With Quote
  #3 (permalink)  
Old 01-15-09, 07:49
freeBatjko freeBatjko is offline
Registered User
 
Join Date: Mar 2008
Posts: 89
Ah, of course.... That's just me being a noob, like always.
Thanks for feeding the noob, mike.
__________________
"My brain is just no good at being a relational Database - my relations suck real bad!"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On