使用用户输入的路径检查硬盘分区空间

时间:2016-03-15 14:04:17

标签: regex bash shell unix scripting

我写了一个函数,询问用户路径并验证它;随后显示用户输入路径的驱动器的特定分区的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 个答案:

答案 0 :(得分: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)

with:

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)