A question about if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A question about if statement
# 8  
Old 12-05-2011
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:
Code:
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:
Code:
 if grep -q 'VERIFICATION SUCCESSFUL' result.txt

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

Last edited by Scott; 12-05-2011 at 04:58 AM.. Reason: Code tags, please...
# 9  
Old 12-05-2011
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...

Code:
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

This User Gave Thanks to neutronscott For This Post:
# 10  
Old 12-05-2011
Quote:
Originally Posted by neutronscott
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...

Code:
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

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 ]
# 11  
Old 12-05-2011
[ is a command, but in bash it's a shell builtin.

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

Quote:
Just one last question, if [ is a command, what is its meaning in this statement if [ $i -gt $j ]
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)..

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

This User Gave Thanks to neutronscott For This Post:
# 12  
Old 12-05-2011
I understand. Thanks again.
# 13  
Old 12-06-2011
After if it's always command in shells: [, [[, (( are only some commands.
Code:
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.
Code:
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
Code:
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
Code:
if grep "some" file >/dev/null 2>&1 ; then
     echo founded
else
    echo not
fi

Same result
Code:
grep "some" file >/dev/null 2>&1  && echo founded || echo not

This User Gave Thanks to kshji 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