为什么这个shell脚本在没有读取`case`的情况下退出?

时间:2017-04-03 05:32:30

标签: bash shell

我正在为练习编写bash shell脚本,但似乎无法修复其行为。该脚本将文件和目录数组复制到给定的备份目录 $ path 中,并将其压缩为 .tar.gz 文件。

我正在进行的迭代应使用备份目录的命名模式检查所述 .tar.gz 文件是否已存在( $ date_value.tar.gz ),具体取决于答案。该脚本现在应该从主项目目录运行。

以下代码是出错的部分。

if [ -f "$path/$date_value.tar.gz" ] ; then

  echo "A $date_value.tar.gz directory already exists."

  read -p "Do you wish to update directory ? (Y/N)"

  echo # Moves to new line; for user experience purpose only

  case $REPLY in

        # Case when user wrote "Y" or "y" as in Yes
        " [[ $REPLY =~ ^[Yy]$ ]] " )

            # Extract archive
            tar xf "$path/$date_value.tar.gz"

            # Synchronise directories
            rsync --update -raz --progress \ 
                  --include="$current_path${files_array[@]}" "$path/$date_value" \
                  --exclude="*"

   exit 0;
;;

        # Case when user wrote anything else
        " [[ $REPLY =~ ^[*]$ ]] " )

            echo "Script didn't make any change and stopped itself."

    exit 1;
esac

# Else if backup directory doesn't exist yet
else

    # Make a directory using the date
    mkdir "$path/$date_value"

    # Loops over the whole array and copies files/directories
    # recursively to given directory with current rights
    for i in ${files_array[@]}; do
            cp -ar ${i} /home/robin/backup/by_date/"$date_value"/
    done

    # Goes to backup directory
    cd "$path"

    # Compress backup directory into tarball AND(=&&) removes it if successfull
    tar cfM "$date_value.tar.gz" "$date_value" && rm -Rf "$date_value"

fi

bash -x模式下运行时,我看到它按预期进行到case $REPLY in行,然后突然停止而没有通过案例。我在最后一次关闭exit 1之后将fi作为测试,并且可以确认脚本只是跳过case并停止,因为没有进一步的说明。

那里出了什么问题,为什么放弃case

用于编写此代码的文档:

Debian 上运行,bash为终端。

1 个答案:

答案 0 :(得分:2)

不正确:

<input type="hidden" name="warehouseBean.pickReqList[0].grade" value="BR000-R" id="warehousePick_warehouseBean_pickReqList_0__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[0].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_0__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[0].action" value="false" id="warehousePick_warehouseBean_pickReqList_0__action"/>

<input type="hidden" name="warehouseBean.pickReqList[1].grade" value="BR001-R" id="warehousePick_warehouseBean_pickReqList_1__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[1].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_1__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[1].action" value="false" id="warehousePick_warehouseBean_pickReqList_1__action"/>

<input type="hidden" name="warehouseBean.pickReqList[2].grade" value="BR002-R" id="warehousePick_warehouseBean_pickReqList_2__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[2].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_2__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[2].action" value="false" id="warehousePick_warehouseBean_pickReqList_2__action"/> .........

<input type="hidden" name="warehouseBean.pickReqList[18].grade" value="BR0018-R" id="warehousePick_warehouseBean_pickReqList_18__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[18].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_18__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[18].action" value="false" id="warehousePick_warehouseBean_pickReqList_18__action"/>

<input type="hidden" name="warehouseBean.pickReqList[19].grade" value="BR0019-R" id="warehousePick_warehouseBean_pickReqList_19__grade"/>
<input type="hidden" name="warehouseBean.pickReqList[19].quantity" value="1" id="warehousePick_warehouseBean_pickReqList_19__quantity"/>
<input type="hidden" name="warehouseBean.pickReqList[19].action" value="false" id="warehousePick_warehouseBean_pickReqList_19__action"/>

正确:

" [[ $REPLY =~ ^[Yy]$ ]] " )

OP代码中还有其他错误,但从...开始......