Logical if error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logical if error
# 1  
Old 07-04-2013
Logical if error

Hi All,

I am writing a simple script to read a file and display the lines with char count between 20 and 25. I am stuck with the if condition here. Tried a lot but still getting an error on the if condition
[: 14: unexpected operator

I am trying both the if formats below but none are working !
Code:
# if [ "$countchar" -ge "20" ] && [ "$countchar" -le "25" ]
if [ "$countchar" > "20" ] && [ "$countchar" < "25" ]

My script is very simple as below, not able to understand why its getting stuck Smilie

Code:
echo "Enter file name"
read FILENAME=$1
linecount=0
charcount=0
nu=0
linecount=`wc -l $FILENAME | cut -d " " -f7`
if [ $linecount -ge 0 ]; then
        while [ $nu -ne $linecount ]
                do
                nu=`expr $nu + 1`
                line=`sed -n ''$nu'p' $FILENAME`
                charcount=`echo $line | wc -c`
if [ [ "$countchar" > "20" ] && [ "$countchar" < "25" ] ]; then
                                        echo "Line with length between 20 and 25"
                                        echo $charcount $line
                        fi
                done
else
        echo "Error: no lines in the file"
        exit 1
fi


Regards,
# 2  
Old 07-04-2013
Quote:
Originally Posted by nss280
Code:
if [ [ "$countchar" > "20" ] && [ "$countchar" < "25" ] ]; then

Drop the outer pair of enclosing brackets.

For numeric comparison use -gt and -lt, not > and < which perform string comparison.

Regards,
Alister
# 3  
Old 07-04-2013
Hi Alister,

Thank you for the quick reply. I tried as you suggested but still its showing me the same error

Code:
$ vi prog11.sh
"prog11.sh" 22 lines, 646 characters
echo "Enter file name"
read FILENAME=$1
linecount=0
charcount=0
n=0
linecount=`wc -l $FILENAME | cut -d " " -f7`
if [ $linecount -ge 0 ]; then
        while [ $nu -ne $linecount ]
                do
                nu=`expr $nu + 1`
                line=`sed -n ''$nu'p' $FILENAME`
                charcount=`echo $line | wc -c`
if [ "$countchar" > "20" ] && [ "$countchar" < "25" ];
then
                                        echo "Line with length between 20 and 25"
                                        echo $charcount $line
                        fi
                done
else
        echo "Error: no lines in the file"
        exit 1
fi
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"prog11.sh" 22 lines, 647 characters
$ sh prog11.sh
Enter file name
myfile
[: 14: unexpected operator
$

---------- Post updated at 12:57 AM ---------- Previous update was at 12:51 AM ----------

I am in bash.

Even tried without quotes, still not working Smilie

Code:
$ vi prog11.sh
"prog11.sh" 23 lines, 647 characters
echo "Enter file name"
read FILENAME=$1
linecount=0
charcount=0
n=0
linecount=`wc -l $FILENAME | cut -d " " -f7`
if [ $linecount -ge 0 ]; then
        while [ $nu -ne $linecount ]
                do
                nu=`expr $nu + 1`
                line=`sed -n ''$nu'p' $FILENAME`
                charcount=`echo $line | wc -c`
if [ $countchar > 20 ] && [ $countchar < 25 ]
then
                                        echo "Line with length between 20 and 25"
                                        echo $charcount $line
                        fi
                done
else
        echo "Error: no lines in the file"
        exit 1
fi

~
~
~
~
~
~
~
~
~
~
~
~
~
"prog11.sh" 23 lines, 639 characters
$ sh prog11.sh
Enter file name
myfile
[: 14: unexpected operator
$


Regards,
Nss280
# 4  
Old 07-04-2013
Where does "countchar"
Code:
if [ "$countchar" > "20" ] && [ "$countchar" < "25" ];

come from?

Should this be "charcount"?
# 5  
Old 07-04-2013
Hi Wisecracker, I must say that was a wonderful catch.

Code:
$ vi prog11.sh
"prog11.sh" 23 lines, 639 characters
echo "Enter file name"
read FILENAME=$1
linecount=0
charcount=0
n=0
linecount=`wc -l $FILENAME | cut -d " " -f7`
if [ $linecount -ge 0 ]; then
        while [ $nu -ne $linecount ]
                do
                nu=`expr $nu + 1`
                line=`sed -n ''$nu'p' $FILENAME`
                charcount=`echo $line | wc -c`
if [ "$charcount" > "20" ] && [ "$charcount" < "25" ]
then
                                        echo "Line with length between 20 and 25"
                                        echo $charcount $line
                        fi
                done
else
        echo "Error: no lines in the file"
        exit 1
fi

~
~
~
~
~
~
~
~
~
~
~
~
~
"prog11.sh" 23 lines, 647 characters
$ sh prog11.sh
Enter file name
myfile
[: 14: unexpected operator
$

Still the same issue Smilie

Regards,
Nss280

---------- Post updated at 01:38 AM ---------- Previous update was at 01:23 AM ----------

would redirecting to /dev/null >0 help here?

Regards,
nss280
# 6  
Old 07-04-2013
Also just noticed "n=0" should this be "nu=0"?

LBNL, make sure you have indented correctly, it does make it difficult to read...
This User Gave Thanks to wisecracker For This Post:
# 7  
Old 07-04-2013
The read statement should just use FILENAME.

Your script suffers from a pervasive lack of double quoted variables. Even the character counting is buggy because $line isn't quoted.

If after fixing those issues the script still misbehaves, enable tracing, sh -x prog11.sh, and post the output.

By the way, this is an extremely inefficient way of filtering lines. awk's length function would make this a concise and efficient one- liner.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Logical Error With Type Conversion In C

So, I'm into about 650 lines of some code I'm working on. So I'll try to explain instead of flooding this post. Say I have some code like this: int main() { int i, j; char data; printf("Gimme something: "); fgets(data, INPUT_BUFF, stdin); for (j = 0; j < data; j++){... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

Logical error

I have this script to uvscan-update. Seems like that i am getting logical error at the end of the script. It is updating the script and also giving the error message to update it manually. I have deleted the DAT files to see if it will create new and it does. Below is the error and the script: ... (1 Reply)
Discussion started by: mk07md
1 Replies

3. UNIX for Dummies Questions & Answers

problem using logical or

Hi, I have a script testor.s which takes a string as command line argument, Contents of the script: #!/bin/ksh -x if ] then echo "error" else echo "correct" fi Here, though i provide the command line argument as "WO_STMT_05292009", it displays error Is there... (3 Replies)
Discussion started by: Sheema
3 Replies

4. Shell Programming and Scripting

logical expressions

Hi Fdz, I have to verify the length of two uids are equals to 11 or not. The conditions is both lengths should be 11.I tried with this logic expr. am unable to get correct results.(Shell script in Unix i need) if do echo"Results" done Thanx (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

5. Shell Programming and Scripting

How to do logical AND and logical OR with grep

Hi can someone please help me on this. I need to perform this code: Grep any lines that meets the following criteria (A AND B) OR (A AND C) I tried this code, but it didn't work Grep-I "A &&B" | "A&&C" *.* $ thanks in advance (12 Replies)
Discussion started by: Needhelp2
12 Replies

6. Shell Programming and Scripting

logical OR in sed

frnds.. can i perform an OR operation in sed syntax ? if yes.. how? I need to search for some 2-3 mail addresses in multiple files and delete all those... and instead of them.. I need to insert a new mail id...( these are also other emails in that list .. which sud not be affected ) is... (8 Replies)
Discussion started by: clx
8 Replies

7. Shell Programming and Scripting

logical if condition

hi i have the following scenario #!/bin/sh a=21.0 b=33.0 c=16.0 cmd=20 cmd1=30 if && ] then echo "problem....." exit 1 else echo "ok" exit 0 fi the issue here is the above condition is never TRUE coz a>cmd && b >cmd1 (7 Replies)
Discussion started by: nano2
7 Replies

8. UNIX for Dummies Questions & Answers

Error in creating logical partition

Dear Members, I am using SCO-Unix 5.0.5. I have created 2 logical drives but some of the blocks are overlapped and are now giving warning during startup. I have used fsck to settle the problems but to no avail. Kindly help me to sort out the problem. Thanks (3 Replies)
Discussion started by: nagendrajaiswal
3 Replies
Login or Register to Ask a Question