如果回答为“是”则继续

时间:2015-11-03 22:32:41

标签: bash

我有一个脚本,用于检查目录是否存在并运行以下命令:

 if [ -d /home/philip_lee/csv_dump/$TABLENAME ]; then
     echo "Directory '$TABLENAME' already exists. Delete the existing directory?"
     select yn in "Yes" "No"; do
         case $yn in
             Yes )    rm -rf /home/philip_lee/csv_dump/$TABLENAME
                      break;;
             No )    echo "$EXIT"
                     exit 1;;
         esac
     done
 else
 echo "Please enter the email you would like the results to be sent to:"
 read RECIPIENT
 mkdir /home/philip_lee/csv_dump/$TABLENAME

在此之后,还有几行代码用csv文件填充目录。我遇到的问题是,如果他们选择“是”,我希望脚本继续执行mkdir命令。

到目前为止,break命令提供了部分修复 - 如果连续两次选择“yes”,则删除该目录,然后继续重新创建它。这是我尝试运行脚本时当前发生的事情:

+ '[' -d /home/philip_lee/csv_dump/banks ']'
+ echo 'Directory '\''banks'\'' already exists. Overwrite the existing directory?'
Directory 'banks' already exists. Overwrite the existing directory?
+ select yn in '"Yes"' '"No"'
1) Yes
2) No
#? 1
+ case $yn in
+ rm -rf /home/philip_lee/csv_dump/banks
+ break 1
#? 1
+ case $yn in
+ '[' -d /home/philip_lee/csv_dump/banks ']'
+ echo 'Please enter the email you would like the results to be sent to:'
Please enter the email you would like the results to be sent to:
+ read RECIPIENT
dummyemail@gmail.com
+ mkdir /home/philip_lee/csv_dump/banks

我想要一个解决方案,如果选择“是”,则删除目录然后继续执行以下行,否则如果选择“否”则退出。

编辑:找出问题的根源 - 休息实际上是让我回到我之前运行的另一个yesno查询。我只需要找出一种方法来防止中断让我回到原来的yesno - 这个例子应该有助于说明问题。

echo "Proceed?"
select yn in "Yes" "No"; do
        case $yn in
                Yes )   echo "Creating new directory for 'banks'..."
                        if [ -d /home/banks ]; then
                                echo "Delete the existing directory?"
                                select yn in "Yes" "No"; do
                                        case $yn in
                                                Yes)    rm -r /home/banks
                                                        break;;
                                                No )    echo "Exit Status 1"
                                                        exit 1;;
                                        esac
                                done
                        else
                                echo "Proceeding..."
                                mkdir /home/banks
                                echo "Proceeded to next step."
                                exit 1
                        fi;;
                No )    echo "Exit Status 2"
                        exit 1;;
        esac
done

2 个答案:

答案 0 :(得分:1)

您已获得使该目录位于else语句内的代码。只要目录存在,它就永远不会到达该命令。试试这个(只需用else替换fi。)

if [ -d /home/philip_lee/csv_dump/$TABLENAME ]; then
     echo "Directory '$TABLENAME' already exists. Delete the existing directory?"
     select yn in "Yes" "No"; do
         case $yn in
             Yes )    rm -rf /home/philip_lee/csv_dump/$TABLENAME
                      break;;
             No )    echo "$EXIT"
                     exit 1;;
         esac
     done
 fi
 echo "Please enter the email you would like the results to be sent to:"
 read RECIPIENT
 mkdir /home/philip_lee/csv_dump/$TABLENAME

为了记录,这就是我将如何处理相同的脚本:

if [ -d "/home/philip_lee/csv_dump/$TABLENAME" ]; then
    read -n 1 -p "Directory '$TABLENAME' already exists. Delete the existing directory? " YN
    if [ $YN != "Y" -a $YN != "y" ]; then
        echo "$EXIT"
        exit 1
    fi
fi
read -p "Please enter the email you would like the results to be sent to: " RECIPIENT
mkdir "/home/philip_lee/csv_dump/$TABLENAME"

始终引用变量;在我假设的数据库表名称中不太可能存在空间,但无论如何它都是一种好的做法。 read接受提示作为命令的一部分,并且不会强迫您进入像select那样的控制结构。 case似乎有两种选择有点过分(而且个人而言,我讨厌语法!)

答案 1 :(得分:1)

通过以这种方式分离出yes / no循环来管理解决问题:

echo "Proceed?"
select yn in "Yes" "No"; do
        case $yn in
                Yes )   break;;
                No )    echo "Exiting...Status 1"
                        exit 1;;
        esac
done
echo "Creating new directory for 'banks'..."
if [ -d /home/philip_lee/csv_dump/banks ]; then
        echo "Directory 'banks' already exists. Delete the existing directory?"
        select yn in "Yes" "No"; do
                case $yn in
                        Yes)    rm -r /home/philip_lee/csv_dump/banks
                                break;;
                        No )    echo "Exiting...Status 2"
                                exit 1;;
                esac
        done
fi
echo "Proceeding..."
mkdir /home/philip_lee/csv_dump/banks
echo "Proceeded to next step."
exit 1

现在看起来更干净 - 感谢所有回复的人。