Use backslash in Applescript running a Bash Shell Script

Discussion in 'Mac Programming' started by rebello95, Nov 3, 2011.

  1. rebello95 macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #1
    I'm trying to do this shell script:

    do shell script ¬
    "find . -type f -name 'AmikiConnect.zip' -exec cp {} ~/Desktop/Test/File/1 \;"

    but I'm getting errors about the end \;" because since there's a backslash in the bash command, it thinks there should be a " after it but there's supposed to be a ; like there is.

    How can I fix this without putting it in a text file? Thanks!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Open the reference doc for do shell script. (Top hit by googling do shell script.)

    Find the word backslash on that web page and read what it says.
     
  3. rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #3
    Yes, I already read that. The problem is when I try to run the program with double backslashes at the end, it just keeps going and doesn't stop. In the end I have to force quit the app. Any suggestions on how to fix this?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post the actual code that doesn't work.

    And please explain what you mean by "it just keeps going and doesn't stop". If you mean "the find continues to find and copy other files", then yes, it's supposed to work that way. Did you read the man page for find to understand how it works?

    If you want to search only a specific directory, then use that directory name in place of "." in the find command. Go back to your other thread and look very carefully at the "cd ~" command you were told to use before running find. Perhaps the "." you're running find from isn't the "." you think it is.

    For testing you should try a simple -print action for find, look at everything it outputs, then decide what to do. If you want advice, you'll have to post that output. We can't read your mind or see your screen.
     
  5. rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #5
    Here's the code:

    do shell script ¬
    "find . -type f -name 'AmikiConnect.zip' -exec cp {} ~/Desktop/Test/ \\;"

    Here's a screen shot showing what I mean by it keeps running... look at the bottom left of the screen shot.
    [​IMG]
     
  6. r0k macrumors 68040

    r0k

    Joined:
    Mar 3, 2008
    Location:
    Detroit
    #6
    Sorry but the screenshot is blocked by our inane firewall, but when you are running a shell script, chances are . is somewhere inside the bundle or perhaps your /Applications folder. Is that what you intend? Also why do you need backslashes? Using a ; in "do shell script" allows you to string several commands together and there isn't any reason I can think of at the moment why you would need need any \.
     
  7. chown33, Nov 3, 2011
    Last edited: Nov 3, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    The -exec action of 'find' needs an actual semicolon arg (read the find man page). One way to do that is \;. Quoting is another way, so ';' also works.


    Which supports what I said about it searching the wrong directory. You left out the "cd ~", which was given to you in your other thread.

    If you don't know what "." represents, i.e. you don't know the current working directory, then use a specific directory name. Blindly assuming that "." will be your home directory is madness. The find command can work on any directory you give it, as long as you have access permission. You can learn this by reading the man page for find.

    If you want find to find only the first instance of the named file, I'm not sure how to do that off the top of my head. Perhaps a predicate like:
    Code:
    -exec killall find \;
    
    That's just a guess. I recommend testing it to see if it works, and that it has no unexpected side effects, such as killing other 'find' processes you may want to keep running. Also, a detailed examination of the find man page may show a predicate that halts the descent.
     
  8. rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #8
    I tried with code

    do shell script ¬
    "find . -type f -name 'AmikiConnect.zip' -exec cp {} ~/Desktop/Test/ \\';'"

    and got the following errors:
    sh: -c: line 0: unexpected EOF while looking for matching `''
    sh: -c: line 1: syntax error: unexpected end of file
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    I said ';'. I didn't say \\';'.


    By the way, this isn't going to affect the "it keeps running" problem at all. It's not going to affect the "working directory" problem, either.

    You already had it working with \\;. It was working. It was doing exactly what it should do. The problem is that what you expected and what you actually told it to do are different. You need to understand what you're actually telling it to do.

    The working directory issue has already been explained. Did you reread your other thread? Do you realize you're still not entering a 'cd ~' command?

    If you don't understand an explanation, then please state that. If you don't understand how to fix it, then please state that. Blindly trying different things, none of which you understand, is not a recipe for success.
     
  10. rebello95, Nov 3, 2011
    Last edited: Nov 3, 2011

    rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #10
    Okay, sorry about that. for some reason I didn't see your post in this thread about cd ~

    I ran this:

    do shell script ¬
    "cd ~
    find . -type f -name 'AmikiConnect.zip' -exec cp {} ~/Desktop/Test/ ';'"

    and it worked perfectly. Thank you so much!

    So basically it was searching the whole HD, right?
     
  11. rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #11
    So thanks again and sorry for my non-understanding a few times there ;)
     
  12. r0k macrumors 68040

    r0k

    Joined:
    Mar 3, 2008
    Location:
    Detroit
    #12
    I found an example over at macscripter.net that seems to confirm the use of \\; for the find -exec command...

    Code:
    do shell script "/usr/bin/find /Volumes/TS-Leenbakker/JPEG/10/101/ -name '*_Landscape*' -type f -exec cp {} '/Path/to/destination/folder' \\;"
    
    Also the web page suggested by chown33 requires less than half an hour reading and explains the whole backslash issue quite well.

    Can you explain more about why you don't appear to care about the path? Is it that you know that . is inside the bundle and the files you are looking for are inside the bundle? It could be that running the script inside the script editor moves . somewhere else and . turns out to be inside the applescript editor's bundle. I have heard there is a way to run your script "as an app" and force it to run outside the script editor environment and have its own path, bundle, etc. Perhaps that is what you need to do.

    Oops. Nevermind. I was typing slowly while you were typing...
    No it was probably searching inside applescript editor's bundle.

    Nothing to be sorry about. It's not a crime to ask a question. It's sometimes a crime NOT to ask a question like "Are the seawalls near my reactor high enough?"
     
  13. rebello95 thread starter macrumors member

    Joined:
    Jun 15, 2011
    Location:
    USA
    #13
    The reason I don't care about adding a path is because I don't know the exact path. The files I'm looking for are in the bundle. The bundle will be downloaded originally as a .zip then extracted to wherever the user wants. Because I don't know that they'll put it in a definite directory, I'm having it simply find the file.

    And I have the script saved as a read/run-only application that runs on its own (it is a .app), and the files I'm looking for are in the bundle, like I said before.

    ----------

    Lol :)
     

Share This Page