![]() ![]() ![]() With GNU find (non-embedded Linux, Cygwin), use the -quit primary. You should make find stop as soon as it finds a match, or soon after. This can potentially be inefficient: if there are many files in /var/log/crashes, find will explore them all. You don't need double quotes inside ] (though they don't hurt). In ksh, bash and zsh but not in ash, you can also use ] which has different parsing rules: ] is a different parsing construct. As always, use double quotes around the command substitution, otherwise the output of the find command will be broken into words, and here you'll get a syntax error if there is more than one matching file (and when there are no arguments, is true, whereas you want which is false). The only difference is their name, and the fact that as its last argument. The commands test and are exactly synonymous. Or if test -n "$(find /var/log/crashes -name app-\*\.log -mmin -5)" then I also changed `command` to $(command), which also generally behaves similarly, but is nicer with nested commands. I've used ] in this example, as I do whenever possible. Otherwise it is generally the same as using. If you are using bash (and not sh), you can use ], which behaves more predictably when there are spaces or other special cases in your condition. ![]() n tests for a non-empty string: if ]Ī full list of test arguments is available by invoking help test at the bash commandline. However, all of the above examples only test against the program's exit status, and ignore the program's output.įor find, you will need to test if any output was generated. If /bin/rmdir then echo 'command succeeded' else echo 'command failed' fi # echoes "command failed" because rmdir requires an argument If /bin/echo hi then echo 'command succeeded' else echo 'command failed' fi This can actually be replaced by any program to check its exit status, where 0 indicates success and non-zero indicates failure: # echoes "command succeeded" because echo rarely fails Test returns a zero exit status if the condition is true, otherwise nonzero. Test -x /bin/cat & echo 'cat is executable' ), so you don't want to use & echo 'cat is executable' Not 100% necessary to answer this question, but test is for testing against return codes that commands return? And they are sort of invisible - outside of stdout / stderr? I read the man page but I'm still pretty unclear about when to use test and how to debug it.Do I need the test directive or should I just process against the results of the find command directly, or maybe use find.I've looked at the if flags but I'm not sure which one, if any, that I should use.I'm not sure how to incorporate that into an if statement properly. The find command that I would use is: find /var/log/crashes -name app-\*\.log -mmin -5 I want the if statement to only return true if there is a crash log for a specific app which has been modified in the last 5 minutes. The log files are stored in this format: /var/log/crashes/app-.log I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |