当前位置:网站首页>【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
边栏推荐
- Revit secondary development Hof method calls transaction
- Deep anatomy of C language -- C language keywords
- LeetCode:394. String decoding
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- LeetCode:124. Maximum path sum in binary tree
- Variable length parameter
- Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
- LeetCode:836. Rectangle overlap
- [today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
- Mongodb installation and basic operation
猜你喜欢
A convolution substitution of attention mechanism
一篇文章带你了解-selenium工作原理详解
postman之参数化详解
LeetCode:124. Maximum path sum in binary tree
I-BERT
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[OC-Foundation框架]--<Copy对象复制>
[OC-Foundation框架]---【集合数组】
随机推荐
[OC foundation framework] - string and date and time >
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
一篇文章带你了解-selenium工作原理详解
Advance Computer Network Review(1)——FatTree
Implement window blocking on QWidget
LeetCode:394. 字符串解码
LeetCode:26. 删除有序数组中的重复项
BN folding and its quantification
Super efficient! The secret of swagger Yapi
Hutool gracefully parses URL links and obtains parameters
opencv+dlib实现给蒙娜丽莎“配”眼镜
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
MYSQL卸载方法与安装方法
Using C language to complete a simple calculator (function pointer array and callback function)
UnsupportedOperationException异常
Leetcode: Jianzhi offer 03 Duplicate numbers in array
[OC]-<UI入门>--常用控件-UIButton
[MySQL] multi table query
IJCAI2022论文合集(持续更新中)
在QWidget上实现窗口阻塞