RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    Escaping commands in script modules

    Scheduled Pinned Locked Moved Help and Support
    escapecommandscript
    5 Posts 2 Posters 496 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sleve_mcdichael
      last edited by sleve_mcdichael

      I am having trouble getting a couple of commands to script properly.

      First, to build the project from terminal, I need to type exactly:

      CFLAGS='-DDATADIR="\"home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/data/\""' CXXFLAGS='-DDATADIR="\"/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/data/\""' cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=On ..
      

      Substituting vars $romdir and $md_id, that looks like:

      CFLAGS='-DDATADIR="\"$romdir/ports/CaveStory/$md_id/data/\""' CXXFLAGS='-DDATADIR="\"$romdir/ports/CaveStory/$md_id/data/\""' cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=On ..
      

      ...but when I put that in my build function, something gets lost. When I run the binary, it reports: Error opening font file omdir/ports/CaveStory/d_id/data/font_1.fnt

      It seems rather than being expanded properly, the vars are just omitting the first char after the $ sign (and the sign itself) and passing as omdir and d_id. How do I properly escape this command so that the vars will expand correctly? I tried just adding another \ before the $ but that didn't work either. Error was the same ("omdir/ports/CaveStory/d_id/data/").

      Second, I need to move the contents of a directory one level up, and delete the empty dir.

      I tried:

      mv "$romdir/ports/CaveStory/$md_id/CaveStory/*" "$romdir/ports/CaveStory/$md_id"
      

      ...but the log shows: mv: cannot stat '/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/CaveStory/*': No such file or directory

      ...and then of course rmdir failed, because the dir was not empty. I'm guessing it's because the * is inside the quotes because it doesn't work from command-line either:

      mv "/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/CaveStory/*" "/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo"
      mv: cannot stat '/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/CaveStory/*': No such file or directory
      

      If I move the star outside the quotes, it works:

      mv "/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo/CaveStory/"* "/home/pi/RetroPie/roms/ports/CaveStory/nxengine-evo"
      pi@retropie: ~/RetroPie/roms/ports/CaveStory/nxengine-evo $ 
      

      Is it "okay" to do this? I haven't tried it in the script yet, will it work there? Is there a better/other way?

      1 Reply Last reply Reply Quote 0
      • mituM
        mitu Global Moderator
        last edited by

        You need to learn how Bash expands variables. Single quotes (') prevent variable expansion, while double quotes (") allow it.

        $ var=X
        $ echo '$var'
         $var
        $ echo "$var"
        X
        $ echo '"$var"'
        "$var"
        $ echo "'$var'"
        'X'
        
        S 1 Reply Last reply Reply Quote 0
        • S
          sleve_mcdichael @mitu
          last edited by sleve_mcdichael

          @mitu said in Escaping commands in script modules:

          $ echo "'$var'"
          'X'
          

          So there's single quotes there, but they're inside of double ones. So it only depends on the outermost ones, or...? I tried swapping them in the command which didn't work, nor did using double-quotes exclusively (escaping the existing un-escaped doubles that were now inside the outer ones) or wrapping additional doubles around the whole thing, or various pieces of. Got any hints what else I might try?

          Or, I understand if you don't want to be my bash teacher. Know of any resources that might help me understand?

          Edit: ah, will this do it?

          Inside single quotes everything is preserved literally, without exception.

          That means you have to close the quotes, insert something, and then re-enter again.

          'before'"$variable"'after'
          'before'"'"'after'
          'before'\''after'
          

          ...ooh when I do this, it highlights the vars in nano in a way it wasn't doing before. Thats promising?

           CFLAGS='-DDATADIR="\"'"$romdir"'/ports/CaveStory/'"$md_id"'/data/\""' CXXFLAGS='-DDATADIR="\"'"$romdir"'/ports/CaveStory/'"$md_id"'/data/\""' cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=On ..
          

          Edit again: yeah that seems to have done it, I guess I just needed to keep searching...

          1 Reply Last reply Reply Quote 0
          • S
            sleve_mcdichael
            last edited by

            mv "$romdir/ports/CaveStory/$md_id/CaveStory/"* (...)
            

            Is this right, though? Just close the quotes and leave a naked star outside them like that? It feels kind of...dirty to do it this way. Am I wrong? (It does work, though...)

            mituM 1 Reply Last reply Reply Quote 0
            • mituM
              mitu Global Moderator @sleve_mcdichael
              last edited by mitu

              @sleve_mcdichael said in Escaping commands in script modules:

              Is this right, though?

              It is, because the filename globbing for * doesn't happen inside the quoted string:

              $ ls "~/roms/nes/*"
              ls: cannot access 'roms/nes/*': No such file or directory
              # tries to list a file named '*'
              
              $ ls "~/roms/nes/"*
               roms/nes/240pee.nes                 roms/nes/game1.zip
              ... 
              
              # filename expansion happens normally
              
              1 Reply Last reply Reply Quote 1
              • First post
                Last post

              Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

              Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.