Escaping commands in script modules
-
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 asomdir
andd_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?
-
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'
-
@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...
-
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...)
-
@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
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.