Computing.Net > Forums > Unix > Unix, Script, If then else

Unix, Script, If then else

Reply to Message Icon

Original Message
Name: jerome
Date: August 26, 2004 at 01:40:31 Pacific
Subject: Unix, Script, If then else
OS: SUN
CPU/Ram: -
Comment:

I try to make a Script, which verify the lines in a File.
Dos File has 3 lines = dothis1
Dos File NOT have 3 lines = dothat2

unfortunately the if-then-else-command get always FALSE (dothat2). Even if the File has only 3 Lines. What is wrong in this command?

Script:
#################################
#!/bin/bash
if [[ 'wc -l < test.log' == "3" ]]
then
echo "File has 3 Lines!"
else
echo "File dos NOT have 3 Lines!"
fi

echo "number of lines in File:"
wc -l < test_mail.log



Report Offensive Message For Removal


Response Number 1
Name: Jc
Date: August 26, 2004 at 03:32:52 Pacific
Subject: Unix, Script, If then else
Reply: (edit)

for equlaity in bash it is -eq
So rewrite
if [[ 'wc -l < test.log' == "3" ]]
as
if [ `wc -l < test.log` -eq 3 ]

Moreover in the last you are printing the number of lines in test_mail.log instead of test.log
HTH
Jc



Report Offensive Follow Up For Removal

Response Number 2
Name: jerome
Date: August 26, 2004 at 04:17:16 Pacific
Subject: Unix, Script, If then else
Reply: (edit)

Thank you for your help.
but now I receive these messages:
__________________________________
. testLog.sh
[: wc -l < test.log: integer expression expected
File dos NOT have 3 Lines!
Number of lines in File:
3
__________________________________
I have adjust the last command to proove the 3 Lines. "test.log" exists and has 3 Lines.


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: August 26, 2004 at 07:49:06 Pacific
Subject: Unix, Script, If then else
Reply: (edit)

The corrected solution posted by jeyachitra will work. A nested command must be enclosed in back-quotes, as shown in the corrected solution. When it is enclosed in single-quotes, it becomes just a string constant, and of course the -eq wants an integer. And since you are comparing integers, notice that the corrected solution does not enclose the 3 in double-quotes, although this would be OK since they will be removed by the shell.

A more updated way to code an embedded command (for ksh and bash) is:

if [ $(wc -l < test.log) -eq 3 ]


Report Offensive Follow Up For Removal

Response Number 4
Name: jerome
Date: August 27, 2004 at 01:31:49 Pacific
Subject: Unix, Script, If then else
Reply: (edit)

GREAT... it works!
I think, I probably used the wrong quotes.. (fool)...
I took your command:
if [ $(wc -l < test.log) -eq 3 ]
And it worked properly.

Thank you very much Jim and Jeyachitra,
Greetings from Switzerland, jerome


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix, Script, If then else

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software