Posted on 11-08-2011 11:59 PM
Hi all,
Hoping you can assist with a problem – Not Casper specific though.
Background:
In our NetBoot image I have a script – Made droppable via Platypus.
This is used by my colleagues so aiming to make it fairly user-friendly.
What we do:
Drag a folder onto the app – Gives us for instance:
/Users/user
We then script that the dropped folder gets backed up via ditto to a server folder with todays date.
The problem being - using Ditto it puts the contents of the the user folder into the Todays Date folder.
How can I extract
/Users/david/
To another variable
/david/
So I can create a folder:
/Backups/2011-11-09/david/
We don’t always use it to backup home folders so the input could vary from one folder to five folders deep in the folder structure.
Presume SED can be used to extract the values between / and /
Any advice or suggestions are appreciated.
Thanks, David
Posted on 11-09-2011 04:07 AM
David,
Try this:
#!/bin/bash
path="/Users/drew/this/is/a/test/"
lastDir=echo $path | grep -Eo '[^/]+/?$' | cut -d / -f1
echo $lastDir
Regards,
Drew McMillen, Desktop Engineering Team Lead
NIEHS Tech Services Team
(w) 919/313-7683
(m) 919/257-8333
mcmillen at niehs.nih.gov
https://apps.niehs.nih.gov/itsupport/web
Posted on 11-09-2011 04:09 AM
David,
You have two different examples
/User/user
and
/User/user/
For the former,
echo ${FILE##*/}
will provide the last item after the last slash, so
bash-3.2$ FILE="/I/want/the/last/directory"; echo ${FILE##*/}
directory
whilst
bash-3.2$ FILE="/I/want/the/last/directory/"; echo ${FILE##*/}
will return nothing as there is nothing after the last slash.
Same problem if you use awk
bash-3.2$ echo "/I/want/the/last/directory" | awk -F "/" '{print $NF}'
directory
bash-3.2$ echo "/I/want/the/last/directory/" | awk -F "/" '{print $NF}'
bash-3.2$
You could then get into 'if' statements and removing characters, etc. However, if all of your users are in /Users, then you could just cheat and do
echo /Users/david/ | cut -d "/" -f 3
Then it wont matter.
Sean
Posted on 11-09-2011 04:58 AM
Hi David,
If you only want the last bit of a path, you can also pass the directory variable through the basename command, which will extract the last item, with or without the end /.
e.g.
bash-3.2$ basename /path/to/directory directory
bash-3.2$ basename /path/to/directory/ directory
Regards,
Daniel Sung Junior Systems Administrator Framestore 19-23 Wells Street, London W1T 3PQ www.framestore.com
Posted on 11-09-2011 09:44 PM
Thanks Daniel, Sean and Drew McMillen (offlist)
I now have a working solution thanks to Drew.
Both Daniels and Seans solutions worked perfectly as expected until I attempted to go live. The problem I had:
When I drag a folder into Platypus Droppable it makes the variable as:
/Volumes/Macintosh HD/Users/test
Using basename and Sean's echo both faulted as Platypus doesn't escape the space in Macintosh HD. I tried to see if I could modify Platypus so it provided a complete folder path (/Volumes/Macintosh HD/Users/test) but unfortunately couldn't find a solution.
Drew's provided solution proved successful!
path="/Users/drew/this/is/a/test/"
lastDir=echo $path | grep -Eo '[^/]+/?$' | cut -d / -f1
echo $lastDir
In testing this is working as expected – I have deployed it now successfully.
Thanks to the input of all – Greatly appreciated!
Thanks, David
Posted on 11-10-2011 02:38 AM
David,
You just need to learn how to pass/escape spaces.
mac118:~ macsupport$ echo "/Users hello/david/" | cut -d "/" -f 3
david
mac118:~ macsupport$ basename "/Users hello/david"
david
mac118:~ macsupport$ basename /Users hello/david
david
Once you have a string returned, you either will need to quote it or replace the space with an escaped space,
mac118:~ macsupport$ echo "/Users hello/david/" | sed 's/ /\ /g'
/Users hello/david/
Sean