Shell Script 101: TEST
I feel a bit embarrassed to ask this having been a Linux user for over 10 years - but I've never really dug deep into shell scripting.
One of the log rotate scripts on a small gateway contains this: Code:
prerotate I'm assuming that if the prerotate line returns true that the postrotate section will run. If not the 'endscript' will terminate it? I'm assuming the truth of prerotate is negated with ! and what that line basically says is if /usr/sbin/sarg-reports is not executable ... then I struggle to get the logical || or with just the file name. If I could understand the purpose of the || or, I think I'd 'get it' and the postrotate line. Be gentle with me, I'm ashamed enough that I don't know. Google has been little help to me (|| seems to upset it) |
Hi.
Command Code:
test ! -x /usr/sbin/sarg-reports || /usr/sbin/sarg-reports Quote:
Code:
test ! -x file |
bash ||
|
What you're seeing is the original developer taking advantage of "short circuit" boolean evaluation.
You have two basic boolean operations where this comes into play: logical AND (&&) and logical OR (||). If you have a statement such as "alpha && beta" then alpha must evaluate to true and beta must evaluate to true for the whole expression to evaluate to true. The point is that for logical AND, both alpha and beta must be evaluated to determine the overall expression. Compare that with a statement such as "alpha || beta." There are three cases where the overall statement evaluates to true: (1) alpha = true; beta = false, (2) alpha = true; beta = true, (3) alpha = false; beta = true. Notice that if alpha evaluates to true, then it does not matter what beta evaluates to--the overall expression will still be true. That's "short circuit" evaluation. The shell will evaluate only so much of the expression as is necessary to determine the overall result. So, back to your code snippets, because they use ||, the second half of the statement will be ignored if the first half is true. Only when the first half evaluates to false will the second half get evaluated (which in this case is launching a new process). EDIT: It seems I was fashionably late to the party :) |
Thank you. Much appreciated. Man Bash. Dear oh dear. How did I miss that.....
{head held in shame} |
Quote:
Code:
test ! -x /usr/sbin/sarg-reports || /usr/sbin/sarg-reports Quote:
Sincere thanks to all. Kicking myself. |
And this is the cue for the entrance of the double-negative that is Unix process exit codes.
firstfire correctly stated that: Code:
test ! -x file What you have to remember is, for Unix process exit codes, 0 is "success" or "true." A non-zero value is "failure" or "false." That's the exact opposite of programming languages like C. So, just to step through: assuming you have execute privileges on the file Code:
test -x file Code:
test ! -x file Because the left half evaluates to "false," the right half is evaluated--and hence, spawns the process. EDIT: And as for my being late, I must be, because firstfire and cin_ were already on the dance floor ;) |
Equivalent command using AND (&&) operator would be
Code:
test -x /usr/sbin/sarg-reports && /usr/sbin/sarg-reports Code:
$ test -x `which bash` && echo yes || echo no |
as led Isadora Duncan's, My Life
Quote:
|
Quote:
I created a simple 'hello world' shell script and did NOT add execute to it. Testing with the example firstfire gave (modified) it seems to be the exact opposite of what I would expect. Code:
$ test -x `/home/leslie/scripts/hello.sh` && echo yes || echo no If I don't have execute permissions test -x the result should be 1/false in shell speak. The other side of the AND is echo yes and fires because the return code 1 (a double bluff here) is logically 'true' and evaluation of the && clause continues - firing the echo. However, as I see it the shell would return '0' (true) for the echo statement, meaning the && as a whole should fail??? 1&&0 is not true???? Therefore the whole clause fails and the || or should be evaluated printing the 'no' to the screen - but this does not happen? Clearly I'm wrong as 'no' never prints. If I invert the test Code:
$ test ! -x `/home/leslie/scripts/hello.sh` && echo yes || echo no Code:
#!/bin/sh Somewhere, something in my brain is broken, can you help me fix it! Quote:
|
Hang on! I'm being a divvy....
Quote:
Funny what a cup of tea will do! Thanks to all. Finally....... derrrrrrrr. |
Quote:
|
Quote:
I cannot think why the programmer chose the more convoluted form. |
Quote:
Yes, my original description was incomplete. If the first/left half of a logical AND evaluates to false, the second/right half will not be evaluated--because there is no value for that portion which could make the entire statement evaluate to true. Sorry to anyone if my haphazard response caused confusion. And I gotta say, this is turning out to be a decent conga line. |
All times are GMT -5. The time now is 06:44 AM. |