文件系统磁盘使用情况脚本

时间:2019-10-18 00:46:31

标签: linux bash

我有一个选择菜单脚本来显示顶级文件系统磁盘使用情况。它正在运行脚本,但是文件系统之一命名为/ data时出现问题。

DATA_RAW_DISK_DUMP="/tmp/$(basename $0).$$.disk_raw_data" 
DATA_RAW_FILE_LIST="/tmp/$(basename $0).$$.file_raw_data"

MY_WORKING_DIR=$(pwd)
MY_OUTPUT_FILE="MY_WORKING_DIR/$(basename $0).output"

#check existing output file
if [[ -e $MY_OUTPUT_FILE ]] ; then
        if [[ -e ${MY_OUTPUT_FILE}.old ]] ; then
                rm ${MY_OUTPUT_FILE}.old
        fi
        mv $MY_OUTPUT_FILE ${MY_OUTPUT_FILE}.old
fi

#will prompt for choices - ignore any arguments
echo "Please select from below:" cat<<EOF
1. /data
2. /data01
3. DO_ALL_DATA_AT_ONCE

Press ENTER to exit EOF

echo -n "Selection:" 
read ans
if [[ $ans == "" ]] ; then
        echo "Bye"
        exit 0
fi

case $ans in
        "1"|"2"|"3")
                FS_TARGET="$ans"
                ;;
        *)
                echo "Unknown option - quitting"
                echo "Bye"
                exit 0
                ;;
esac

DATA="/data0${FS_TARGET}/*"

if [[ $FS_TARGET -eq 3 ]] ; then
        #if the selection is 3, then we just do everything, in one go
        DATA="/data/* /data01/*"
fi

#do the du first
echo "Running du for ${DATA}, this will take a while, please wait..." >&2
du -sk ${DATA} > $DATA_RAW_DISK_DUMP 2>/dev/null

#we need to check each entry, if its directory, go into it and get the youngest file
echo "Checking file age for ${DATA}, this will take a while, please wait..." >&2
for i in $(cat $DATA_RAW_DISK_DUMP | awk '{print $2}') ; do
        if [[ -d $i ]] ; then
                #we need to find the youngest file in this directory tree
                YOUNGEST_FILE="$(find $i -type f -printf "%C@ %p\n" | sort -rn | head -n 1|sed 's,[0-9]*[.][0-9]*[ ],,g')"
                if [[ $YOUNGEST_FILE == "${i}" ]] ; then
                        stat --format "%n;%Y;%X;%U" "$i" >> $DATA_RAW_FILE_LIST 2>/dev/null
                elif [[ $YOUNGEST_FILE == "" ]] ; then
                        stat --format "%n;%Y;%X;%U" "$i" >> $DATA_RAW_FILE_LIST 2>/dev/null
                else
                        stat --format "%n;%Y;%X;%U" "$YOUNGEST_FILE" >> $DATA_RAW_FILE_LIST 2>/dev/null
                fi
        else
                stat --format "%n;%Y;%X;%U" "$i" >> $DATA_RAW_FILE_LIST 2>/dev/null
        fi
done

上面的脚本,选项2和3可以正常工作,但是选项1却什么也没有显示。然后,我尝试通过向脚本中添加另一个变量来尝试另一种方式,但效果不佳。有人可以告诉我如何使其读取我的文件系统/ data吗?

echo "Please select from below:"
cat<<EOF
1. /data
2. /data01
3. DO_ALL_DATA_AT_ONCE

Press ENTER to exit
EOF

echo -n "Selection:"
read ans
if [[ $ans == "" ]] ; then
        echo "Bye"
        exit 0
fi

case $ans in
        "1"|"2"|"3")
                FS_TARGET="$ans"
                ;;
        *)
                echo "Unknown option - quitting"
                echo "Bye"
                exit 0
                ;;
esac


DATA="/data0${FS_TARGET}/*"

if [[ $FS_TARGET -eq 1 ]] ; then
    #if the selection is 1, then change do the variable....
    DATA="/data/*"
fi

if [[ $FS_TARGET -eq 3 ]] ; then
    #if the selection is 3, then we just do everything, in one go
    DATA="/data/* /data01/*"
fi

0 个答案:

没有答案