2016-03-15 86 views
0

我寫了一個函數,詢問用戶路徑並驗證它;隨後顯示用戶輸入路徑的驅動器的特定分區的HDD使用情況/可用空間。我不知道爲什麼我收到錯誤使用用戶的路徑輸入檢查HDD分區空間

代碼 -

funcHDD() { 
     read -p "Enter Path: " Path 
     if [ -d $Path ]; then 
       echo "Choose option from menu - HDD details:" 
       echo "1. Used" 
       echo "2. Available" 
       read -p "Select: " HddUseAva 
       tdisk=$(df -h "${Path}" | awk '{print $2}' | tail -1) 
       udisk=$(df -h "${path}" | awk '{print $3}' | tail -1) 
       adisk=$(df -h "${path}" | awk '{print $4}' | tail -1) 
       fsys=$(df -h "${path}" | awk '{print $1}' | tail -1) 
       if [ $HddUseAva = 1 ]; then 
         echo "Current used space in $fsys: $udisk (Total: $tdisk)" 
       elif [ $HddUseAva = 2 ]; then 
         echo "Current available space in $fsys: $adisk (Total: $tdisk)" 
       elif echo $HddUseAva | grep -iq 'Done'; then 
         exit 
       elif echo $HddUseAva | grep -iq 'Exit'; then 
         exit 
       else 
         funcHDD 
       fi 
     else 
       echo "Invalid Path" 
       funcHDD 
     fi 
     } 

錯誤 -

Enter Path: /home 
Choose option from menu - HDD details: 
1. Used 
2. Available 
Select: 1 
df: ‘’: No such file or directory 
df: ‘’: No such file or directory 
df: ‘’: No such file or directory 
Current used space in : (Total: 20G) 
+1

你做了什麼錯誤? – Raju

+0

@Raju在主體中添加了錯誤 – PythonCobra

回答

0

您使用兩個不同的變量:$path$Path,但$path是不確定的。

替換:

udisk=$(df -h "${path}" | awk '{print $3}' | tail -1) 
adisk=$(df -h "${path}" | awk '{print $4}' | tail -1) 
fsys=$(df -h "${path}" | awk '{print $1}' | tail -1) 

有:

udisk=$(df -h "${Path}" | awk '{print $3}' | tail -1) 
adisk=$(df -h "${Path}" | awk '{print $4}' | tail -1) 
fsys=$(df -h "${Path}" | awk '{print $1}' | tail -1) 
+0

我不確定爲什麼我不能使用〜作爲路徑 – PythonCobra

+0

'〜'在腳本中被視爲字符串。這裏有一個warkaround:http://stackoverflow.com/questions/3963716/how-to-manually-expand-a-special-variable-ex-tilde-in-bash – SLePort