A question about if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A question about if statement
# 1  
Old 12-04-2011
A question about if statement

Hi everybody,

I'm sorry If I ask a silly question. I have a simple code like this
Quote:
#!/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
I have the following error:
Quote:
./test: line 8: syntax error near unexpected token `else'
./test: line 8: ` else'
Can anyone explain for me why I have this error, and how can I correct it?
Thanks in advance.
# 2  
Old 12-04-2011
First thing, indent your code properly.
Code:
#!/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"
# 3  
Old 12-05-2011
Code tags

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:
Code:
verify()
{
// do something return $(grep -q 'VERIFICATION SUCCESSFUL' result.txt)
} if [ verify 'a Condition' ]; then
echo debug
fi

Then I have the following error:
Code:
 line 37: [: verify: unary operator expected

Would you please explain for me?
# 4  
Old 12-05-2011
Try something like this:
Code:
verify()
{
    // do something
    grep -q 'VERIFICATION SUCCESSFUL' result.txt
    if [ $? -eq 0 ]; then
        echo debug
    fi
}

# 5  
Old 12-05-2011
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.
# 6  
Old 12-05-2011
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.
Code:
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 $?

This User Gave Thanks to kshji For This Post:
# 7  
Old 12-05-2011
$? is the exit status of the last command executed. Check the below post:
https://www.unix.com/unix-dummies-que...it-status.html

You may try something like this:
Code:
#! /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:
Quote:
return $(grep -q 'VERIFICATION SUCCESSFUL' result.txt)
This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Dummies Questions & Answers

Question using 2 variables in a "for" statement

Good Morning, I typically run a batch of commands, from the command line, to process server operating statistics. That would look like this: (these are days of the month) In this instance I am processing a directory of file for July 6th 7th etc. Those files would have names... (9 Replies)
Discussion started by: scotm
9 Replies

3. Programming

question about how the if statement works

on C programming, on an if statement, if u have something like if A && B && C { } if A is false, will it still move on to B and check it? (1 Reply)
Discussion started by: omega666
1 Replies

4. Shell Programming and Scripting

Quick question regarding if statement

Hi, I'm trying to write an if statement that will check the USER parm against some text but I'm not quite sure how to use the or switch in the statement.. Can anyone help me out?... If someone could also let me know when to use ( or if (( $USER != "user1" || "user2" || "user3" || "user4" ))... (6 Replies)
Discussion started by: Jazmania
6 Replies

5. Shell Programming and Scripting

perl : semi complex if statement question

Hi there I am trying to write an if statement in perl that will return "SUCCESS" if either of these conditions are true a) if $changes is greater than 5 AND the $force flag is set to 1 OR b) if $changes is greater than 0 AND $changes is less than 6 #!/usr/bin/perl -w my $force =... (5 Replies)
Discussion started by: rethink
5 Replies

6. Shell Programming and Scripting

ksh "case" statement question

Hi I have the following case statement: case $larg in *_* ) a=${larg%_*}; b=${larg#*_}; ;; *^* ) a=${larg%^*}; b=${larg#*^}; ;; esac I cannot figure out what *_* and *^* stand for... Also what a=${larg%_*}; b=${larg#*_}; and a=${larg%^*}; b=${larg#*^}; ... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

compound Bash if then statement question

I am writing a Bash script that will either clone a database or setup a standby database. So Parameter 2 will be the operation type. If the value is not clone or standby I want to throw an error message. I suppose I can also do a case block. So far i have been unable to get the syntax working... (1 Reply)
Discussion started by: gandolf989
1 Replies

8. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

9. Shell Programming and Scripting

IF statement question

Hi there We have boxes named server-sybase2, server-oracle1, etc etc Does any body know how I can construct an if statement to say, if the hostname of the box contains the string "sybase" then do X ie if then X fi any help would be greatly appreciated (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. Shell Programming and Scripting

Another question on awk - Begin statement

Hi, I've a question on awk. In English I want to: (a) open a file, (b) search through the file for records where length of field15 > 20 characters and (c) print out some fields in the record. I've written the following and it works OK. The trouble is this will ALWAYS write out the column... (5 Replies)
Discussion started by: eff_cee
5 Replies
Login or Register to Ask a Question