sh -f和-d被视为-e

时间:2016-07-12 02:44:03

标签: android bash function shell sh

因此,出于某种原因,在我的下面的函数中,if [-f]和if [-d]位被破坏了。不知怎的,sh / android同时将-f和-d视为-e,这导致我的脚本在android恢复中运行脚本时无法正常运行。我想知道是否有人对此有任何见解?我尝试使用&&和强制它,但无济于事。

readLog () {
    ui_print "Readlog $1"
    ui_print "AddPath $2"
    ADDDIR="$2"
    if [[ $BACKUP == true ]]
    then
        echo $BUILD_VERSION > $BACKUP_DIR"rom_version.txt"
        echo $FLASHABLE_VERSION > $BACKUP_DIR"flashable_version.txt"
    fi
    while IFS= read line
    do
    MPATH=${ADDDIR}$line
    ui_print "Line: $line"
    if [ -d $MPATH && ! -f $MPATH]
    ui_print "Dir: $MPATH"
    then
        if [ -d $line && $BACKUP == true ]
        then
            ui_print "Dir $line Exists, Back it up"
            # Backup the Directories if they exist
            mkdir -p $BACKUP_DIR$line
            echo $line >> $BACKUP_LOG
        fi  
        mkdir -p $line
        cp_perm 0 0644 ${ADDDIR}$line $line

    elif [ -f $MPATH && ! -d $MPATH ]
    ui_print "File: $MPATH"
    then
        if [ -f $line && $BACKUP == true ]
        then
            ui_print "File $line Exists, Back it up"
            # 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 ${ADDDIR}$line $line

    fi
    done < $1
}

这是完整的脚本:

FILE_PATH="/root"
EXTRACT_DIR="/tmp/test"
BACKUP_DIR="/data/local/tmp/test"
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="/data/local/exclude1.sh /data/local/exclude2.sh"

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

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
checkExclusions () {
    if [ -f "$1" ];
    then
        DNAME=`dirname ${1#$2}`
    fi

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

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

readLog () {
    ui_print "Readlog $1"
    ui_print "AddPath $2"
    ADDDIR="$2"
    if [[ $BACKUP == true ]]
    then
        echo $BUILD_VERSION > $BACKUP_DIR"rom_version.txt"
        echo $FLASHABLE_VERSION > $BACKUP_DIR"flashable_version.txt"
    fi
    while IFS= read line
    do
    MPATH=${ADDDIR}$line
    ui_print "Line: $line"
    if [ -d $MPATH && ! -f $MPATH]
    ui_print "Dir: $MPATH"
    then
        if [ -d $line && $BACKUP == true ]
        then
            ui_print "Dir $line Exists, Back it up"
            # Backup the Directories if they exist
            mkdir -p $BACKUP_DIR$line
            echo $line >> $BACKUP_LOG
        fi  
        mkdir -p $line
        cp_perm 0 0644 ${ADDDIR}$line $line

    elif [ -f $MPATH && ! -d $MPATH ]
    ui_print "File: $MPATH"
    then
        if [ -f $line && $BACKUP == true ]
        then
            ui_print "File $line Exists, Back it up"
            # 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 ${ADDDIR}$line $line

    fi
    done < $1
}

populateLog ${EXTRACT_DIR}$FILE_PATH

# 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

1 个答案:

答案 0 :(得分:1)

您应该检查测试([)命令的语法。

例如

[ -d $line $BACKUP == true ]

缺少2个表达式之间的运算符。它应该像

[ -d "$line" -a "$BACKUP" == true ]