Why this behaviour of IF condition?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why this behaviour of IF condition?
# 1  
Old 09-12-2008
Why this behaviour of IF condition?

I have a variable, defndata, which is a number (fetched from a file using awk).

I want that if defndata is not initialized (that is its not found in the file using awk), then to execute a block of statements, otherwise execute another block.

if [ "$defndata" -ne "" ]
then
....
else
...
fi

Now this statement is working fine if the value is not found, and defndata is blank.
However, if the value found is 0 (zero), its still treating it as a blank ("") and else condition block is being executed. If any other number is being found, it executes the first block (as expected).

I am not able to understand why?Smilie

Note: I am using ksh.
# 2  
Old 09-12-2008
Try using:
Code:
if [[ -n "$defndata" ]]
then
   ...
else
   ...
fi

# 3  
Old 09-12-2008
Quote:
Originally Posted by zaxxon
Try using:
Code:
if [[ -n "$defndata" ]]
then
   ...
else
   ...
fi

Thanks. It worked. Could you explain me where i went wrong?
# 4  
Old 09-12-2008
-ne (like -eq, -ge, ...) are for numerical comparisons. You are comparing strings.
# 5  
Old 09-12-2008
Just as Annihilannic said.

Best use double brackets (most modern shells should support them), which uses the shell instead of invoking "test", whereas it is faster. Round brackets for arithmetic values and square chars/strings etc.

There are the options -n for "not empty" and -z for "zero" for testing which make it more safe to not get the problem you had.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Ps command different behaviour

Hi Experts, ps command behavior in Redhat is such that it outputs all the output(of long lengths). In Unix the ps command output was limited to only 80 chars. In that if you pipe its output to another command hen the 80 chars restriction wouldn't be there. This 80 char limitation will only be... (14 Replies)
Discussion started by: Albert_Pinto7
14 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. UNIX for Dummies Questions & Answers

behaviour of awk

Can someone explain how the below awk simulates cat? pandeeswaran@ubuntu:~$ awk '1' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 pandeeswaran@ubuntu:~$ awk '2' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO... (3 Replies)
Discussion started by: pandeesh
3 Replies

4. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

5. Ubuntu

Weird rm behaviour

I am little bit confused by the behaviour of rm in Ubuntu. It seems that as a regular user I can delete files owned by another user even when the permissions are set to 644. Here is an example: cjohnson@carbon:~/test$ sudo touch testfile cjohnson@carbon:~/test$ ls -al total 8 drwxr-xr-x... (2 Replies)
Discussion started by: ccj4467
2 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

9. Programming

Different behaviour of this program

Hi, I have one doubt, in the below program, if I declare char *b inside the main(), the function compiles & runs properly. But at the same time, if I declare it globally it compiles but when we run it, it creates core dump (segmentation fault) both in C & C++. It is not being trapped by catch... (7 Replies)
Discussion started by: royalibrahim
7 Replies

10. Shell Programming and Scripting

Count behaviour when using su -

Gentlemen, OK, I have an odd issue here perhaps someone can shed some light for me. When running a script as its user/owner the below pause/wait works just fine. When a developer has used su - to assume the username the pause does not happen... This is a HPUX11.00 machine, I have copied the blurb... (1 Reply)
Discussion started by: Eronysis
1 Replies
Login or Register to Ask a Question