I have an android build i need to customize, the question is ehre and not in stackoverflow because my issue wis with bash. I wrote a script:
#!/bin/bash -xv
params=$@
if [ "$#" -ge "2" ]; then
params=$1
shift
params="$params --custom-package com.some.package"
while [ "$#" -gt "0" ]; do
temp=$1
echo $1 | grep ' ' > /dev/null
if [ "$?" -eq "0" ]; then
temp=\"$1\"
fi
params="$params $temp"
shift
done
fi
path=$(echo $0|xargs dirname)
echo $params
$path/aapt-orig $params
the script suppose to take the parameters it got, add --custom-package com.some.package parameter and keep the rest of the params, the first ($1) param is the action so i need to keep it first.
The problem is with spaces in directory names './my android dir/' for example. if i just pass the parameter through: param ="$param $1"; shit; if i have spaces in the name it doesn't work cause the new param is not escaped(with "), if i use the code above it still doesn't work because for some reason bash ignores the " char and still consider the space in the middle as a separate parameter! here is the output from the run: './aapt action "/Users/bankleumi/some dir/" another_param'
'[' 0 -eq 0 ']'
+ temp='"/Users/bankleumi/some dir/"'
+ params='action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/"'
+ shift
+ '[' 1 -gt 0 ']'
+ temp=another_param
+ echo another_param
+ grep ' '
+ '[' 1 -eq 0 ']'
+ params='action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/" another_param'
+ shift
+ '[' 0 -gt 0 ']'
path=$(echo $0|xargs dirname)
echo $0|xargs dirname
++ echo ./aapt
++ xargs dirname
+ path=.
echo $params
+ echo action --custom-package com.leumi.leumiwallet '"/Users/bankleumi/some' 'dir/"' another_param
action --custom-package com.leumi.leumiwallet "/Users/bankleumi/some dir/" another_param
$path/aapt-orig $params
+ ./aapt-orig action --custom-package com.leumi.leumiwallet '"/Users/bankleumi/some' 'dir/"' another_param
As you can see in the end in the printing of the echo bash consider the string "/Users/bankleumi/some dir" as two strings even though there are \" integrated IN the string! how can i make bash leave the space alone? ( i tried playing with IFS and it didn't work either...)