我在寫一個shell腳本。我的腳本進入一個目錄,但我希望它只能繼續執行下一個命令,如果目錄包含任何數據(該目錄不是空的)。否則,它不應該進一步。我如何在shell腳本中指定這樣的條件?如何在ash shell腳本中指定「if directory not empty」?
2
A
回答
0
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
fi
1
if [ `find dir -type f | wc -l` = "0" ]
then
echo no files
else
echo files
fi
這會檢查dir
和子目錄。 find dir -maxdepth 1 -type f
將只檢查dir
如果要計算子目錄和文件:
find dir -mindepth 1 -maxdepth 1
+1
而不是管道到wc -l,它使用grep更清潔:如果找到dir -type f | grep -q。;然後 ...; fi – 2011-05-05 03:15:24
3
使用測試以 「$(LS-A $ DIR)」,如:
if [ "$(ls -A $DIR)" ]; then
echo "directory is not empty"
else
echo "directory is Empty"
fi
+0
+1請注意,'-A'不是POSIX,但在GNU下也支持BSD引入的擴展。 – pilcrow 2013-05-24 14:24:40
相關問題
- 1. 是什麼導致「Directory not empty」錯誤?
- 2. shell腳本問題「if [-d $ {directory}]」
- 3. OpenWRT ASH腳本
- 4. Shell腳本-directory compare-輸出
- 5. ASH if if語句中的if語句
- 6. umbraco - masterpage:「if field is not empty then show field」
- 7. shell腳本:if語句
- 8. 如何在shell腳本中使用elif中的if語句?
- 9. 如何在shell腳本
- 10. 如何在Makefile中指定--no-print-directory
- 11. 如何在指定時間後終止shell腳本
- 12. Shell腳本:在if語句中定義一個變量
- 13. 的if-else在shell腳本錯誤
- 14. 錯誤在Bash Shell腳本IF聲明
- 15. 如何定義在shell腳本變種
- 16. SQL/MySQL NOT NULL與NOT EMPTY
- 17. 如何使用shell腳本
- 18. 如何在configure腳本中指定makeargs?
- 19. 如何在Bamboo OnDemand中指定腳本?
- 20. if(!empty)issues
- 21. jQuery if div empty
- 22. Php empty not working using POST
- 23. 如何使用shell腳本中的if else
- 24. 管道到Ash Shell中的命令
- 25. 如何在shell腳本中顯示進度指示器功能?
- 26. shell腳本的if語句麻煩
- 27. Django - 如何在'limit_choices_to'中指定'NOT IN'?
- 28. Shell腳本:RegEx in if語句
- 29. 如何將stdin從shell腳本重定向到shell腳本中的命令?
- 30. $?在shell腳本
可能重複[檢查目錄是否包含文件](http://stackoverflow.com/questions/91368/checking-if-a-directory-contains-files) – 2013-09-11 19:00:11