Bash 脚本不会更改目录

Bash 脚本不会更改目录

首先,我没有任何编程背景,只是将其作为业余爱好。我的意思是:这是一种“爱好”,我这样做是为了好玩。我从 6 周前开始,这里所有类似脚本的内容都是互联网研究和/或查看代码并弄清楚运行 set -xe 时会发生什么加上反复试验方法的混合,所以不要指望过于出色的编码和我问“愚蠢”的问题。

所以我有一个脚本,它可以让你:

  1. 选择一个(必需:系统分区)或两个(可选:数据分区)flashfire (android) 备份,
  2. 导出路径并创建六个文件夹(project_folder、project_folder/rom、project_folder/rom/data、projevt_folder/rom/system 和另外两个)
  3. 将备份的各个部分(分割为 1.07GB)重新加入数据或系统文件夹中,然后
  4. 将目录更改为 .../system 或 .../data 并提取 data 或 system 文件夹内的备份

除了将目录更改为project_folder/rom/data 之外,这一切都运行得很好。

它总是说:

cd: ./project_folder/rom/data: No such file or directory

有趣的是,project_folder/rom/system 可以工作,并且将备份重新加入数据文件夹也可以工作。为什么当脚本更改到数据文件夹时更改目录不起作用?

这是不起作用的部分(系统起作用,数据不起作用):

EXT_SYS() {
# extract backups
echo "Extracting :"

echo "System partition backup..."
cd "$ACTIVE_DB/rom/system"
lz4 -dc --no-sparse system.tar.lz4 | tar xf -
}

EXT_DATA() {
# extract backups
echo "Data partition backup..."
cd "$ACTIVE_DB/rom/data"
lz4 -dc --no-sparse data.tar.lz4 | tar xf -
}

和其余的

#!/bin/bash

PRINT_LINE3
echo "To create a DB make sure that you have:"
echo
echo "[REQUIRED] system.tlz4 and all system.*.tlz4 "
echo "[OPTIONAL] data.tlz4 and all data.*.tlz4"    
echo "in $BASE_DIR"
echo

local READY=0
while [[ $READY -eq 0 ]]; do
    echo
    echo "Please fill a path for your system.tlz4 file"
    echo "Potential candidates:"
    echo "=-=-=-=-=-=-=-=-="
    find ./base_drops -type f -iname "system.tlz4"
    echo "=-=-=-=-=-=-=-=-="
    read -p "system.tlz4: " SYSTEM_BAK

    echo
    echo "Please fill a path for your data.tlz4 file. Leave empty if none."
    echo "Potential candidates:"
    echo "=-=-=-=-=-=-=-=-="
    find ./base_drops -type f -iname "data.tlz4"
    echo "=-=-=-=-=-=-=-=-="
    read -p "data.tlz4: " DATA_BAK

    if [[ -f "$SYSTEM_BAK" ]] && ([[ -z "$DATA_BAK" || -f "$DATA_BAK" ]]); then
        READY=1
    else
        echo "Some of your files do not exist"
        PRESS_ENTER
    fi
done
# creates db dir plus subdirs if needed
PRINT_LINE3
read -p "Name your database: " NAME
echo
echo "backups: "
if [ -f  "$SYSTEM_BAK" ] ; then
    echo "  [SYS]  -> $SYSTEM_BAK"
fi
if [ -f  "$DATA_BAK" ] ; then
    echo "  [DATA] -> $DATA_BAK"
else
    echo "  no DATA "
fi
PRINT_LINE1
export ACTIVE_DB="./DB_$NAME"
export LIST_DIR="$ACTIVE_DB/lists"
export LOGS_DIR="$ACTIVE_DB/logs"
export ROM_DIR="$ACTIVE_DB/rom"
export SYS_DIR="$ACTIVE_DB/rom/system"
if [[ ! -z ${DATA_BAK+x} ]] ; then
    export DATA_DIR="$ACTIVE_DB/rom/data"
fi
echo "Creating $ACTIVE_DB, please wait..."
mkdir -p "$ACTIVE_DB"
mkdir -p "$LIST_DIR"
mkdir -p "$LOGS_DIR"
mkdir -p "$SYS_DIR"
if [[ ! -z "$DATA_BAK" ]] ; then  
   mkdir -p "$DATA_DIR"
fi
# SYSTEM
# find backup parts
echo "Merging: "
echo -n "System partition "
if [[ -f "./base_drops/system.0004.tlz4" ]] ; then
   export SYSTEM_BAK4="./base_drops/system.0004.tlz4"
   export SYSTEM_BAK3="./base_drops/system.0003.tlz4"
   export SYSTEM_BAK2="./base_drops/system.0002.tlz4" 
   cat "$SYSTEM_BAK" "$SYSTEM_BAK2" "$SYSTEM_BAK3" "$SYSTEM_BAK4" > "$ACTIVE_DB/rom/system/system.tar.lz4"        
   echo "(4 parts) -> $SYSTEM_BAK $SYSTEM_BAK2 $SYSTEM_BAK3 $SYSTEM_BAK4"    
else
    if [[ -f "./base_drops/system.0003.tlz4" ]] ; then
        export SYSTEM_BAK3="./base_drops/system.0003.tlz4"
        export SYSTEM_BAK2="./base_drops/system.0002.tlz4"  
        cat "$SYSTEM_BAK" "$SYSTEM_BAK2" "$SYSTEM_BAK3" > "$ACTIVE_DB/rom/system/system.tar.lz4"
        echo "(3 parts) -> $SYSTEM_BAK $SYSTEM_BAK2 $SYSTEM_BAK3"    
    else
        if [[ -f "./base_drops/system.0002.tlz4" ]] ; then
            export SYSTEM_BAK2="./base_drops/system.0002.tlz4"  
            cat "$SYSTEM_BAK" "$SYSTEM_BAK2" > "$ACTIVE_DB/rom/system/system.tar.lz4"
            echo "(2 parts) -> $SYSTEM_BAK $SYSTEM_BAK2"    
        fi
    fi
fi    
# /DATA-Partition backup
if [[ ! -z ${DATA_BAK+x} ]] ; then
echo -n  "Data partition:"
    if [[ -f "./base_drops/data.0004.tlz4" ]] ; then       # check for backup split-archive parts
        export DATA_BAK4="./base_drops/data.0004.tlz4"
        export DATA_BAK3="./base_drops/data.0003.tlz4"
        export DATA_BAK2="./base_drops/data.0002.tlz4"
        cat "$DATA_BAK" "$DATA_BAK2" "$DATA_BAK3" "$DATA_BAK4" > "$ACTIVE_DB/rom/data/data.tar.lz4"        # join into one archive
        echo "(4 parts) -> $DATA_BAK $DATA_BAK2 $DATA_BAK3 $DATA_BAK4"    
    else 
        if [[ -f "./base_drops/data.0003.tlz4" ]] ; then
            export DATA_BAK3="./base_drops/data.0003.tlz4"
            export DATA_BAK2="./base_drops/data.0002.tlz4"
            cat "$DATA_BAK" "$DATA_BAK2" "$DATA_BAK3" > "$ACTIVE_DB/rom/data/data.tar.lz4"
            echo "(3 parts) -> $DATA_BAK $DATA_BAK2 $DATA_BAK3"    
        else
            if [[ -f "./base_drops/data.0002.tlz4" ]] ; then
                export DATA_BAK2="./base_drops/data.0002.tlz4"
                cat "$DATA_BAK" "$DATA_BAK2" > "$ACTIVE_DB/rom/data/data.tar.lz4"
                echo "(2 parts) -> $DATA_BAK $DATA_BAK2"    
            fi
        fi
    fi
fi
EXT_SYS
EXT_DATA
# clean up
cd "$MAIN_DIR"
if [[ ! -z ${DATA_BAK+x} ]] ; then
    rm -f "$ACTIVE_DB/rom/data/data.tar.lz4"
    rm -f "$DATA_BAK1" 
    rm -f "$DATA_BAK2" 
    rm -f "$DATA_BAK3" 
    rm -f "$DATA_BAK4"
    rm -f "$ACTIVE_DB/rom/system/system.tar.lz4"
    rm -f "$SYSTEM_BAK1" 
    rm -f "$SYSTEM_BAK2" 
    rm -f "$SYSTEM_BAK3" 
    rm -f "$SYSTEM_BAK4"
else        
    rm -f "$ACTIVE_DB/rom/system/system.tar.lz4"
    rm -f "$SYSTEM_BAK1" 
    rm -f "$SYSTEM_BAK2" 
    rm -f "$SYSTEM_BAK3" 
    rm -f "$SYSTEM_BAK4"  
fi
echo "."
PRESS_ENTER

答案1

有多种选择;我倾向于使用子shell

( cd some/dir && do_work )
( cd some/other/dir && do_more_work )

因为pwd只会在子 shell 内更改,而不会在父进程中更改。另一个选项是pushd/ popd

pushd some/dir
do_work
popd
pushd some/other/dir
do_even_more_work

还有一种方法是使用完全限定的路径,它的好处是不关心以前的工作目录在哪里,尽管可以将代码与特定的文件系统层次结构联系起来:

WORKDIR=/blah/de/blah
cd $WORKDIR/some/dir && do_work
cd $WORKDIR/some/other/dir && you_get_the_idea

错误检查也很方便,因为cd可能会失败,然后会发生什么?始终检查是否cd发生了(&& ...测试)或用于set -e避免任何失败。

相关内容