Solved

bash script, find using passed path

Posted on 2013-11-04
4
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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