Re: [Cooker] BUG: chkconfig true is false
Am Freitag, 25. Oktober 2002 04:29 schrieb Bill Shirley: > Thanks for your reply. It really clears things up. > > > In shell scripts, 0 is true, 1 (or any other non-zero) is false, > > allowing for error codes. > > How counter intuitive!! I have had beginning C programming, (so > I'm an expert now :-) and false is zero and true is anything not > false. It's a crazy world we live in. The key point is the "allowing for error code" part. And actually many C functions don't use 0 => false, 1 => true either. Three basic types of returning results from functions come to mind: 1) int foo( void ); returns non-zero on success, zero on failure. This sticks to the natural "false is 0, true is != 0" idiom and is mostly used for functions that return an exact error code somewhere else and some useful value on success. 2) int foo( void ); returns > 0 on success, < 0 (often -1) on failure. These are used just like 1) when 0 is in the set of meaningful successful return values. 3) int foo( void ); returns 0 on success and != 0 (often an error code) on failure. This is often used to commucate an error code in the return value and on success, no value is returned anyway - or some of the arguments are output parameters. Only 1) implements 0 == false, 1 == true (or rather 0 == false, 0 != true). So why should the shell stick to 1) and not 3) ? It's pretty unusual that a program wants to returns a meaningful value in its exit status *on success*. That's what stdout is for. Just my 0.02€, -Malte
RE: [Cooker] BUG: chkconfig true is false
Thanks for your reply. It really clears things up. > In shell scripts, 0 is true, 1 (or any other non-zero) is false, > allowing for error codes. > How counter intuitive!! I have had beginning C programming, (so I'm an expert now :-) and false is zero and true is anything not false. It's a crazy world we live in. So: [root@elmo cron.hourly]# chkconfig innd on [root@elmo cron.hourly]# ! /sbin/chkconfig innd || echo "Running nntpsend"; echo $? Running nntpsend 0 [root@elmo cron.hourly]# chkconfig innd off [root@elmo cron.hourly]# ! /sbin/chkconfig innd || echo "Running nntpsend"; echo $? 0 works correctly. And if I change the two scripts in /etc/cron.hourly: [root@elmo cron.hourly]# cat /etc/cron.hourly/inn-cron-nntpsend #!/bin/sh ! /sbin/chkconfig innd || su - news -c /usr/bin/nntpsend [root@elmo cron.hourly]# cat /etc/cron.hourly/inn-cron-rnews #!/bin/sh ! /sbin/chkconfig innd || /usr/bin/rnews -U then I should get a return code of zero no matter whether innd is turned on or off and thus no email from cron. Who should make this change? Bill Shirley
[Cooker] BUG: chkconfig true is false
I am getting an email from cron every hour when /etc/cron.hourly/inn-cron-nntpsend runs. I found an bug in chkconfig: [root@elmo cron.hourly]# chkconfig --list innd innd0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@elmo cron.hourly]# chkconfig innd; echo $? 1 If I read the man page correctly, this should give a zero return code because innd is not included in this run level (which is 3). And then: [root@elmo cron.hourly]# chkconfig innd on [root@elmo cron.hourly]# chkconfig --list innd innd0:off 1:off 2:off 3:on4:on5:on6:off [root@elmo cron.hourly]# chkconfig innd; echo $? 0 should return a 1. It is REVERSED. This causes: [root@elmo cron.hourly]# chkconfig --list innd innd0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@elmo cron.hourly]# cat /etc/cron.hourly/inn-cron-nntpsend #!/bin/sh /sbin/chkconfig innd && su - news -c /usr/bin/nntpsend to run the nntpsend command even though innd is turned off. Now the second problem. bash appears to be not using the return code of the last command in a && situation: [root@elmo cron.hourly]# chkconfig --list innd innd0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@elmo cron.hourly]# /sbin/chkconfig innd && su - news -c /usr/bin/nntpsend; echo $? 1 [root@elmo cron.hourly]# su - news -c /usr/bin/nntpsend; echo $? 0 The man page reads: The control operators && and || denote AND lists and OR lists, respec- tively. An AND list has the form command1 && command2 command2 is executed if, and only if, command1 returns an exit status of zero. An OR list has the form command1 || command2 command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list. To sum it all up: when /etc/cron.hourly/inn-cron-nntpsend runs, I get an email saying: run-parts: /etc/cron.hourly/inn-cron-nntpsend exited with return code 1 because chkconfig gives a reversed result and runs the nntpsend command. bash is using the return code from the chkconfig command (1) instead of the nntpsend command (0) and cron is reporting it. How do we fix chkconfig and bash? Bill Shirley
Re: [Cooker] BUG: chkconfig true is false
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Bill Shirley wrote: | I am getting an email from cron every hour when | /etc/cron.hourly/inn-cron-nntpsend runs. Is there anything in the mail? What happens if you run this script manually? What is the error code? Any outout going to stderr or stdout (in which case, cron sends it to you), even just a newline? | | I found an bug in chkconfig: | | [root@elmo cron.hourly]# chkconfig --list innd | innd0:off 1:off 2:off 3:off 4:off 5:off 6:off | [root@elmo cron.hourly]# chkconfig innd; echo $? | 1 | | If I read the man page correctly, this should give a zero return code | because innd is not included in this run level (which is 3). | Quoting the man page: If only a service name is given, it checks to see if the service ~ is configured to be started in the current runlevel. If it is, chkcon- ~ fig returns true; otherwise it returns false. In shell scripts, 0 is true, 1 (or any other non-zero) is false, allowing for error codes. [bgmilne@bgmilne bgmilne]$ true; echo $? 0 [bgmilne@bgmilne bgmilne]$ false; echo $? 1 Looks like it's doing the right thing ... returning false (which is 1) since the service is not configure for the current run level. | And then: | [root@elmo cron.hourly]# chkconfig innd on | [root@elmo cron.hourly]# chkconfig --list innd | innd0:off 1:off 2:off 3:on4:on5:on6:off | [root@elmo cron.hourly]# chkconfig innd; echo $? | 0 | | should return a 1. | | It is REVERSED. This causes: | | [root@elmo cron.hourly]# chkconfig --list innd | innd0:off 1:off 2:off 3:off 4:off 5:off 6:off | [root@elmo cron.hourly]# cat /etc/cron.hourly/inn-cron-nntpsend | #!/bin/sh | /sbin/chkconfig innd && su - news -c /usr/bin/nntpsend | | to run the nntpsend command even though innd is turned off. No, it should only run nntpsend if innd is turned *on*. Try this: /sbin/chkconfig innd && echo "Innd is turned on" | | | Now the second problem. bash appears to be not using the | return code of the last command in a && situation: | It won't return the code of the last command if it didn't run it. | [root@elmo cron.hourly]# chkconfig --list innd | innd0:off 1:off 2:off 3:off 4:off 5:off 6:off | [root@elmo cron.hourly]# /sbin/chkconfig innd && su - news -c | /usr/bin/nntpsend; echo $? | 1 Try: /sbin/chkconfig innd && echo "Running nttpsend" && \ su - news -c /usr/bin/nntpsend; echo $? You won't see the "Running nntpsend, since chkconfig fails, so doesn't run any more commands, thus it will return false (error code of last command that was *actually* run). | [root@elmo cron.hourly]# su - news -c /usr/bin/nntpsend; echo $? | 0 | | The man page reads: | | The control operators && and || denote AND lists and OR lists, respec- |tively. An AND list has the form | | command1 && command2 | |command2 is executed if, and only if, command1 returns an exit | status |of zero. | |An OR list has the form | | command1 || command2 | |command2 is executed if and only if command1 returns a | non-zero exit |status. The return status of AND and OR lists is the exit | status of |the last command executed in the list. | | To sum it all up: when /etc/cron.hourly/inn-cron-nntpsend runs, I get | an email | saying: | run-parts: /etc/cron.hourly/inn-cron-nntpsend exited with return code 1 | | because chkconfig gives a reversed result and runs the nntpsend command. | bash is using the return code from the chkconfig command (1) instead of | the | nntpsend command (0) and cron is reporting it. | | How do we fix chkconfig and bash? | Neither are broken. Try these (and some more yourself): [bgmilne@bgmilne bgmilne]$ true && echo "I succeeded";echo $? I succeeded 0 [bgmilne@bgmilne bgmilne]$ false && echo "I succeeded";echo $? 1 [bgmilne@bgmilne bgmilne]$ false || echo "I failed"; echo $? I failed 0 [bgmilne@bgmilne bgmilne]$ true || echo "I failed";echo $? 0 Buchan - -- |Registered Linux User #182071-| Buchan MilneMechanical Engineer, Network Manager Cellphone * Work+27 82 472 2231 * +27 21 8828820x121 Stellenbosch Automotive Engineering http://www.cae.co.za GPG Key http://ranger.dnsalias.com/bgmilne.asc 1024D/60D204A7 2919 E232 5610 A038 87B1 72D6 AC92 BA50 60D2 04A7 -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE9uBmxrJK6UGDSBKcRAjt2AKCykOpI6YZwOMyWrAbLKxAa3cmbxwCfVCSV YO03LPdl+yW1AD3rP3fBc3M= =kdGZ -END PGP SIGNATURE-