Re: SOLVED(?): [expert] test command
Solved! I should have read man bash pages! Options are one of the Arguments. test arg1 arg2 arg3 .. So, $test -nCR or $test -zCR is just like $test abcCR. -n, -z, or abc is handled as arg1 and as 'a string'. Then test deos not think that -z or -n is an option statement. The arg1 (-z or -a or -*) can be considered option only if test has arg2. Without arg2, -z or -n or -* should be handled just like abc. 1 argument The expression is true if and only if the argument is not null. Then $test -*;echo$? returns 0(true) and $test "";echo$? returns 1(false). And in case of no argment which is just $testCR, man bash says 0 argment - The expression is false. am i right?? +-+ * Get your own Jmail account.|URL= http://www.jmail.co.jp |--jmail--| * Get your own home page.|URL= http://www.servance.jp +-+ * Get your own domain.|URL= http://domaintorou.com/ Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: SOLVED(?): [expert] test command
wuha, i am sorry to say that, but what i wrote was mostly wrong :( it's just too simple. i didn't saw cause i assumed to deal with a strange situation. but this is just normal behaviour. you wrote: $echo $? returns 0 after these test commands. $test -z $test -z first of all. my biggest mistake ;) test isn't a shell builtin but a /usr/bin command. but doesn't really matter. so what do you know about test after this first to commands? that it works correctly, because is an empty string an '-z' tests for empty strings. than you also know, that the 'test' is programmed in that way, that it returns a '0' if an option but no argument is given. you see this in the manpages (man test): test EXPRESSION [ EXPRESSION ] test OPTION you see that EXPRESSION is in [ ] brackets. means it is optional - no error - therefor exit code '0' ;). i had to read thru the manpage many times carefully too until i saw it. just looked to far to see the obvious. after you understood this, the rest explains it self: But it returns different value after followings. $test -n -- echo$? returns 1 $test -n -- 0 is an empty string, but '-n' tests for a not empty string. i know it's a brain sucker, but think about mathematicly. what is an not empty string? a string with something inside. so... string is 'not empty', here would '-n' return with 0. but is empty, it is not an 'not empty' string. therefor '-n' returns 1. while i was writing this, i remembered that i did fallen already once into this. funny unix is, isnt it? luv it ;) have phun B) miLosh -- DONT ATTACK IRAQ !!! this message was sent by: +--+ |.::|[ The pleXus Network ]|::.| +--+ www:plexus.shacknet.nu | ftp:plexus.shacknet.nu +---+ irc:plexus.shacknet.nu:6667 channel: #plexus | +--+---+ pub 1024D/C944D699 miLosh (pleXus) [EMAIL PROTECTED] \_ Key fingerprint = 18FB 24BA 77A8 813F 6A8C F67B C08C 5A76 C944 D699 \ +--+ msg63125/pgp0.pgp Description: PGP signature
Re: [expert] test command
Tks for reply. I understand the way $test -z "" returns 0 and $test -n "" returns 1. What's puzzling me here is this. $test -z (there is no space after -z, no argment) returns 0 which means 'test command thinks no argment equals to the empty string'. However, $test -n (no space after, no argment) return 0 which means 'test command in this case thinks no argment equals to NO-empty string'. When no argment is given, why test command thinks differenly depending on the options -z and -n. Can you help?? On Thu, Dec 19, 2002 at 12:44:30PM +0900, ath1410 wrote: $test -z "" this tests whether the string is empty. in this example it is, therefor exit code 0. But it returns different value after followings. $test -n ""-- echo$? returns 1 $test -n -- " " 0 this tests whether the string is NOT empty. the first one is empty, so exit code is 1. the second one isn't empty. i guess that you think that spaces count as empty strings, but a space is a character as everyone (ascii 039), therefor your string isnt empty and exit code is 0. okey? have phun;) miLosh +-+ * Get your own Jmail account.|URL= http://www.jmail.co.jp |--jmail--| * Get your own home page.|URL= http://www.servance.jp +-+ * Get your own domain.|URL= http://domaintorou.com/ Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] test command
On Fri, 20 Dec 2002 18:18:10 +0900 ath1410 [EMAIL PROTECTED] wrote: Tks for reply. I understand the way $test -z returns 0 and $test -n returns 1. What's puzzling me here is this. $test -z (there is no space after -z, no argment) returns 0 which means 'test command thinks no argment equals to the empty string'. However, $test -n (no space after, no argment) return 0 which means 'test command in this case thinks no argment equals to NO-empty string'. When no argment is given, why test command thinks differenly depending on the options -z and -n. Can you help?? this page can help you: http://tldp.org/LDP/abs/html/testconstructs.html bye jipe Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] test command
On Fri, Dec 20, 2002 at 12:48:01PM +0100, jipe wrote: On Fri, 20 Dec 2002 18:18:10 +0900 ath1410 [EMAIL PROTECTED] wrote: $test -z (there is no space after -z, no argment) returns 0 which means 'test command thinks no argment equals to the empty string'. However, $test -n (no space after, no argment) return 0 which means 'test command in this case thinks no argment equals to NO-empty string'. When no argment is given, why test command thinks differenly depending on the options -z and -n. oh, didn't saw that. ok. this' kinda twisted to understand. first of all, in the '-z' example u r giving a string, which means 'test' has something to test for. depending on the argument, it returns its exit code. the '-n' example u showed has NO argument, which means 'test' has nothing to test for. it returns zero. i understand ur confusion. it is common in programms to return a 0 value if a needed argument is missing. but this is not a programm, it's a shell builtin. end you do not got the exit code from the command explizitly, but from that what happend for the shell (namely bash, 4example). so what happens for the shell? a 'strace test -n' reveals here many secrets ;). the shell takes the command, validates it, than takes the option, validates it. than nothing more is to do. no more 'commands' r available to the shell. now here plays strace. as u may see at the very end, it makes a close of a library it opened befor to get ur locale. the close is successful, end THIS returns your exit (0). clear now? hope didn't confused u more, hehe. this page can help you: http://tldp.org/LDP/abs/html/testconstructs.html nice site, but doesnt explain he's problem. at least i didn't found an explanation there. have phun ;) miLosh -- DONT ATTACK IRAQ !!! this message was sent by: +--+ |.::|[ The pleXus Network ]|::.| +--+ msg63097/pgp0.pgp Description: PGP signature
Re: [expert] test command
:: Tks for reply. :: :: I understand the way $test -z returns 0 and $test -n returns 1. :: :: What's puzzling me here is this. :: :: $test -z (there is no space after -z, no argment) returns 0 which means :: 'test command thinks no argment equals to the :: empty string'. :: :: However, $test -n (no space after, no argment) return 0 which :: means 'test command in this case thinks no argment equals to :: NO-empty string'. :: :: When no argment is given, why test command thinks differenly :: depending on the options -z and -n. :: :: Can you help?? Maybe. What I'll say is based on years old knowledge so it may be slightly off in the details (I hardly used `vargs' in my progamming days) but the idea of what's happening will remain intact. When, at the prompt (==), you type something like: ==command Arg1 Arg2 The shell first splits the line into strings arg0, arg1, arg2 and assigns the elements of the commandline to them: arg0 = command arg1 = Arg1 arg2 = Arg2 Whitespace, i.e., spaces, newlines, and tabs in between, before, and after the elements on the commandline is (as I recall) discarded so that if, say, Arg2 is not supplied, then string arg2 will not be defined in the application you are running. In particular, then, lets say you type: ==test -n foo Within `test' string arg1 = -n and string arg2 = foo and the test is meaningful and will return TRUE becuase `-n' says (is arg2 a sting) AND (is arg2 not empty) which is true. Likewise if arg2 = (the empty string) will return false. However for ==test -nspacesCR or just ==test -nCR where spaces stands for spaces and CR stands for the the newline, arg2 is _undefined_ which means it is not even in the category of string much less the empty string (which is still something. namely \0 in memory). So `test' is saying to itself I have no arg2 to test against. Formally, arg2 does not exist == arg2 is not a string. Hence you get an automatic false. Likewise for -z and no following argument. Dean S. Messing Display Algorithms Visual Optimization Lab Information Systems Technologies Dept. Sharp Laboratories of America Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] test command
Tks Mr.miLosh Mr.Dean, This problem is getting clear now. However, let me please ask one more question that still remains in me. Mr.miLosh advised; first of all, in the '-z' example u r giving a string, which means 'test' has something to test for. depending on the argument, it returns its exit code. No, in the '-z' example, I am NOT giveing a string. $test -zCR -- NOT $test -zspaceCR Mr.Dean advised; So `test' is saying to itself "I have no arg2 to test against. Formally, arg2 does not exist == arg2 is not a string. Hence you get an automatic "false". Yes, I understand why $test -nCR returns 0. As you mentioned, no argment is different from no string. But this assumptions also apply '-z' option, isn't it? If so, $test -zCR should return 1 not 0, because `test' should say to itself (as in -z case) that I have no arg to test against, then it should return authentic `false'. I read comments from Mr.miLosh Mr.Dean many time and tried test command in various patters many times. (I should have studied English harder!!) And finally I came up with a conclusion as Mr.which quotes; 'it is common in programms return a 0 value if a needed argument is missing.' So test returns 0 if no arg is given after -* option. Thus both $test -zCR and $test -nCR returns 0. Now I forced myself to be clear on this!! But why $testCR (no option, no args) returns 1 now, not 0!! I agagin have to force myself into thinking that it's a rule. +-+ * Get your own Jmail account.|URL= http://www.jmail.co.jp |--jmail--| * Get your own home page.|URL= http://www.servance.jp +-+ * Get your own domain.|URL= http://domaintorou.com/ Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
Re: [expert] test command
On Thu, Dec 19, 2002 at 12:44:30PM +0900, ath1410 wrote: $test -z this tests whether the string is empty. in this example it is, therefor exit code 0. But it returns different value after followings. $test -n -- echo$? returns 1 $test -n -- 0 this tests whether the string is NOT empty. the first one is empty, so exit code is 1. the second one isn't empty. i guess that you think that spaces count as empty strings, but a space is a character as everyone (ascii 039), therefor your string isnt empty and exit code is 0. okey? have phun;) miLosh -- DONT ATTACK IRAQ !!! this message was sent by: +--+ |.::|[ The pleXus Network ]|::.| +--+ www:plexus.shacknet.nu | ftp:plexus.shacknet.nu +---+ irc:plexus.shacknet.nu:6667 channel: #nocturne | +--+---+ pub 1024D/C944D699 miLosh (pleXus) [EMAIL PROTECTED] \_ Key fingerprint = 18FB 24BA 77A8 813F 6A8C F67B C08C 5A76 C944 D699 \ +--+ msg63051/pgp0.pgp Description: PGP signature
Re: [expert] test command
Tks for reply. I understand the way $test -z "" returns 0 and $test -n "" returns 1. What's puzzling me here is this. $test -z (there is no space after -z, no argment) returns 0 which means 'test command thinks no argment equals to the empty string'. However, $test -n (no space after, no argment) return 0 which means 'test command in this case thinks no argment equals to NO-empty string'. When no argment is given, why test command thinks differenly depending on the options -z and -n. Can you help?? On Thu, Dec 19, 2002 at 12:44:30PM +0900, ath1410 wrote: $test -z "" this tests whether the string is empty. in this example it is, therefor exit code 0. But it returns different value after followings. $test -n ""-- echo$? returns 1 $test -n -- " " 0 this tests whether the string is NOT empty. the first one is empty, so exit code is 1. the second one isn't empty. i guess that you think that spaces count as empty strings, but a space is a character as everyone (ascii 039), therefor your string isnt empty and exit code is 0. okey? have phun;) miLosh +-+ * Get your own Jmail account.|URL= http://www.jmail.co.jp |--jmail--| * Get your own home page.|URL= http://www.servance.jp +-+ * Get your own domain.|URL= http://domaintorou.com/ Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com
[expert] test command
$echo $? returns 0 after these test commands. $test -z "" $test -z But it returns different value after followings. $test -n ""-- echo$? returns 1 $test -n -- " " 0 Can someone explain why it goes as above? Especially I don't figure out why $test -n returns 0. +-+ * Get your own Jmail account.|URL= http://www.jmail.co.jp |--jmail--| * Get your own home page.|URL= http://www.servance.jp +-+ * Get your own domain.|URL= http://domaintorou.com/ Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com