Linux Bash脚本-for循环和if else

Linux Bash脚本-for循环和if else,linux,bash,shell,loops,hdfs,Linux,Bash,Shell,Loops,Hdfs,我不熟悉Bash脚本,尽管多次尝试重构下面伪代码中所示的逻辑结构,但我无法让它工作。如何将if/else逻辑与for循环组合在一起 伪代码: 我有一个命令,用于检查HDFS文件系统的目录中是否存在任何子文件夹。让我们把这个命令称为A If subfolders do NOT exist { proceed with remaining execution of script } Else { sleep for 30 minutes and run command_A again

我不熟悉Bash脚本,尽管多次尝试重构下面伪代码中所示的逻辑结构,但我无法让它工作。如何将if/else逻辑与for循环组合在一起

伪代码: 我有一个命令,用于检查HDFS文件系统的目录中是否存在任何子文件夹。让我们把这个命令称为A

If subfolders do NOT exist {
    proceed with remaining execution of script
}
Else {
    sleep for 30 minutes and run command_A again to see if the subfolders have been removed.  Note:  This sleep and re-check cycle should repeat up to 4 times and if subfolders are never removed the script is killed with exit 1
}
下面是我尝试过的例子。我不知道如何将| |与else语句结合使用

使用这些结构

1. for i in {1..4}; do command_A && break || sleep 1800; done
2. if command_A ; then echo "Command succeeded" else echo "Command failed" fi 
for i in {1..4};
  do
   if hdfs dfs -test -e $mypath/*;
     then
       if [ $i -eq 4 ]
        then
          echo "script exiting now with code 1"
       else
          echo "Folder is full"
          sleep 10
       fi
   else
    echo "Folder is empty"
    break
  fi
done
测试示例

for i in {1..4};
  do
     if hdfs dfs -test -e $mypath/*
       echo "Folder is empty" && break
else
     ???
显示工作解决方案的更新

1. for i in {1..4}; do command_A && break || sleep 1800; done
2. if command_A ; then echo "Command succeeded" else echo "Command failed" fi 
for i in {1..4};
  do
   if hdfs dfs -test -e $mypath/*;
     then
       if [ $i -eq 4 ]
        then
          echo "script exiting now with code 1"
       else
          echo "Folder is full"
          sleep 10
       fi
   else
    echo "Folder is empty"
    break
  fi
done
这样行吗

sleep_count=0
max_attempts=4
for ((i=0; i < $max_attempts; i++)); do
  if hdfs dfs -test -e $mypath/*; then
     echo "No subfolders!"
     break
  else
     # subfolders exist; wait for a while and see if they go away
     ((sleep_count++))
     sleep 1800
  fi
done

if [[ $sleep_count == $max_attempts ]]; then
  # waited too long for subfolders to go away
  exit 1
fi

# probably repeat the above steps again?
sleep\u count=0
最大尝试次数=4次
对于((i=0;i<$max_尝试次数;i++));做
如果hdfs dfs-test-e$mypath/*;然后
echo“没有子文件夹!”
打破
其他的
#子文件夹存在;等一会儿,看看他们是不是走了
((睡眠计数++))
睡1800
fi
完成
如果[[$sleep\u count==$max\u尝试次数]];然后
#等待子文件夹消失的时间太长
出口1
fi
#可能再次重复上述步骤?

更新

这是这个特定问题的完整代码,我将在下面保留原始的稍微通用的代码,以供将来参考,因为对于其他搜索类似问题的人来说,它不太复杂(if/else嵌套更少)

for i in {1..4};
  do
   if hdfs dfs -test -e $mypath/*;
     then
       if [ $i -eq 4 ]
        then
          echo "script exiting now with code 1"
       else
          echo "Folder is full"
          sleep 10
       fi
   else
    echo "Folder is empty"
    break
  fi
done

我认为这样做会奏效,这正是你所拥有的。您只需将要检查的内容放入if语句中,这段代码应该适合您

for i in {1..4}
do
   if [ <check for subdirs> ]
   then 
       echo "Folder is empty!" 
       break
   else
       sleep 1800
   fi
done
{1..4}中i的

做
如果[]
然后
echo“文件夹为空!”
打破
其他的
睡1800
fi
完成

i在{1..4}中为什么需要
用于?您没有使用
i
anywhere@Inian我需要重复检查文件夹是否已在2小时内删除多达4次。如果它们从未被删除,那么我会退出脚本,因为我不希望它无限期地运行?或者说你错过了什么?@HendPro12我不清楚你到底想做什么。您希望运行最多4个循环的循环,并在循环中运行命令并测试其结果,如果结果不为空,则退出循环,否则睡眠30分钟并再次运行命令(直到循环自行退出)?我已将上述命令作为终端中的独立命令成功测试。如果检查显示在第一次执行命令时存在文件夹,我无法确定逻辑结构是否会重复检查3或4次。@iAn bash shell正在从本地操作系统运行脚本并调用hdfs文件系统命令。不确定这是否是您要问的……但将hdfs命令封装在bash shell scriptsThanks中是一个常见的用例!通过对then和else块中的内容进行小的交换,这是可行的。我在上面提供了一个更新来反映这一变化。如果要更新响应以匹配。我将把它作为答案。我错误地认为| |运算符是for循环的一部分,无法理解它是如何与if/else并行工作的。很高兴它有所帮助。你可能已经想得太多了(如果每次我想出一个比需要的更复杂的解决方案时我只有一美元的话…)或者有时候在范例之间切换会产生奇怪的副作用,我们想把所有的东西都结合在一起,哈哈。我已经用你的工作代码更新了答案,并把稍微简化的版本留给了其他可能不需要做完全相同事情的人。