2016-07-12 48 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 –

相關問題