quanguoheme 发表于 2013-07-05 14:54

只能正常运行一次的shell脚本,重启也一样【求助】

本帖最后由 quanguoheme 于 2013-07-05 19:29 编辑

我有一个shell脚本只能正常运行一次。第二次运行后,就不对了 。求助,脚本哪一个语句有问题。
脚本使用方法:sudo ./fsl-sdcard-partition.sh /dev/sdb
脚本是用来对sd卡进行格式化的。/dev/sdb代表我的sd卡。第一次运行可以,正常格式化,是可以实现在sd卡前面保留一个2mb作为u-boot.bin作为区域的。
脚本第二次运行,格式化不正常,不可以实现在sd卡前面保留一个2mb区域。
脚本内容:#!/bin/bash

# partition size in MB
BOOTLOAD_RESERVE=8
BOOT_ROM_SIZE=8
SYSTEM_ROM_SIZE=512
CACHE_SIZE=512
RECOVERY_ROM_SIZE=8
VENDER_SIZE=8
MISC_SIZE=8

help() {

bn=`basename $0`
cat << EOF
usage $bn <option> device_node

options:
-h                                displays this help message
-s                                only get partition size
-np                                 not partition.
-f                                 flash android image.
EOF

}

# check the if root?
userid=`id -u`
if [ $userid -ne "0" ]; then
        echo "you're not root?"
        exit
fi


# parse command line
moreoptions=1
node="na"
cal_only=0
flash_images=0
not_partition=0
not_format_fs=0
while [ "$moreoptions" = 1 -a $# -gt 0 ]; do
        case $1 in
          -h) help; exit ;;
          -s) cal_only=1 ;;
          -f) flash_images=1 ;;
          -np) not_partition=1 ;;
          -nf) not_format_fs=1 ;;
          *)moreoptions=0; node=$1 ;;
        esac
        [ "$moreoptions" = 0 ] && [ $# -gt 1 ] && help && exit
        [ "$moreoptions" = 1 ] && shift
done

if [ ! -e ${node} ]; then
        help
        exit
fi


# call sfdisk to create partition table
# get total card size
seprate=40
total_size=`sfdisk -s ${node}`
total_size=`expr ${total_size} / 1024`
boot_rom_sizeb=`expr ${BOOT_ROM_SIZE} + ${BOOTLOAD_RESERVE}`
extend_size=`expr ${SYSTEM_ROM_SIZE} + ${CACHE_SIZE} + ${VENDER_SIZE} + ${MISC_SIZE} + ${seprate}`
data_size=`expr ${total_size} - ${boot_rom_sizeb} - ${RECOVERY_ROM_SIZE} - ${extend_size} + ${seprate}`

# create partitions
if [ "${cal_only}" -eq "1" ]; then
cat << EOF
BOOT   : ${boot_rom_sizeb}MB
RECOVERY: ${RECOVERY_ROM_SIZE}MB
SYSTEM : ${SYSTEM_ROM_SIZE}MB
CACHE: ${CACHE_SIZE}MB
DATA   : ${data_size}MB
MISC   : ${MISC_SIZE}MB
EOF
exit
fi

function format_android
{
    echo "formating android images"
    mkfs.ext4 ${node}4 -Ldata
    mkfs.ext4 ${node}5 -Lsystem
    mkfs.ext4 ${node}6 -Lcache
    mkfs.ext4 ${node}7 -Lvender
}



if [[ "${not_partition}" -eq "1" && "${flash_images}" -eq "1" ]] ; then
    flash_android
    exit
fi


# destroy the partition table
dd if=/dev/zero of=${node} bs=1024 count=1

sfdisk --force -uM ${node} << EOF
,${boot_rom_sizeb},83
,${RECOVERY_ROM_SIZE},83
,${extend_size},5
,${data_size},83
,${SYSTEM_ROM_SIZE},83
,${CACHE_SIZE},83
,${VENDER_SIZE},83
,${MISC_SIZE},83
EOF

# adjust the partition reserve for bootloader.
# if you don't put the uboot on same device, you can remove the BOOTLOADER_ERSERVE
# to have 8M space.
# the minimal sylinder for some card is 4M, maybe some was 8M
# just 8M for some big eMMC 's sylinder
sfdisk --force -uM ${node} -N1 << EOF
${BOOTLOAD_RESERVE},${BOOT_ROM_SIZE},83
EOF

# format the SDCARD/DATA/CACHE partition
part=""
echo ${node} | grep mmcblk > /dev/null
if [ "$?" -eq "0" ]; then
        part="p"
fi

format_android
页: [1]
查看完整版本: 只能正常运行一次的shell脚本,重启也一样【求助】