当前位置:网站首页>【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
边栏推荐
- [oc foundation framework] - < copy object copy >
- 甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Advanced Computer Network Review(3)——BBR
- Improved deep embedded clustering with local structure preservation (Idec)
- Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
- Excellent software testers have these abilities
- TP-LINK 企业路由器 PPTP 配置
- [text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
- What is an R-value reference and what is the difference between it and an l-value?
猜你喜欢

Pytest参数化你不知道的一些使用技巧 /你不知道的pytest

项目连接数据库遇到的问题及解决

UML图记忆技巧

Excellent software testers have these abilities

使用latex导出IEEE文献格式

Using C language to complete a simple calculator (function pointer array and callback function)

Improved deep embedded clustering with local structure preservation (Idec)

一篇文章带你了解-selenium工作原理详解
![[OC foundation framework] - string and date and time >](/img/75/e20064fd0066810135771a01f54360.png)
[OC foundation framework] - string and date and time >

BMINF的後訓練量化實現
随机推荐
[OC-Foundation框架]-<字符串And日期与时间>
TP-LINK 企业路由器 PPTP 配置
什么是MySQL?MySql的学习之路是怎样的
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
LeetCode:39. Combined sum
Mongodb installation and basic operation
使用latex导出IEEE文献格式
SimCLR:NLP中的对比学习
Intel distiller Toolkit - Quantitative implementation 1
Hutool gracefully parses URL links and obtains parameters
LeetCode:394. 字符串解码
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
[OC]-<UI入门>--常用控件的学习
[OC-Foundation框架]--<Copy对象复制>
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
MYSQL卸载方法与安装方法
Nacos 的安装与服务的注册
Problems encountered in connecting the database of the project and their solutions
不同的数据驱动代码执行相同的测试场景
vb. Net changes with the window, scales the size of the control and maintains its relative position