0

Assuming that the folders exist in the script below, can someone tell my why this is not working? I escape the spaces extra for the test to work but somehow it does not like it with no error...

#!/bin/bash
Base='/tmp/'
Sub='one space/another space/'
declare -a ASub
for argR in "${Sub[@]}"
    do
        Sub+=($(printf %q "$argR"))
    done
clear
echo -n $Base
if [ -d  $ExBase ]
    then
        echo "...OK"
    else
        echo "...FAIL"
fi
BaseAndSub=$Base$Sub
echo -n $BaseAndSub
if [ -d  "$BaseAndSub" ]
    then
        echo "...OK"
    else
        echo "...FAIL"
fi
exit 0
2
  • 1
    what do you want to do with this script?
    – Zarick Lau
    Oct 4, 2011 at 15:26
  • You declare ASub but never use it. It is not clear where you define/initialize the Sub array in for argR in "${Sub[@]}". Classically at least, you would need to use BaseAndSub="$Base/$Sub"; I wouldn't write it otherwise, though it may not be 100% necessary (but it is absolutely clear and foolproof). If the name had multiple adjacent spaces, echo -n $BaseAndSub loses track of them, where echo -n "$BaseAndSub" does not. Use double quotes a bit more liberally. Oct 4, 2011 at 15:32

2 Answers 2

2

Lots wrong with that script. ASub is declared and never used. ExBase is never declared but used.

I guess this is the main problem: if [ -d $ExBase ]
Since ExBase is empty and unset, the shell sees this: if [ -d ]
I would have expected a syntax error but apparently the shell sees "-d" as just a non-empty string, and is therefore true.

1
  • I use set -o nounset; set -o errexit at the beginning of all my Bash scripts which will cause Bash to stop at errors like this instead of letting them pass silently. Oct 4, 2011 at 16:27
0

As @glenn said, there's lots wrong here; ASub is declared but not used, Sub is sometimes used as a string variable sometimes as an array, ExBase is used without being set, ...

The fundamental problem, though, is that you're going through a lot of unnecessary (and sometimes destructive) work in an attempt to handle spaces in the filename, when all that's necessary is to use double-quotes around everything that might contain spaces. Arrays are great for storing lists of filenames (each of which might have spaces) or command strings (that might have spaces in their arguments), but in this case you have a single filename so that's not needed. Adding quotes (printf %q) is almost never useful, it just means you'll be looking for files with actual quotes/escapes/whatever in the names.

Here's my rewrite with the irrelevant stuff stripped out, and double-quotes added in a couple of places. I also changed to the more standard convention of not including trailing slashes in the filenames, so putting Base and Sub together is "$Base/$Sub" not just "$Base$Sub". It seems to work fine for me:

#!/bin/bash
Base='/tmp'
Sub='one space/another space'
clear
echo -n "$Base"
if [ -d  "$Base" ]
    then
        echo "...OK"
    else
        echo "...FAIL"
fi
BaseAndSub="$Base/$Sub"
echo -n "$BaseAndSub"
if [ -d  "$BaseAndSub" ]
    then
        echo "...OK"
    else
        echo "...FAIL"
fi
exit 0

BTW, when trying to troubleshoot bash scripts, it's very helpful to use the -x option (either with set -x, or use #!/bin/bash -x as your shebang). This makes bash print each command before executing it -- with parameters expanded to show exactly what's happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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