i=kk
if [ $i > 5 ]; then echo hi; else echo ii; fi
The aboce command returns the value "hi" and the most important thing is... it created a file of name 5. why does this happen???
Hi sajithamol,
You have created a variable $i with a value of "kk". Next, you have told it (even though that's not what you thought you were telling it) to run "kk" and redirect the standard output of the command to a file called "5" and to test the
exit status from that command to determine whether to echo "hi" or "ii". Since there is (presumably) no command "kk" the command "$i > 5" creates the file 5 but there is no output sent to the file, so file "5" is left empty. Furthermore, since "kk" is not the name of a command that your shell can find, the "exit status" is non-zero (127 for my shell, but it's possible that it is something else for you.
I may be misunderstanding what you are actually trying to do, but it looks awfully odd to me to be testing to see if a string "kk" is greater than an integer 5. Furthermore, if you are trying to do a string comparison, the only conditions that you can test are equal (=) and not equal (!=). OTOH, if you want an integer comparison, then you don't use ">". You should use "-gt". Check "man test" for more details.
I'm not sure which shell you are using, but it looks more like a Bourne-shell derivative than a C-shell derivative, so I'll give you the following example:
- $ i=7
-
$ export i
-
$ echo $i
-
7
-
$ if [ $i -gt 5 ]; then echo "hi"; else echo "ii"; fi
-
hi
This does not create a file named "5". :)
Best Regards,
Paul