Register
It is currently Thu Nov 27, 2014 1:30 pm

Using a variable in a path?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri May 31, 2013 1:58 am   

Joined: Fri May 31, 2013 1:37 am
Posts: 2
I developed a bash backup script for my home network that works pretty well, and I learned a lot in the process. I'm having trouble using a variable in a path however. I want to prepend the date to an mbr backup. This code works
Code:
ssh ws-mint dd if=/dev/sda of=$MBR_path/sda/"`date +%m-%d-%Y-%R`"_xps-mbr-sda-512.img bs=512 count=1 > /dev/null 2>&1 &&

But I would like to use this:
Code:
# Variables
TODAY="`date +%m-%d-%Y-%R`"
ssh ws-mint dd if=/dev/sda of=$MBR_path/sda/$TODAY_xps-mbr-sda-512.img bs=512 count=1 > /dev/null 2>&1 &&
But this doesn't work. Neither does any combination of /\$TODAY\ or quotes etc. I googled this, but kept getting PATH_ENV and related. I could leave it as is, but I'd like to know how to do this. thx, feffer777


Top
 Profile  
 PostPosted: Fri May 31, 2013 7:16 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Code:
# Variables
TODAY=$(date +%m-%d-%Y-%R)
ssh ws-mint dd if=/dev/sda of=$MBR_path/sda/${TODAY}_xps-mbr-sda-512.img bs=512 count=1 > /dev/null 2>&1


I cleaned the variable assignment up a little and added braces around the TODAY var otherwise bash will see it as $TODAY_xps rather than just $TODAY :)

DW


Top
 Profile  
 PostPosted: Fri May 31, 2013 9:40 am   

Joined: Fri May 31, 2013 1:37 am
Posts: 2
DarthWavy wrote:
Code:
# Variables
TODAY=$(date +%m-%d-%Y-%R)
ssh ws-mint dd if=/dev/sda of=$MBR_path/sda/${TODAY}_xps-mbr-sda-512.img bs=512 count=1 > /dev/null 2>&1


I cleaned the variable assignment up a little and added braces around the TODAY var otherwise bash will see it as $TODAY_xps rather than just $TODAY :)

DW
Just tested in my script--worked great. Guess I did not get the code for the variable right in the first place; mine was too cluttered. thx!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP