0
#!/bin/bash
# File Count
if (($# < 2))
then
echo "${0}: ERROR: Incorrect number of arguments entered." 1>&2
echo "${0}: USAGE: ${0} <directory> <filename>" 1>&2
exit 1
fi
#checks for a valid number of arguments then exits if the user doesn't provide them and shows an error message.
if [[ ! -d "${1}" ]]
then
echo "${0}: ERROR: directory ${1} does not exist." 1>&2
echo "${0}: USAGE: ${0} <directory> <filename>" 1>&2
exit 2
fi
#checks for a the directory the user specified then exits if the user doesn't provide a valid directory and shows an error message.
if [[ -d "${1}" ]]
then
typeset -i directoryCount=0
for files in $(ls)
do
((directoryCount++))
done
# if it's a directory file add 1 to the directory count
if [[ -x "${1}" ]]
then
typeset -i executableCount=0
for files in $(ls)
do
((executableCount++))
done
# if it's a executable file add 1 to the executable count
if [[ -f "${1}" ]]
then
typeset -i ordinaryCount=0
for files in $(ls)
do
((ordinaryCount++))
done
# if it's a ordinary file add 1 to the ordinary count
echo The number of directory files is "${directoryCount}"
echo The number of executable files is "${executableCount}"
echo The number of ordinary files is "${ordinaryCount}"
#display file counts
雖然我認爲我是沿着正確的線路有麻煩這個程序。任何人都可以提供洞察力?以下是它需要做的事情。謝謝。文件計數程序是不是工作的外殼
- 如果目錄的路徑名爲空,則退出並顯示錯誤消息。
- 如果給出的目錄路徑名沒有命名現有目錄,則退出時出現 錯誤消息。
- 如果無法讀取目錄內容,則退出並顯示錯誤消息。
- 初始化變量,通過目錄中的每個文件命名爲保持三項罪名
- 循環:
- ,如果它是一個目錄就當是這樣的;
- 如果它是可執行計數它是這樣
- 如果它是一個普通的文件並將其視作這樣
- 輸出結果。
'if [[-d「$ {1}」]]'總是會爲真,因爲您已經檢查過相反的情況。不要[解析ls](http://mywiki.wooledge.org/ParsingLs)只使用'for file in *'。這裏的問題是關於如何添加'is executable'邏輯? – 2014-12-02 12:35:10
爲什麼需要第二個從未使用的參數?爲什麼不使用'find'將管道輸出到'wc'? – 2014-12-02 17:46:31
在這些事情中,我總是和這些事情一起奮鬥的邏輯, – user3017497 2014-12-03 01:16:31