2

I have written a bash script and I am receiving an error when I am testing a condition whether a variable is empty or not.

Below is an example script:

I have not mentioned the commands that are executed to assign values to variables a and fne but

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if[ -z "$a" ]
    then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

error: syntax error near unexpected token, "then".

I tried another variation like this:

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if[ -z "$a" ]; then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

again same error.

when I try comparing this way:

if[ "$a" == "" ]; then

again same error.

I am not sure what is the reason for the error. The value of variable a is like this:

Something with it (1) : [x, y]

it contains, spaces, brackets, comma, colon. I am enclosing the variable name in double quotes in comparison.

1
  • It's hard to figure why you're using a loop. You never seem to use the variable f. Commented Oct 26, 2013 at 8:14

1 Answer 1

8

You are missing the space after the if:

#! /bin/bash

for f in /path/*
do
    a=`some command output`
    fne=`this command operates on f`
    if [ -z "$a" ]; then
        echo "nothing found"
    else
        echo "$fne" "$a"
    fi
done

Side note: if you were using vi for editing, it would have syntax-colored your typo...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.