Reputation: 2025
I can execute the command below in my terminal succesfully.
command:
gdalwarp -s_srs "+datum=WGS84 +no_defs +geoidgrids=egm96-15.gtx" -t_srs "+datum=WGS84 +no_def" input.tif output.tif
Now, I want to store this command into a variable and expand this command inside a docker container.
My script run.sh
looks like the following. I first store my target command into mycommand
and run the container with the command as input.
mycommand=$@;
docker run -ti --rm osgeo/gdal:ubuntu-small-latest /bin/bash -c "cd $(pwd); ${mycommand}"
And then I execute the run.sh
as following.
bash run.sh gdalwarp -s_srs "+datum=WGS84 +no_defs +geoidgrids=egm96-15.gtx" -t_srs "+datum=WGS84 +no_def" input.tif output.tif
Issue:
bash run.sh
can be store literally into the mycommand
variable.mycommand
can be expand and execute literally. But it looks like that the double quote in my original command will be lost during this process.Thank you.
Upvotes: 0
Views: 3694
Reputation: 140940
You could pass the command as argument and then invoke "$@"
inside the shell. I prefer mostly single quotes.
docker run -ti --rm osgeo/gdal:ubuntu-small-latest \
/bin/bash -c 'cd '"$(pwd)"' && "$@"' -- "$@"
If only you want cd
just let docker change the directory with -w
. In Bash $PWD
will be faster then pwd
command.
docker run ... -w "$PWD" image "$@"
Note that "$(pwd)"
is not properly quoted inside child shell - the result will undergo word splitting and filename expansion. Anyway, I recommend declare -p
and Bash arrays (and declare -f
for functions) to transfer data between Bash-es. declare
will always properly quote all stuff, so that child shell can properly import it.
cmd=("$@")
pwd=$PWD
work() {
cd "$pwd"
"${cmd[@]}"
}
docker ... bash -c "$(declare -p pwd cmd); $(declare -f work); work"
Research: when to use quoting in shell, difference between single and double quotes, word splitting expansion and how to prevent it, https://mywiki.wooledge.org/Quotes , bash arrays, https://mywiki.wooledge.org/BashFAQ/050 .
Upvotes: 3