1

I'm trying to find out whether there is a Screen running or not with a function. To make it more simple ive written a little test script:

#! /bin/bash

function status()
    {
        if [ $a==1 ] 
        then
            echo 1
        else
            echo 0
        fi
    }

a=1
echo $(status)
a=0
echo $(status)
status
if [ $(status)==0 ]
then
    echo "Success"
else
    echo "Fail"
fi

The Output is: 1;1;Sucess

But it should be: 1;0;Sucess

What am I doing wrong?

thanks alot, chrys

2 Answers 2

1

You need spaces around ==

so: if [ $a==1 ] should be:

if [ "$a" == 1 ] 

and

if [ $(status) == 0 ]
Sign up to request clarification or add additional context in comments.

7 Comments

Also, == is not defined. It may work in bash, but correctly it's only a single equals sign =
@JoSo The question is tagged bash and the code in question begins with #!/bin/bash. I'm not sure if the comment was warranted.
@devnull: From bash manpage: "= should be used with the test command for POSIX conformance."
@JoSo Hmm.. I assume that you always start bash by saying bash --posix.
@JoSo So might make more sense to follow the sh tag instead of bash. No?
|
0

Try this:

Remove spaces around == , included quotes and removed a call for status

#! /bin/bash

function status()
    {
        if [ $a == '1' ]
        then
            echo 1
        else
            echo 0
        fi
    }

a=1
echo $(status)

a=0
echo $(status)

if [ $(status) == '0' ]
then
    echo "Success"
else
    echo "Fail"
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.