3 ms·
Apologies for being stupid but this does not make sense to me: while [ true ];do ssh user@62.141.55.117; sleep 0.1; done Is this the same as writing: whi
by micro_softy 10y ago
Apologies for being stupid but this does not make sense to me:
while [ true ];do ssh user@62.141.55.117; sleep 0.1; done
Is this the same as writing:
while test true; do ...; done
Then this would also work:
while [ false ]; do ...; done
But if the plan is to use the keyboard e.g. INT to stop this loop, why test anything? One could just write:
while :; do ...; done
- jordigh 10y ago`test` is the oldest and most portable syntax, "[" is a POSIX synonym which is either a shell built-in (e.g. in bash) or a command i.e. /usr/bin/[ . You are correct that the author seems to misunderstand what "[" and "test" do, probably by failed analogy with C syntax. In the case of "test foo" for any string "foo", "test" will be evaluating a non-empty expression which is always true.
- INTPenis 10y agoThere's always something to remark on other peoples use of bash, I often find. For example I'd rather just do while command; do sleep 0.1; done. Or simply install autossh.
- yuubi 10y agoFor a vaguely similar use case (repeat till host is up) I once wrote until ssh ...; do sleep 5; done which stops looping once a connection succeeds.
- micro_softy 10y agowhile :;do ssh ... && break; sleep 5; done
- yuubi 10y agoWhy? That reminds me of the while(1) { if (condition) break; ... } stuff I've seen too much of and can't explain, instead of just while(!condition) { ... }.
- micro_softy 10y agoI'm not much of C programmer but I think it's an approximation of an infinite loop. for(;;){ if(condition) break; }
- yuubi 10y agoYes, that looks exactly analogous, but my question is why people don't use the perfectly good built-in conditional termination in a while or for loop and instead add on an if statement. (Also, I don't understand why a for(;;) loop is more attractive than a while(1) loop.)