single quote escaping in applescript/shell

后端 未结 3 879
无人共我
无人共我 2021-02-10 08:35

I\'m trying to process this shell script in applescript, but it keeps saying error EOF, because of the single quote in the folder name.

do shell script \"cp -R \         


        
相关标签:
3条回答
  • 2021-02-10 09:03

    The problem arises because you have that tick in the filename of course, on the terminal commandline you would use a tick - double-tick tick - double-tick tick sequence to present it.

    (From the terminal below)

    729$ echo 'My '"'"'Java Folder'
    My 'Java Folder
    

    In Applescript, and your command line, it becomes even more complicated, I recommend you echo your commandline, until you get back what you expect.

    set res to (do shell script "echo 'My '\"'\"'Java Folder'")
    log res
    --> (*My 'Java Folder*)
    

    I think you'll have to start out with that, and reconstruct how you escape the rest of the commandline around it, if it can't be just plugged in as it is.

    You should also remove single quotes for entities, that doesn't need them, (no spaces). That way your commandline will become easier to both edit and read.

    0 讨论(0)
  • 2021-02-10 09:03

    Adding an answer for JavaScript for AppleScript (JAX) users based on answer from McUsr:

    debugger
    
    var app = Application.currentApplication();
    app.includeStandardAdditions = true;
    
    var source = "/documents/John's Spreadsheet.xls";
    var target = "/documents/John's Spreadsheet.csv";
    
    source = source.replace("'", "'\"'\"'", "g");
    target = target.replace("'", "'\"'\"'", "g");
    
    var exportScript = "/excel -i";
    exportScript += " '" + source + "'";
    exportScript += " '" + target + "'";
    exportScript += ";true";
    
    try {
        app.doShellScript(exportScript);
    }
    catch (error) {
        console.log(error.message);
    }
    

    If you don't know what JAX is it's AppleScript but using JavaScript. Open Script Editor and select JavaScript from the dropdown beneath the record button.

    0 讨论(0)
  • 2021-02-10 09:16

    Always use quoted form of when using arbitrary data as an argument to an command. This will always quote the value even if it doesn't need to be quoted but it's better safe than sorry. When you have a string single quoted, you can only unquote (turn substitution mode back on) with another quote. Unlike AppleScript strings, you can't escape characters inside single quoted strings. So you need to turn substitution mode on, escape a quote and then turn substitution mode back one. For instance "Joe's car" should be quoted as "'Joe'\\''s car'". It's a quoted string "Joe" + escaped quote character + quoted string "s car". But like I started you should use quoted form of, quoted form of "Joe's car" will return "'Joe'\\''s car'"

    Your command using quoted form will look like:

    do shell script "cp -R " & quoted form of a & space & quoted form of (d & "/My 'Folder/java/")
    
    0 讨论(0)
提交回复
热议问题