当前位置:网站首页>【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
2022-07-06 08:58:00 【chaolei_9527】
背景
有一个需求,需要在集群内的各个主机创建一样的目录,比如要新增目录x,那么该集群内的所有主机都需要创建该x目录,并更改目录的执行权限。
实现
为了方便操作,准备使用菜单命令构建选项,脚本根据选项选择对应的操作。
2.1 配置文件
services_conf 配置如下:
test1/logs;test1/project-logs;test1/agent/logs
test2/logs;test2/project-logs;test2/agent/logs
test3/logs;test3/project-logs;test3/agent/logs
test4/logs;test4/project-logs;test4/agent/logs
2.2 脚本
#!/bin/bash
# 判断配置文件是否存在
function file_exist() {
conf_filename=$1
[ ! -f $conf_filename ] && {
echo "there is no conf file named $conf_filename"
exit 1
}
}
# 在集群中执行命令
function create_dir() {
echo "ansible 要执行的命令是 $1"
ansible all -m shell -a "$1"
}
function main_op() {
services_conf=$1
services_name=`cat $services_conf | grep -v "^#"`
oldIFS=$IFS
IFS=$'\n'
for service in $services_name
do
echo "service:$service 开始处理........."
IFS=$";"
for sub_item in $service
do
#文件夹如果不存在则创建
if [ ! -d /home/work/chao/$sub_item ];then
create_dir "mkdir -p /home/work/chao/$sub_item && chmod -R 777 /home/work/chao/$sub_item"
else
echo "$sub_item已存在..............."
fi
done
echo "$service处理完毕..................."
echo
done
IFS=$oldIFS
}
##########脚本开始执行
echo "Now ,the shell will be executed named $0.............."
PS3="Enter option"
flag=1
while [ $flag -eq 1 ]
do
echo "Please input a number: 1. 创建目录; 2.退出"
select option in "创建目录" "退出"
do
case $option in
"退出")
flag=0
break
;;
"创建目录")
echo -e "\033[31m 开始创建目录操作................\033[0m"
file_exist services_conf
main_op services_conf
echo
echo -e "\033[31m 完成创建目录................\033[0m"
;;
*)
clear
echo "对不起,输入的选项有误!"
echo "Please input a number: 1. 创建目录; 2.退出"
;;
esac
#select
done
#while
done
边栏推荐
- LeetCode:214. Shortest palindrome string
- 什么是MySQL?MySql的学习之路是怎样的
- 数学建模2004B题(输电问题)
- LeetCode:162. Looking for peak
- TP-LINK 企业路由器 PPTP 配置
- Intel distiller Toolkit - Quantitative implementation 3
- After reading the programmer's story, I can't help covering my chest...
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- LeetCode:剑指 Offer 04. 二维数组中的查找
- Unsupported operation exception
猜你喜欢
随机推荐
LeetCode:34. Find the first and last positions of elements in a sorted array
Revit secondary development Hof method calls transaction
LeetCode:836. Rectangle overlap
CSP first week of question brushing
I-BERT
Ijcai2022 collection of papers (continuously updated)
[sword finger offer] serialized binary tree
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Navicat Premium 创建MySql 创建存储过程
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
MySQL uninstallation and installation methods
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
BMINF的後訓練量化實現
SimCLR:NLP中的对比学习
BN folding and its quantification
BN折叠及其量化
Philosophical enlightenment from single point to distributed
【嵌入式】Cortex M4F DSP库
Li Kou daily question 1 (2)
Compétences en mémoire des graphiques UML