blogger is right. however, it's important to note the syntax exactly: if you're using an interactive shell (e.g. the terminal)
echo -n; for i in ; do if [[ $? ]]; then foo; fi; done
the "$?" returns the status of the previous function call; because we're calling "echo" first, the first iteration of the loop will be "true" (0 in bash.) the "fi" closes off the "if" statement, and the semicolons are necessary.
here's the same thing if you were writing a bash script:
#!/bin/bash
echo -n
for i in ; do
if [[ $? ]]; then
foo
fi
done
you'll notice that foo doesn't have a semicolon after it; when writing a full-blown bash script in an environment where line-breaks are available (e.g. your favorite text editor,) line-breaks do the same thing as semicolons :)
also, note that you could instead use
[[ $? ]] && foo
instead of the if statement; realize that the && means that foo will only be executed if the previous foo didn't have an error. i.e. in the function call
thing1 && thing2
thing2 will only be executed if thing1 runs fine (technically, if it returns a 0 value) to ignore whether foo is working or not, use semicolons instead of &&
В дополнение к
for, вы можете использоватьwatch, как упоминалось в этом вопросе StackOverflow