Error in if condition string comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error in if condition string comparison
# 1  
Old 05-14-2011
Error in if condition string comparison

Hello all! I need help in debugging following script. I have no idea where I am going wrong.

Code:
#!/bin/bash
for p1 in A1 TM MP
do
    for p2 in A1 TM MP
    do
 
  for mp1 in N1 N2
  do
   for mp2 in N1 N2
   do
    for mp3 in N1 N2
    do
     for mp4 in N1 N2
     do
      for mp5 in N1 N2
      do
       for mp6 in N1 N2
       do
        for mp7 in N1 N2
        do
         for mp8 in N1 N2
         do
 
          if [ "$mp1" == "N1" || "$mp2" == "N1" || "$mp3" == "N1" || "$mp4" == "N1" || "$mp5" == "N1" || "$mp6" == "N1" || "$mp7" == "N1" || "$mp8" == "N1" ]; then
           if [ "$mp1" == "N2" || "$mp2" == "N2" || "$mp3" == "N2" || "$mp4" == "N2" || "$mp5" == "N2" || "$mp6" == "N2" || "$mp7" == "N2" || "$mp8" == "N2" ]; then
 
            echo " $p1 $p2 $mp1 $mp2 $mp3 $mp4 $mp5 $mp6 $mp7 $mp8 "
           fi
          fi
 
         done
        done
       done
      done
     done
    done
   done
  done
 done
done

What I want to achieve is, "if" condition must be true only when at least one variable from $mp1 to $mp8 is N1 and at least one variable from $mp1 to $mp8 is N2.

The errors I am getting are:
Code:
./script.sh: line 25: N1: command not found
./script.sh: line 25: N2: command not found
./script.sh: line 25: [: missing `]'

Line 25 is the line of first "if" statement

Could you please tell me where I am going wrong. Sorry if the question is noobish. Thanks in advance! Smilie

Last edited by RLOA; 05-14-2011 at 01:30 PM..
# 2  
Old 05-14-2011
Your if loop is wrong. If you want to club more than one condition it should be done like this

Code:
if [ $a == 1 ] || [ $b == 2]; then...

Your code should look something like this

Code:
if [ "$mp1" == "N1" ] || [ "$mp2" == "N1" ] || [ "$mp3" == "N1" ] 
|| [ "$mp4" == "N1" ] || [ "$mp5" == "N1" ] || [ "$mp6" == "N1" ] 
|| [ "$mp7" == "N1" ] || [ "$mp8" == "N1" ]; then

Never checked your logic, am just correcting your syntax
regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 05-14-2011
Also, the correct syntax requires a single = sign, not ==
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-14-2011
Thanks a ton guys for replying!

I made the changes and script works now. But now I'm getting following errors.

Code:
./script.sh: line 25: [: N1==: unary operator expected
./script.sh: line 25: [: N2==: unary operator expected
./script.sh: line 26: [: N1==: unary operator expected
./script.sh: line 26: [: N2==: unary operator expected

Script continues to run even after this. I'm not sure whether I should ignore this. What might be the reason for error?

Thanks! Smilie
# 5  
Old 05-14-2011
Make sure you have enough space in the if condition

Following code will fail
Code:
if ["$a" = "oops"];then

Correct it like this
Code:
if [ "$a" = "ahaa" ];then

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 05-15-2011
@ahamed,

Yeah, I have kept spaces after opening and before closing braces. Still this error keeps on popping up.
# 7  
Old 05-15-2011
can you paste your code your executing?

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

String comparison in if condition

I have written a condition if && where i am comparing the hour value with 00, can i do that, as i am checking if the hour value in the date is not equal to 00 and then diplaying the time as p.m else in a.m. Please tell me if i can compare $hour with 00. Is that correct or an error? (2 Replies)
Discussion started by: sharmistha
2 Replies

3. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. UNIX and Linux Applications

Need Help on String Comparison

Hi , My requirement is to read from a file into a variable. I need to check if the variable is equal to string "no rows selected". The sh script snippet is as follows: file=/data/lpgtwo/home/nikhilp/TriggerNames.txt echo $file var=`cat $file` echo $var if then echo "No... (3 Replies)
Discussion started by: MNG
3 Replies

6. Shell Programming and Scripting

String comparison

Hi, I have the below logic. Here 'X' is a variable having some string. if then echo "i dont need to go to ofc" else echo "i need to go to ofc" Please help me to write it in unix. Thanks. (2 Replies)
Discussion started by: 46019
2 Replies

7. Shell Programming and Scripting

String Comparison

Is there a way to compare the permission string of two files and output the string if they match? For ex: -rw-r--r-- 1 user newuser 0 2009-03-12 16:45 file2 -rw-r--r-- 1 user newuser 0 2009-03-12 16:46 fileone output: -rw-r--r-- If they don't match output will be just... (3 Replies)
Discussion started by: squardius
3 Replies

8. Shell Programming and Scripting

Help with String Comparison

I'm running the following script to compare string values to a regexp: for entry in $(lpinfo -v | cut -c 1-); do if then echo "blah" continue fi done Whenever I run it, each token of lpinfo is being interpreted as a command and I get errors such as: ... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

9. Solaris

Error in String comparison

Hi, I am getting an error while executing the below code. The error is " then echo "string matches" fi Please help me to solve this error (3 Replies)
Discussion started by: sreedivia
3 Replies

10. Programming

String Comparison

Hi all, I have a file like this ibhib=ere wefwfl=werfe sfdes=wef From this file, i need to get the lefthand side string with respect to the corresponding righthand side string. i.e, I need to get the string "ere" with respect to "ibhib". But i am stuck with how to compare a string... (1 Reply)
Discussion started by: abey
1 Replies
Login or Register to Ask a Question