用for循环BASH递归

时间:2015-06-05 18:11:42

标签: linux bash loops for-loop

我还有一个问题。这段代码是针对Linux发行版中的rcS脚本而制作的,并且由于某种原因偶尔会出现段错误。大约85%的时间它工作得很好,一切都很花花公子。但是,有15%的时间它会在创建目录或执行挂载时出现段错误。

    # Find and mount all detected drives sda-sdm and partitions 0-10, you can change these by changing the values but odds are you won't have more than 26 USB drives plugged in or 10 partitions ;)
echo "  ${YELLOW}[ ${BLUE}*${YELLOW} ] Finding and mounting the program drive"
for DRIVE in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
    mkdir "/mnt/sd${DRIVE}/"
    mount "/dev/sd${DRIVE}" "/mnt/sd${DRIVE}/" 2> /dev/null
    if [ ! "${?}" = '0' ]; then
        rmdir "/mnt/sd${DRIVE}/"
    fi
    for PARTITION in 1 2 3 4 5 6 7 8 9 10; do
        mkdir "/mnt/sd${DRIVE}${PARTITION}"
        mount "/dev/sd${DRIVE}${PARTITION}" "/mnt/sd${DRIVE}${PARTITION}/" 2> /dev/null
        if [ ! "${?}" = '0' ]; then
            rmdir "/mnt/sd${DRIVE}${PARTITION}/"
        fi
        if [ -d /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH} ]; then
            if [ ! "${PROGRAM_DRIVE_FOUND}" = 'YES' ]; then
                PROGRAM_DRIVE_FOUND='YES'
                echo "${MESG} Found program drive on device sd${DRIVE}${PARTITION} mounted at /mnt/sd${DRIVE}${PARTITION}"
                source /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/os_info
                rm "/etc/init.d/os_info"
                cp /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/os_info "/etc/init.d/os_info"

                echo "${MESG} Adding user ${USER}"
                /bin/adduser -s /bin/bash -G root -D "$USER"
                echo "${USER}:password" | /usr/sbin/chpasswd -m > /dev/null
                echo -e "${USER}\tALL=NOPASSWD: ALL" >> /etc/sudoers

                mount -o loop /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/home "/home/${USER}/" 2> /dev/null
                rmdir "/home/${USER}/lost+found/" 2> /dev/null
                chmod 770 "/home/${USER}/"
                if [ ! "${?}" = '0' ]; then
                    echo "${MESG} Error mounting home on program drive! 'home' file gone or corrupt."
                    echo "${RED}SYSTEM BROKEN, PRESS ANY KEY TO SHUTDOWN:"
                    read -sn 1
                    poweroff
                fi
            fi
        fi
    done
done

0 个答案:

没有答案