ENOSIG Discussie (threads)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bash append
Op 14 jan 2013, om 18:12 heeft casper-enosig@xxxxxxxxxxx het volgende geschreven:
> Op 14-01-13 17:59, H schreef:
>> Hi enosiggers,
>>
>> I'm stuck with this simple bash script :
>>
>> cat append.sh
>> #!/bin/bash
>> set -x
>> declare -gx TODO="Todo:"
>> TODO+=' aap.'
>> [ -f /isernie ] || (
>> TODO+=' noot.'
>> )
>> TODO+=' mies.'
>> echo "[$TODO]"
>>
>> Output is :
>>
>> ./append.sh
>> + set -x
>> + declare -gx TODO=Todo:
>> + TODO+=' aap.'
>> + '[' -f /isernie ']'
>> + TODO+=' noot.'
>> + TODO+=' mies.'
>> + echo '[Todo: aap. mies.]'
>> [Todo: aap. mies.]
>>
>> Afaik bash does not have name scoping.
>>
>> What must I do to have 'noot' appended here ?
>>
>> ps. this is on GNU bash, version 4.2.37(1)-release
>
> Your problem is that TODO+=' noot.' is executed inside a subshell, as
> denoted by the parenthesis ( and ) .
>
> My solution:
>
> #!/bin/bash
> set -x
> declare -gx TODO="Todo:"
> TODO+=' aap.'
> TODO+=$( [ -f /isernie ] || echo " noot." )
> TODO+=' mies.'
> echo "[$TODO]"
What's wrong with
if [ ! -f /isernie ]; then
TODO+=' noot'
fi
and avoid the sub-shell problem altogether (test/[ is internal in bash)?
Groetjes,
Edwin
Follow-ups:
Gerelateerd:
[
Date Index]
[
Thread Index]