Ahrion Gallegos
Ahrion Gallegos

Reputation: 65

Defining an array in sh for Android

I keep getting an error for my first line, but I'm not sure why exactly. I'm assuming that sh doesn't allow array, at least not in the current form. Is there a workaround for this? My entire script depends on it. Any help and/or insight would be greatly appreciated.

Here's my error, line 292 is the first bit of the script:

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

Script:

#!/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

Upvotes: 3

Views: 372

Answers (1)

Will
Will

Reputation: 24699

Unfortunately, you cannot. Arrays are a Bash feature, and Android only offers a POSIX-compatible sh.

I would do it like this:

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
}

Upvotes: 4

Related Questions