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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix, Script, If then else

Reply to Message Icon

Name: jerome
Date: August 26, 2004 at 01:40:31 Pacific
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




Sponsored Link
Ads by Google

Response Number 1
Name: Jc
Date: August 26, 2004 at 03:32:52 Pacific
Reply:

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



0

Response Number 2
Name: jerome
Date: August 26, 2004 at 04:17:16 Pacific
Reply:

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.


0

Response Number 3
Name: Jim Boothe
Date: August 26, 2004 at 07:49:06 Pacific
Reply:

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 ]


0

Response Number 4
Name: jerome
Date: August 27, 2004 at 01:31:49 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix, Script, If then else

Bourne shell error if/then/else www.computing.net/answers/unix/bourne-shell-error-ifthenelse/4983.html

IF-Then-Else syntax Help!!!! www.computing.net/answers/unix/ifthenelse-syntax-help/6684.html

ksh script if then if? www.computing.net/answers/unix/ksh-script-if-then-if/5649.html