A question about if statement

Hi everybody,

I'm sorry If I ask a silly question. I have a simple code like this

I have the following error:

Can anyone explain for me why I have this error, and how can I correct it?
Thanks in advance.

First thing, indent your code properly.

#!/bin/bash

pattern()
{
    if [ $1 -eq 100 ]
    then
        if [ $2 -eq 100 ]
        then
            echo debug 1
        else
            echo debug 2
        fi
    elif [ $1 -eq 50 ]
    then
        echo debug 3
    else
        echo debug 4
    fi
}

pattern 2 3

And if you want to use "then" in the same line as "if" then I think you should put a semi-colon after the condition, something like this: "if [ condition ]; then"

I'm sorry. I tried to indent, but it seems that the space is automatically deleted. I will tried with your advice.
Thanks a lot.

---------- Post updated at 11:11 PM ---------- Previous update was at 11:03 PM ----------

I have another problem, If my code is like below:

verify()
{// do something
return $(grep -q 'VERIFICATION SUCCESSFUL' result.txt)
}

if [ verify 'a Condition' ]; thenecho debug
fi

Then I have the following error:

 line 37: [: verify: unary operator expected

Would you please explain for me?

Try something like this:

verify()
{
    // do something
    grep -q 'VERIFICATION SUCCESSFUL' result.txt
    if [ $? -eq 0 ]; then
        echo debug
    fi
}

Sorry, but what is $?.
I simplified the code, actually I need to use the function verify at many condition, I cannot do anything with the if statement inside the function.

Return is exit from function. Not for return values. Stdout is method to return values.
$? = last command exit status, 0=ok, <>0 not so ok.

pattern()
{
   val1=$1
   val2=$2
   (( val1 == 100 && val2 == 100)) && return 1
   (( val1 == 100 )) && return 2
   (( val1 == 50  )) && return 3
   return 4
}

some()
{
   input="$1"
   infile="$2"
   data=$(grep "$input"  $infile 2>/dev/null )
   stat=$?
   # if stat = 0, grep found it
   # data is empty, nothing founded or if founded, data include it
   [ "$data" = "" ] && echo "0"  && return 1
   # or
   ((stat !=0 )) &&  echo "0"  && return 1
   #
   #return passwd line
   echo "$data"
}

##############
result=$(some  "xyz"  /etc/passwd)
stat=$?
echo "stat:$stat"   # 1 if xyz not in passwd
echo "result:$result"  # 0 or passwd line
result=$(some  "root"  /etc/passwd)
stat=$?
echo "stat:$stat"   # 1 if root not in passwd
echo "result:$result"  # 0 or passwd line

pattern 100 100
echo $?
pattern 50 1
echo $?
1 Like

$? is the exit status of the last command executed. Check the below post:

You may try something like this:

#! /bin/bash

verify()
{
    # do something
    grep -q 'VERIFICATION SUCCESSFUL' result.txt
    verify_exit_status=$?
}

verify

if [ $verify_exit_status -eq 0 ]; then
    echo debug
fi

By the way, what did you mean by this:

1 Like

Thank you very much for your replies.
What I want to do is: I will run a program and write the result of its execution to the file "result.txt". Then I grep in the output file, if it contains the phrase "VERIFICATION SUCCESSFUL", it means the verification process is successful.

Then I need to run a complicated multi-level if statement whose conditions are the verification result. Something like this:

pattern()
{     if [ verify 'condition1' ] then
         if [ verify 'condition2' ] then
             echo debug 1
         else
             echo debug 2
         fi
     elif [ verify 'condition3' ] then
         echo debug 3
     else
         echo debug 4
     fi
}

I can test the exist of the phrase by this way:

 if grep -q 'VERIFICATION SUCCESSFUL' result.txt

But how can I make a wrapper of it, to use the verify function as above?

You're rather ambiguous about this verify() function. It takes an argument? I don't see where...

'if' takes a command as an argument, [ is a command, grep is also a command. A function by default returns the exit code of it's last command...

verify() {
    local condition=$1

    # do something with $condition
    grep -q 'SUCCESS' results.txt
}

if verify 'condition1'; then
   whatever
elif verify 'condition2'; then
   something else...
else
   i don't know ...
fi
1 Like

Yes, it worked for me. Don't know how to thank you enough.
I didn't know that is also a command, i thought that it just like "if (condition)" in C.
Just one last question, if [ is a command, what is its meaning in this statement if [ $i -gt $j ]

[ is a command, but in bash it's a shell builtin.

$ ls -l /usr/bin/[
-rwxr-xr-x 1 root root 28300 Apr 28  2010 /usr/bin/[

test $i greater than $j. So that exit is 0 (success) if $i > $j, and non-zero otherwise.

Since you're using bash, it's worth noting that [[ is a better and preferred test. Quoting isn't needed, and it offers more (regex matching)..

# using "[":
if [ "$i" -gt "$j" ]; then
# using bash's "[["
if [[ $i -gt $j ]]; then
# using arithmetic
if ((i > j)); then
1 Like

I understand. Thanks again.

After if it's always command in shells: [, [[, (( are only some commands.

if commandline 
then # exit status 0
   :
else # exit status != 0
  :
fi

[ and test are same command, usually builtin command today. If you use command [, then the last argument must be ] = line looks like as in some programming languages.

grep "some" file >/dev/null 2>&1
stat=$?
if [ $stat = 0 ] ; then
     echo founded
else
    echo not
fi

Same result [ and test are same command

grep "some" file >/dev/null 2>&1
stat=$?
if  test $stat = 0 ; then
     echo founded
else
    echo not
fi

Same result, grep output is not needed => output to the /dev/null

if grep "some" file >/dev/null 2>&1 ; then
     echo founded
else
    echo not
fi

Same result

grep "some" file >/dev/null 2>&1  && echo founded || echo not
1 Like