【问题标题】:sh -f and -d are being treated as -esh -f 和 -d 被视为 -e
【发布时间】:2016-07-12 02:44:03
【问题描述】:

因此,由于某种原因,在我下面的函数中, 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

【问题讨论】:

  • 试试 shellcheck ,它会自动检测到这样的问题

标签: android bash function shell sh


【解决方案1】:

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

例如

[ -d $line $BACKUP == true ]

缺少两个表达式之间的运算符。应该是这样的

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

【讨论】:

  • 我尝试用 && 强制它,但都没有成功。我在发帖时不小心删除了 && 而没有放回 -a。
  • 准确地说,&amp;&amp; 不是有效的运算符,我在示例中使用了-a
  • 还是无法区分文件夹和文件的区别。带 -a
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 2018-03-28
  • 2016-12-06
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多