Solved

bash script, find using passed path

Posted on 2013-11-04
4
290 Views
Last Modified: 2013-11-09
There's probably a super exotic way to do this, and I AM interested!
If someone could show me how to lessen the 'if/then', I am interested in that too.

What I can't get working is the 'find'.  When tracing, the command looks perfect!


TreeTop=$1                     # TreeTop is the path to start searching
maybe_shorter=${TreeTop#* }    # Get the first part if there's a space
if [ ${#TreeTop} != ${#maybe_shorter} ]
then
  TreeTop=`echo "$TreeTop" | sed -e 's/ /\\\ /g'`   # Escape spaces
fi
...
find "$TreeTop"  -type f | paste -s -d '\n' - | grep -v ".DS_Store" > killme10.txt

I can't get the following to work!

bash -x ./FindDuplsInOneTree.sh   "~/Music/iTunes/iTunes Media/Music"
This yields

+ TreeTop='~/Music/iTunes/iTunes Media/Music'
+ maybe_shorter=Media/Music
+ '[' 33 '!=' 11 ']'
++ echo '"~/Music/iTunes/iTunes Media/Music"'
++ sed -e 's/ /\\ /g'
+ TreeTop='"~/Music/iTunes/iTunes\ Media/Music"'
...
+ cat /dev/null
+ find '"~/Music/iTunes/iTunes\ Media/Music"' -type f
+ paste -s -d '\n' -
+ grep -v .DS_Store
find: "~/Music/iTunes/iTunes\ Media/Music": No such file or directory

Any advice much appreciated.  I'm trying to fix a merged library.
Thanks!
eTop=$1                     # TreeTop is the path to start searching
maybe_shorter=${TreeTop#* }    # Get the first part if there's a space
if [ ${#TreeTop} != ${#maybe_shorter} ]
then
  TreeTop=`echo "$TreeTop" | sed -e 's/ /\\\ /g'`   # Escape spaces
fi
...
find 

Open in new window

0
Comment
Question by:Ralph
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39621234
You don't need the if, since sed will make no substitutions when $TreeTop has no spaces,
but you don't need the sed, since you are quoting "$TreeTop"  when you do the find.
In fact, since you are quoting it, you don't want to do the sed.
0
 

Author Comment

by:Ralph
ID: 39621568
I tried just quotes, and since the ~ is there, it won't work as
"~/blah/blah blah/blah"
nor as
~"/blah/blah blah/blah"

Also, I'm not sure about a leading or trailing space if the user has it in his quotes.
Maybe I don't need to be that idiot proof.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39623076
try
find "${1/#~/$HOME}"  -type f
or
find "`eval echo $1`"  -type f
with no sed
or, try calling the script with
bash -x ./FindDuplsInOneTree.sh   ~/"Music/iTunes/iTunes Media/Music"
0
 

Author Closing Comment

by:Ralph
ID: 39636597
All of the advice given was good, (even when you think you have things covered).

MORE importantly for me though, is the "Oh yeah" new find search path handler,
and more? fun, the challenge of figuring out a tricky regular expression search path handling.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question