Register
It is currently Mon Dec 22, 2014 2:09 pm

TILDE EXPANSION WANTED


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Oct 07, 2009 5:03 am   

Joined: Tue Dec 02, 2008 8:30 am
Posts: 6
Location: Bonny Scotland
I have a script as follows which as part of its function tries the following

MYFILE="~/myprogram/datafile"
if ! [ -e "$MYFILE" ]; then
echo file $MYFILE not there
fi
unfortunately, the ~ is not expanded to /home/username
and so the file is never found.
the MYFILE variable value may have spaces in it. eg "~/my nasty path/yuck data"

Any clues on a generic way to expand the path in the variable would be appreciated. thanks
Tim


Top
 Profile  
 PostPosted: Wed Oct 07, 2009 5:47 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
Code:
myFile=~/"myprogram/datafile"
if ! [ -e "$myFile" ]
then
   echo "file $myFile not there"
else
   echo "$(basename "$myFile") is at $(dirname "$myFile")"
fi


Top
 Profile  
 PostPosted: Wed Oct 07, 2009 7:23 am   

Joined: Tue Dec 02, 2008 8:30 am
Posts: 6
Location: Bonny Scotland
thanks for such a quick response watael,

and to make that slightly more generic
Code:
myfile="~/myprogram/datafile"
if [ `echo $myfile|cut -b-2` = "~/" ]; then
  myfile=~/`echo $myfile|cut -b3-`
fi

should work when initial value of myfile is not known

eg:
Code:
read myfile
if [ `echo $myfile|cut -b-2` = "~/" ]; then
  myfile=~/`echo $myfile|cut -b3-`
fi
echo myfile is $myfile


Top
 Profile  
 PostPosted: Wed Oct 07, 2009 10:10 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
Code:
cut -b-2 <<<"$myfile"
looks nicer to me, and uses one command less


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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