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 !
# 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
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,
Drop the outer pair of enclosing brackets.
For numeric comparison use -gt and -lt, not > and < which perform string comparison.
Regards,
Alister
Hi Alister,
Thank you for the quick reply. I tried as you suggested but still its showing me the same error
$ 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
$ 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
Where does "countchar"
if [ "$countchar" > "20" ] && [ "$countchar" < "25" ];
come from?
Should this be "charcount"?
Hi Wisecracker, I must say that was a wonderful catch.
$ 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
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
Also just noticed "n=0" should this be "nu=0"?
LBNL, make sure you have indented correctly, it does make it difficult to read...
1 Like
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
2 Likes
I used sh -x as advised. Took sometime but was able to debug successfully.
Thanks Alister. Btw, your comment about the lack of double quotes really helped. It helped me fixed quite many issues even in other codes.. Thank you very muhc.
Regards
Nss280:b: