K
K
ksvdon2014-09-22 19:28:24
bash
ksvdon, 2014-09-22 19:28:24

Is the following entry valid in bash?

if [ "$p" = 0 ] && [ "$n" = 0 ]; then
    return 1
else
    return 0
fi

I ran the script via bash -x ./script and noticed that only the first condition is checked. It looks something like this:
+ '[' 0 = 0 ']'
Let's say my $p variable is equal to "0", and the variable $n is equal to "1". The first check satisfies the condition, but the second does not. But there is no second check at all in the "bash -x" output. Perhaps this is not possible? (the script issues "return 1")...
In short. I don't know what the hell, but I put in an entry like
if [ $p -eq 0 -a $n -eq 0 ]; then
echo "success"
else
echo "fail"
fi
and it seems to have failed. Issued fail at last. If you do the same, just write -ne and replace success and fail with places - it doesn't work...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor, 2014-09-22
@ksvdon

I would do like this:
if [ "$p"= 0 -a "$n"= 0 ]

V
Vladimir, 2014-09-22
@rostel

if ; then

S
Sergey Petrikov, 2014-09-22
@RicoX

The condition you describe is correct, see if there is an error in data types, for example, text messages come in, try to describe it like this:
if the data is int in both cases, then remove the quote from the variable and use eq
if && ; then

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question