我怎么能测试一个命令输出空字符串?
How can I test if a command outputs an empty string?
推荐答案命令不的收益的价值观 - 他们将其输出。您可以通过使用一个子shell捕获该输出;例如 $(LS -A)。您可以测试在猛砸一个非空字符串是这样的:
Commands don’t return values – they output them. You can capture this output by using a subshell; e.g. $(ls -A). You can test for a non-empty string in Bash like this:
if [[ $(ls -A) ]]; then echo "there are files" else echo "no files found" fi请注意,我已经使用 -A ,而不是 -a ,因为它忽略了象征性的电流(。)和父( .. )的目录项。
Note that I've used -A rather than -a, since it omits the symbolic current (.) and parent (..) directory entries.
如果您要检查该命令成功完成,您可以检查 $?,其中包含的最后一个命令的退出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:
files=$(ls -A) if [[ $? != 0 ]]; then echo "Command failed." elif [[ $files ]]; then echo "Files found." else echo "No files found." fi更多信息这里。
更多推荐
测试如果命令输出为空字符串
发布评论