Register
It is currently Tue Sep 02, 2014 12:52 am

How to set path to new directory as variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu May 05, 2011 12:00 am   

Joined: Wed May 04, 2011 11:44 pm
Posts: 1
Trying to learn to use bash instead of applescript.

I am writing a script to copy files to new directories based upon their file type. everything works if I set the destination to an existing known directory on my desktop. However, this will be used by multiple users on various machines and I need to create a new directory at the current users desktop then use the path to the new directory for the destination folder.

Any assistance greatly appreciated.

Rick Davis


Top
 Profile  
 PostPosted: Thu May 05, 2011 8:09 am   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
Try using something like that:
Code:
SOURCE="$HOME/dir0"
DESTIN="$HOME/dir1/dir2"

[ -d "$DESTIN" ] || { mkdir -p "$DESTIN"; }

for fname $(find "$SOURCE/" -type f -name "*php"); do
   cp -f "$fname" "$DESTIN/"
done


Top
 Profile  
 PostPosted: Thu May 05, 2011 2:38 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
oh, no! don't ever use this form :
Code:
for i in $(command); do [...]; done


you'd rather use
Code:
find [...] -exec [...]
or globs
Code:
for file in $source/*php; do cp "$file" "$destination"; done


Top
 Profile  
 PostPosted: Fri May 06, 2011 2:30 am   

Joined: Mon Nov 08, 2010 10:39 am
Posts: 17
I suppose you meant of how the for loop parses the files with the whitespaces. It does not parse them correctly. Another approach is to use the while loop instead.

Code:
find "$SOURCE/" -type f -name "*php" | while read fname; do
      cp -f "$fname" "$DESTIN/"
done


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 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:  
cron


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