Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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...)

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Don't mess with re-quoting, it won't work due to the way sh/bash use word-splitting on variables -- that is, they split the value without first considering the quotes in the value.

You could get around that by using eval "$path/aapt-orig $params"...

But Bash has arrays, use them instead:

#!/usr/bin/env bash
args=()
args+=("$1"); shift
args+=(--custom-package com.leumi.leumiwallet)
for arg in "$@"; do
    args+=("$arg")
done
exec "$(dirname "$0")/aapt-orig" "${args[@]}"

Or even simpler:

#!/usr/bin/env bash
exec "${0%/*}/aapt-orig" "$1" --custom-package com.leumi.leumiwallet "${@:2}"
share|improve this answer
2  
Side note: echo $0 | xargs dirname? Really? –  grawity Oct 4 '12 at 13:08
    
For some reason which I am sure @grawity will understand better than I, using double quotes also works: ./aapt action ""/Users/bankleumi/some dir/"" another_param –  terdon Oct 4 '12 at 13:23
    
@terdon: I'm not sure how that would work. After bash's unquoting, ""foo bar"" baz is exactly the same as foo bar baz, and aapt receives it as three single-word arguments. –  grawity Oct 4 '12 at 13:38
    
No idea, @grawity, just tried it on a whim and it worked. I used the script in the OP and just double quoted the CLI argument as shown above. The relevant output was: + echo action --custom-package com.some.package /Users/bankleumi/some dir/ another_param. Bash version: GNU bash, version 4.2.36(1)-release (x86_64-pc-linux-gnu) –  terdon Oct 4 '12 at 13:41
add comment

This should work with more shells, and may be easier to understand:

#!/bin/sh
first="$1"; shift;
exec "`dirname '$0'`/aapt-orig" "$first" --custom-package com.foo.bar "$@";

Note that it works because shells treat "$@" specially, see section "Special Parameters" in the Bash manpage.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.