How can I test if a command outputs an empty string?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
This will run the command and check whether the returned output (string) has a zero length. You might want to check the 'test' manual pages for other flags.
Use the "" around the argument that is being checked, otherwise empty results will result in a syntax error as there is no second argument (to check) given!
Note: that
ls -la
always returns.
and..
so using that will not work, see ls manual pages. Furthermore, while this might seem convenient and easy, I suppose it will break easily. Writing a small script/application that returns 0 or 1 depending on the result is much more reliable!Bash Reference Manual
6.4 Bash Conditional Expressions
You can use shorthand version:
Sometimes you want to save the output, if it's non-empty, to pass it to another command. If so, you could use something like
This way, the
rm
command won't hang if the list is empty.Here's an alternative approach that writes the std-out and std-err of some command a temporary file, and then checks to see if that file is empty. A benefit of this approach is that it captures both outputs, and does not use sub-shells or pipes. These latter aspects are important because they can interfere with trapping bash exit handling (e.g. here)
Previously, the question asked how to check whether there are files in a directory. The following code achieves that, but see rsp's answer for a better solution.
Empty output
Commands don’t return values – they output them. You can capture this output by using command substitution; e.g.
$(ls -A)
. You can test for a non-empty string in Bash like this:Note that I've used
-A
rather than-a
, since it omits the symbolic current (.
) and parent (..
) directory entries.Note: As pointed out in the comments, command substitution doesn't capture trailing newlines. Therefore, if the command outputs only newlines, the substitution will capture nothing and the test will return false. While very unlikely, this is possible in the above example, since a single newline is a valid filename! More information in this answer.
Exit code
If you want to check that the command completed successfully, you can inspect
$?
, which contains the exit code of the last command (zero for success, non-zero for failure). For example:More info here.
As Jon Lin commented,
ls -al
will always output (for.
and..
). You wantls -Al
to avoid these two directories.You could for example put the output of the command into a shell variable:
An older, non-nestable, notation is
but I prefer the nestable notation
$(
...)
The you can test if that variable is non empty
And you could combine both as
if [ -n "$(ls -Al)" ]; then