在Android中为sh定义数组

时间:2016-07-11 05:33:10

标签: android arrays bash shell sh

我的第一行仍然出错,但我不确定为什么。我假设sh不允许数组,至少不是当前形式。这有解决方法吗?我的整个脚本都依赖于它。任何帮助和/或见解将不胜感激。

这是我的错误,第292行是脚本的第一位:

/tmp/updater: line 297: syntax error: unexpected "("
Updater process ended with ERROR: 2

脚本:

#!/sbin/sh
FILE_PATHS=(
"/main"
)
EXTRACT_DIR="/tmp/dax"
BACKUP_DIR="/data/local/tmp/dax"
CP_LOG="cp.log"
EXEMPT_LOG="exempt.log"
BACKUP_LOG="backup.log"
BUILD_VERSION=`grep "^ro\.build\.fingerprint" /system/build.prop`
FLASHABLE_VERSION="0.9.1"
EXCLUDE=(
"/system/addon.d"
"/system/app"
"/system/etc/init.d"
"/su.d"
)

BACKUP=true
if [[ -d $BACKUP_DIR ]] then
    if [[ $BUILD_VERSION == `cat $BACKUP_DIR"rom_version.txt"` && $FLASHABLE_VERSION == `cat $BACKUP_DIR"flashable_version.txt"` ]] then
        BACKUP=false
        RESTORE=true
    else 
        # Build Versions don't match, get rid of the backup and let it rebuild
        rm -rf $BACKUP_DIR
    fi
fi

function restoreBackup () {
    while IFS= read -r line
    do
    if [[ -f $line ]] then
        echo rm $line
        if [[ $line == `grep "^$line$" $BACKUP_DIR$BACKUP_LOG` ]] then
            cp_perm 0 0 0755 $BACKUP_DIR$line $line
        fi
    elif [[ -d $line ]] then
        # This is where you would need to do mkdir and such
        if [[ $line == `grep "^$line$" $BACKUP_DIR$BACKUP_LOG` ]] then
            mkdir -p $line
            cp_perm 0 0644 BACKUP_DIR$line $line
        fi
    fi
    done < $1
}

if [[ $1 == "uninstall" ]] then
    restoreBackup $BACKUP_DIR$CP_LOG
    rm -rf $BACKUP_DIR
    exit
fi

if [[ $RESTORE == 1 ]] then
    restoreBackup $BACKUP_DIR$CP_LOG
fi

mkdir -p $BACKUP_DIR
function checkExclusions () {
    if [[ -f $1 ]] then
        DNAME=`dirname $1`
    fi

    exists=0
    for match in "${EXCLUDE[@]}"
    do
        if [[ $1 == $match || $DNAME == $match ]] then
            exists=1
        fi
    done
    return $exists
}

function populateLog () { 
    for i in `find $1 -type d`; do
            checkExclusions  $i
            if [[ $? == 0 ]] then
                echo ${i#$EXTRACT_DIR} >> $CP_LOG
            elif [[ $? == 1 ]] then
                echo ${i#$EXTRACT_DIR} >> $EXEMPT_LOG
            fi
    done
    for i in `find $1 -type f`; do
            # Check that the file isn't in an exempt path
            checkExclusions  $i
            if [[ $? == 0 ]] then
                echo ${i#$EXTRACT_DIR} >> $CP_LOG
            elif [[ $? == 1 ]] then
                echo ${i#$EXTRACT_DIR} >> $EXEMPT_LOG
            fi
    done
}

function readLog () {
    if [[ $BACKUP == true ]] then
        echo $BUILD_VERSION > $BACKUP_DIR"rom_version.txt"
        echo $FLASHABLE_VERSION > $BACKUP_DIR"flashable_version.txt"
    fi
    while IFS= read -r line
    do
    if [[ -f $EXTRACT_DIR$line ]] then
        if [[ -f $EXTRACT_DIR$line && $BACKUP == true ]] then
            # Backup the files if they exist. Permissions don't matter here.
            cp $line $BACKUP_DIR$line
            echo $line >> $BACKUP_LOG
        fi
        cp_perm 0 0 0755 $EXTRACT_DIR$line $line
    elif [[ -d $EXTRACT_DIR$line ]] then
        if [[ -d $line && $BACKUP == true ]] then
            # Backup the Directories if they exist
            mkdir -p $BACKUP_DIR$line
            echo $line >> $BACKUP_LOG
        fi  
        mkdir -p $line
        cp_perm 0 0644 $EXTRACT_DIR$line $line
    fi
    done < $1
}

for mpath in "${FILE_PATHS[@]}"
    do
    populateLog $mpath
done

readLog $CP_LOG

# Backup the $CP_LOG so that we can remove files we added during an uninstall
sort -o $BACKUP_DIR$CP_LOG $CP_LOG
sort -o $BACKUP_DIR$EXEMPT_LOG $EXEMPT_LOG
sort -o $BACKUP_DIR$BACKUP_LOG $BACKUP_LOG
rm *.log

1 个答案:

答案 0 :(得分:4)

不幸的是,你做不到。数组是Bash功能,Android只提供与POSIX兼容的sh

我会这样做:

EXCLUDE="/system/addon.d /system/app /system/etc/init.d /su.d"

function checkExclusions () {
    if [ -f "$1" ]; then
        DNAME=`dirname $1`
    fi

    exists=0
    for match in $EXCLUDE; do
        if [ "x$1" == "x$match" ] || [ "x$DNAME" == "x$match" ]; then
            exists=1
        fi
    done

    return $exists
}