编写一个shell脚本。功能为遍历当前文件夹下所有子文件夹。并找出子文件夹中是否有cmd.sh的脚本。如果有,就进入到当前目录。执行sh cmd.sh 等待一秒。在执行其他文件夹中的cmd.sh 。注意:当前文件夹中的 eureka 和config 文件夹最先进行执行,其他文件夹按顺序执行cmd.sh
时间: 2023-08-12 08:04:10 浏览: 30
好的,我已经理解了你的要求。以下是一个实现上述功能的 shell 脚本:
```shell
#!/bin/bash
# 定义需要执行的文件夹列表
folders=("eureka" "config")
# 遍历当前文件夹下所有子文件夹
for dir in */; do
if [[ ! " ${folders[@]} " =~ " ${dir%/} " ]]; then
# 如果不是 eureka 或 config 文件夹,则加入列表
folders+=("${dir%/}")
fi
done
# 遍历需要执行的文件夹列表
for folder in "${folders[@]}"; do
# 进入目录
cd "$folder" || exit
# 判断是否存在 cmd.sh
if [ -f "cmd.sh" ]; then
# 执行 cmd.sh
sh cmd.sh
# 等待一秒
sleep 1
fi
# 回到上级目录
cd - || exit
done
```
这个脚本首先定义了需要执行的文件夹列表,然后遍历当前文件夹下所有子文件夹,将不是 eureka 或 config 文件夹的文件夹加入到列表中。接着,遍历需要执行的文件夹列表,进入到每个文件夹中,判断是否存在 cmd.sh 文件,如果存在,则执行它并等待一秒,然后回到上级目录。注意,在执行脚本之前需要先将脚本保存到文件中,并赋予执行权限。
相关问题
编写一个shell脚本,遍历当前文件夹下的所有文件。如果子文件夹下包含cmd.sh 就执行 sh cmd.sh
你可以使用以下 Shell 脚本来实现你的需求:
```bash
#!/bin/bash
# 遍历当前文件夹下所有文件和子文件夹
for file in $(find . -type f); do
# 如果子文件夹下包含 cmd.sh,则执行 sh cmd.sh
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
```
这个脚本会使用 `find` 命令遍历当前文件夹下的所有文件和子文件夹。对于每个找到的文件,如果它的路径包含 `/cmd.sh`,则执行 `sh cmd.sh` 命令。请注意,此脚本假定当前文件夹下没有空格或其他特殊字符的文件名。如果有这样的文件名,你需要修改脚本以正确处理它们。
编写一个shell脚本,遍历当前文件夹下的所有文件。如果子文件夹下包含cmd.sh 就执行 sh cmd.sh。 a文件夹和b文件夹优先执行
你可以使用以下 Shell 脚本来实现你的需求:
```bash
#!/bin/bash
# 遍历 a 文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find ./a -type f); do
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
# 遍历 b 文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find ./b -type f); do
if [[ $file == */cmd.sh ]]; then
sh $file
fi
done
# 遍历其他文件夹下所有文件和子文件夹,执行 cmd.sh
for file in $(find . -type f); do
if [[ $file == */cmd.sh && $file != */a/* && $file != */b/* ]]; then
sh $file
fi
done
```
这个脚本会先遍历 `a` 文件夹下的所有文件和子文件夹,对于每个找到的文件,如果它的路径包含 `/cmd.sh`,则执行 `sh cmd.sh` 命令。然后它会遍历 `b` 文件夹下的所有文件和子文件夹,执行同样的操作。最后,它会遍历其他文件夹下的所有文件和子文件夹,对于每个找到的文件,如果它的路径包含 `/cmd.sh` 且不在 `a` 或 `b` 文件夹中,就执行 `sh cmd.sh` 命令。请注意,这个脚本同样假定当前文件夹下没有空格或其他特殊字符的文件名。如果有这样的文件名,你需要修改脚本以正确处理它们。