我对 Bash 比较陌生,我正在尝试清理内容并拥有更好的文件夹结构。
我目前正在尝试构建一个具有上下选项的脚本,例如著名的命令“
Docker-Compose Up
”和“Docker-Compose Down
”
我唯一的问题是我不太熟悉 If 函数或是否有任何其他函数可以将 Up 和 Down 作为选项或参数
#!/bin/bash
sudo docker-compose -f /home/home/compose/tools.yml -f /home/home/compose/dashboards.yml up -d
#!/bin/bash
sudo docker-compose -f /home/home/compose/tools.yml -f /home/home/compose/dashboards.yml down
新脚本应命名为 compose.sh,我希望我可以直接输入
./compose.sh up
在 Docker 中运行和创建这些容器
./compose-sh down
停止并删除 Docker 中的这些容器
我的代码最大的设计缺陷是它不是动态的。这可能是一个更复杂的代码,需要改进,但如果有人更容易创建一个代码来基本上在特定目录(例如
Docker-Compose Up
)内的所有 xml 文件上运行 /home/home/compose
。例如,文件夹“compose”中的所有 xml 文件将自动 docker-compose up,如果我也想进行 docker-compose down
#!/bin/bash
This script is a bash script that manages Docker Compose operations such as starting, stopping, restarting, and pulling services defined in YAML files
# down() function stops and removes all Docker Compose containers defined in the YAML files in the current folder.
# @return: Outputs a message indicating whether the containers were successfully removed or if no containers were found.
down() {
local yml_files=($(find "$(pwd)" -maxdepth 1 -name "*.yml")) # Find all yml files in the current directory
# Check if any YAML files are found
if [ ${#yml_files[@]} -eq 0 ]; then
echo "No yml files found in the current folder."
exit 1
fi
local compose_files=""
for file in "${yml_files[@]}"; do
compose_files+="-f $file " # Append each yml file to the command
done
local output=$(sudo docker-compose $compose_files down 2>&1) # Capture the output of the command
# Check if the output contains the message "No resource found to remove"
if [[ $output == *"No resource found to remove"* ]]; then
echo "No compose files are running in the current folder."
else
echo "All compose-related containers are removed in the current folder."
fi
}
# up() function starts Docker Compose containers defined in the YAML files in the current folder.
# @return: No return value.
up() {
local yml_files=($(find "$(pwd)" -maxdepth 1 -name "*.yml")) # Find all yml files in the current directory
# Check if any YAML files are found
if [ ${#yml_files[@]} -eq 0 ]; then
echo "No yml files found in the current folder."
exit 1
fi
local compose_files=""
for file in "${yml_files[@]}"; do
compose_files+="-f $file " # Append each yml file to the command
done
sudo docker-compose $compose_files up -d
}
# restart() function restarts Docker Compose containers defined in the YAML files in the current folder.
# @return: No return value.
restart() {
local yml_files=($(find "$(pwd)" -maxdepth 1 -name "*.yml")) # Find all yml files in the current directory
# Check if any YAML files are found
if [ ${#yml_files[@]} -eq 0 ]; then
echo "No yml files found in the current folder."
exit 1
fi
local compose_files=""
for file in "${yml_files[@]}"; do
compose_files+="-f $file " # Append each yml file to the command
done
sudo docker-compose $compose_files restart
}
# stop() function stops Docker Compose containers defined in the YAML files in the current folder.
# @return: No return value.
stop() {
local yml_files=($(find "$(pwd)" -maxdepth 1 -name "*.yml")) # Find all yml files in the current directory
# Check if any YAML files are found
if [ ${#yml_files[@]} -eq 0 ]; then
echo "No yml files found in the current folder."
exit 1
fi
local compose_files=""
for file in "${yml_files[@]}"; do
compose_files+="-f $file " # Append each yml file to the command
done
sudo docker-compose $compose_files stop
}
# pull() function stops and removes all Docker Compose containers, then starts new containers defined in the YAML files in the current folder.
# @return: No return value.
pull() {
down
up
}
# start() function starts Docker Compose containers defined in the YAML files in the current folder.
# @return: No return value.
start() {
local yml_files=($(find "$(pwd)" -maxdepth 1 -name "*.yml")) # Find all yml files in the current directory
# Check if any YAML files are found
if [ ${#yml_files[@]} -eq 0 ]; then
echo "No yml files found in the current folder."
exit 1
fi
local compose_files=""
for file in "${yml_files[@]}"; do
compose_files+="-f $file " # Append each yml file to the command
done
sudo docker-compose $compose_files start
}
# main() function is the entry point of the script. It calls the appropriate function based on the user's input.
# @param $1: The command to execute (down, up, restart, stop, pull, start).
# @return: No return value.
main() {
if [ "$1" == "down" ]; then
down
elif [ "$1" == "up" ]; then
up
elif [ "$1" == "restart" ]; then
restart
elif [ "$1" == "stop" ]; then
stop
elif [ "$1" == "pull" ]; then
pull
elif [ "$1" == "start" ]; then
start
else
echo "Invalid option. Please choose one of the following: down, up, restart, stop, pull, start."
fi
}
# Check if the script is being executed directly (not sourced) and call the main() function with the provided arguments.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
我在一位老朋友的帮助下成功创建了一个 bash 代码,使我的代码变得动态。 享受:)
该脚本提供了常用 Docker Compose 命令的功能,例如 down、up、restart、stop、pull 和 start。
主要目标是能够基于 pwd 的当前文件夹自动化和运行 docker-compose 文件。
这些函数正确查找当前目录中的所有 YAML 文件,并将它们用作 Docker Compose 命令的输入。
1. sudo mv <CurrentPathofScript>/<ScriptName> /usr/local/bin/
2. sudo chmod 755 /usr/local/bin/<ScriptName>