2016-07-12 26 views
0

因此,出于某种原因,在我的下面的函数中,if [-f]和if [-d]位被破坏。不知何故,sh/android同时将-f和-d视为-e,这导致我的脚本在Android恢复中运行脚本时无法正常工作。我想知道有没有人对此有所了解?我试图强迫它与& &,但无济于事。sh -f和-d被视为-e

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 
+0

尝试[shellcheck(http://shellcheck.net),它autodetecta这样 –

回答

1

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

例如

[ -d $line $BACKUP == true ] 

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

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

我试着用&&迫使它的问题,但是没有奏效。当我发布时,我不小心删除了&&而没有将-a放回去。 –

+0

准确地说,'&&'不是一个有效的运算符,在我的例子中我使用了'-a'。 –

+0

它仍然无法区分文件夹和文件之间的区别。与-a –

相关问题