if stmt argument expected error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if stmt argument expected error
# 1  
Old 02-09-2011
if stmt argument expected error

Code:
CT=0
while read LINE
do
    # Check to see if the LINE is non-empty, and has a <td> tag in it.
    if [ -n "$LINE" -a `echo $LINE |grep "<td>"` != "" ]
    then
       # Increase the TD counter by 1
        CT=`echo "$CT+1"`
     fi
done <test.htm

throws this error: ksh[5]: test: argument expected

Code:
test.htm
<tr>
<td>text</td
<td>text1</td
<td>text2</td
<tr>

# 2  
Old 02-09-2011
It's a big waste to call grep,awk,sed,cut,... to process a single line. These are external tools that the shell has to load, launch, read from, write to, and reap. They're also bulk tools optimized for scanning through hundreds to millions of lines. Using a shell builtin will be at least one hundred times faster.

You don't need to use echo and backticks to do simple arithmetic in ksh either. I don't think your method will work either, you'll end up with a string "0+1+1+1+1" instead of "1", then "2", then "3" etc. Using the built in (( )) math operator instead of creating a subshell with backticks will be at least a hundred times faster too.

I think it's complaining because you forgot to put "" around your backticks and it started splitting strings into multiple arguments on spaces, but this would work better:

Code:
CT=0
while read LINE
do
    # Check to see if the LINE is non-empty, and has a <td> tag in it.
    if [[ "$LINE" == *"<td>"* ]]
    then
       # Increase the TD counter by 1
       ((CT++))
     fi
done <test.htm


Last edited by Corona688; 02-09-2011 at 10:41 AM..
# 3  
Old 02-09-2011
Code:
CT=0
while read LINE
do
    # Check to see if the LINE is non-empty, and has a <td> tag in it.
    if [[ "$LINE" == *"<td>"* ]]
    then
       # Increase the TD counter by 1
       ((CT++))
     fi
done <test.htm

error: ksh: syntax error: `==' unexpected

Code:
CT=0
while read LINE
do
    # Check to see if the LINE is non-empty, and has a <td> tag in it.
    if [ "$LINE" == *"<td>"* ]
    then
       # Increase the TD counter by 1
       ((CT++))
     fi
done <test.htm

error: ksh[4]: ==: unknown test operator
# 4  
Old 02-09-2011
Code:
CT=$(awk '/<td>/{c++}END{print c}' test.htm)

# 5  
Old 02-09-2011
Quote:
Originally Posted by dba_frog
Code:
CT=0
while read LINE
do
    # Check to see if the LINE is non-empty, and has a <td> tag in it.
    if [[ "$LINE" == *"<td>"* ]]
    then
       # Increase the TD counter by 1
       ((CT++))
     fi
done <test.htm

error: ksh: syntax error: `==' unexpected
How curious... I tested that statement with the "worst" version of KSH I had available, but even pdksh doesn't seem to use as old a syntax as yours. What system are you on?

In any case I thought of a better way. grep is capable of counting all by itself, no need to check a file line by line. This is also a typical way to use grep, on entire files, not single lines.

Try this:
Code:
CT=`grep -c '<td>' test.html`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test: argument expected

The following example prompts are passed into the shell script. $1 = /tmp/dir/ $2 = varies (test.txt, test1.txt, test2.txt...) $3 = test_YYYYMMDD.txt --------------------------------------------------------------------------- #!/bin/sh cd $1 if ; then if ; then ... (3 Replies)
Discussion started by: smkremer
3 Replies

2. UNIX for Dummies Questions & Answers

Test: argument expected error in shell script

Hi, I am trying to write a small script that validates if there exist files that start with a pattern in a given directory. Below is the piece of my script: #!/usr/bin/ksh BTFDIR=/opt/ships/temp if then echo 'found' else echo 'not found' fi When I run this... (2 Replies)
Discussion started by: snvniranjanrao
2 Replies

3. Shell Programming and Scripting

Error- test: argument expected

check_build_info_table() { if then export build_info_table=`sqlplus -s sna/dbmanager <<! set pagesize 0 heading off feedback off SELECT DISTINCT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'XYZ' AND TABLE_NAME = 'MY_TABLE'; exit !` ... (3 Replies)
Discussion started by: ambarginni
3 Replies

4. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

5. Shell Programming and Scripting

error : test: argument expected

Hello all, I am trying to figure out why i am getting an error while executing the script...altought it seems like its work...but still get the test arguement error...any help would be appericiate...this script basically connects to any oracle db ( just have to pass db name to it)... (4 Replies)
Discussion started by: abdul.irfan2
4 Replies

6. Shell Programming and Scripting

test: argument expected

# to search a file if it exists and whether its readable or not # if yes print its first 5 lines echo enter the filename to be searched read fname if #-d $fname then echo file doesn exists elif then echo its a directory elif then cat $fname else echo its not readable fi # end of... (9 Replies)
Discussion started by: gotam
9 Replies

7. UNIX for Dummies Questions & Answers

"test: argument expected" error

Hi, No need to say I'm new to unix shell scripting. I have a very simple script that goes this way: for datos in `ls -rt $UNXLOG/26-Jan*` do export arch=`echo $datos |cut -d, -f1` if then export linea1=`grep Debut ${arch}` export horatot=`echo $linea1 |cut -d' ' -f5` ... (7 Replies)
Discussion started by: mvalonso
7 Replies

8. Shell Programming and Scripting

test: argument expected

I'm newbie to coding script so i found test: argument expected when i run it. please help me a=`df -k |awk '{print $5 }'|egrep "(100%|%)"|cut -d"%" -f1|tail -1` if then df -k|egrep "(100%|%)"|awk '{print $1,$5,$6}' else echo "No disk capacity more than 80%" fi thk in advance (7 Replies)
Discussion started by: unitipon
7 Replies

9. Shell Programming and Scripting

ERROR-> test: argument expected , what does it mean?

I am trying to compare two integer variables in the if statement, but i am getting this "test:argument expected". What am i missing? Why is the if loop not executing correctly? trunkPCM="100000"; more $FILE |while read line do PCM=`echo $line | awk '{ print $2 }'` ... (4 Replies)
Discussion started by: tan102938
4 Replies

10. Shell Programming and Scripting

Facing test: argument expected ERROR

Hi All, When i run the below code : v_shortfield = "" if ; then echo "ravi" else echo "kumar" fi i am getting output as : sam.ksh: test: argument expected kumar Why i am getting error test:argument expected and why i am not getting output as "ravi" :confused: (5 Replies)
Discussion started by: rkrgarlapati
5 Replies
Login or Register to Ask a Question