I've got a bootstrap (bash) that should filter out some arguments before it starts the propper python script.
The problem is that whenever i pass a string with spaces into the bootstrap it gets mangled up once it arrives at python
e.g executing
./myBootStrap.sh --preload "argl bargl" -j -as -argl --postload "my Test"
prints this
Executing myBootStrap --preload "argl bargl" -j -as -argl --postload "my Test"
and my python script prints its argument
got arguments ['myBootStrap','--preload', '"argl', 'bargl"', '-j', '-as', '-argl', '--postload', '"my', 'Test"']
as you see the "argl bargl" and "my Test" get split up into ['"argl','bargl"'] & ['"my', 'Test"'] instead of staying combined.
any idea whats wrong with my code ?
thanks heaps!
myBootStrap.sh
#!/bin/bash
declare -a argv
for ((i=1;i<=${#@};i+=1))
do
arg=${@:i:1}
if [[ "$arg" == "--preload"* ]];then
i=$i+1
marg=${@:$((i)):1}
preLoadO=$arg
preLoadA=" \"${marg}\""
argv=("${argv[@]}" $arg)
argv=("${argv[@]}" $preLoadA)
elif [[ "$arg" == "--postload"* ]];then
i=$i+1
marg=${@:$((i)):1}
postLoadO=$arg
postLoadA=" \"${marg}\""
argv=("${argv[@]}" $arg)
argv=("${argv[@]}" $postLoadA)
else
argv=("${argv[@]}" $arg)
fi
done
arguments=$(printf " %s" "${argv[@]}")
arguments=${arguments:1}
echo "Executing myBootStrap" $arguments
exec myBootStrap $arguments
and the python script myBootStrap
#!/usr/bin/env python
import sys
print 'got arguments %s'%sys.argv
getopt,argparseoroptparse(deprecated!). do yourself a favour and use them, it's a lot easier then parsing aruments in bash usingprintfand manually quoting strings...getoptos.environ.