当前位置: 首页 > article >正文

运维高级课作业一

1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。

[root@openEuler-1 scripts]# vim test1.sh

!/bin/bash
#########################
#File name:test1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2025-01-11 21:22:29
#Description:
#########################

file_name="/tmp/size.log"

if [ -f "$file_name" ]; then
    cat "$file_name"
else
    date > "$file_name"
    echo "File created with timestamp."
fi

[root@openEuler-1 scripts]# echo "test1" > /tmp/size.log
[root@openEuler-1 scripts]# bash test1.sh
test1
[root@openEuler-1 scripts]# rm -f /tmp/size.log 
[root@openEuler-1 scripts]# bash test1.sh
File created with timestamp.
[root@openEuler-1 scripts]# bash test1.sh
Sat Jan 11 09:29:04 PM CST 2025


2、写一个 shel1 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。


[root@openEuler-1 scripts]# vim test2.sh

log_name="/server/script/userinfo.txt"
: > "$log_name"

# 批量创建用户
for i in {1..20}
do
    user="user$(printf %02d $i)"
    if id "$user" > /dev/null 2>&1; then
        echo "$user用户已存在!!"
    else
        password=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 5 | head -n 1)
        useradd "$user"
        echo "$password" | passwd --stdin "$user" > /dev/null
        echo "$user用户创建成功!!"
        echo "user:$user,password:$password" >> "$log_name"
    fi
done

[root@openEuler-1 scripts]# bash test2.sh
test2.sh: line 4: /server/script/userinfo.txt: No such file or directory
user01用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user02用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user03用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user04用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user05用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user06用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user07用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user08用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user09用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user10用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user11用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user12用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user13用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user14用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user15用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user16用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user17用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user18用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user19用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory
user20用户创建成功!!
test2.sh: line 17: /server/script/userinfo.txt: No such file or directory


3、编写个shel 脚本将/usr/local 日录下大于10M的文件转移到/tmp目录下

[root@openEuler-1 scripts]# vim test3.sh

# 源目录,目标目录
source_mulu="/usr/local"
target_mulu="/tmp"

# 检查源目录是否存在
if [! -d "$source_mulu" ]; then
    echo "源目录 $source_mulu 不存在,检查!"
    exit 1
fi

# 检查目标目录是否存在,不存在则创建
if [! -d "$target_mulu" ]; then
    mkdir -p "$target_mulu"
    echo "目标目录 $target_mulu 不存在,已自动创建。"
fi

count=0
for file in $(find "$source_mulu" -type f -size +10M 2>/dev/null); do
    if [ -f "$file" ] && mv "$file" "$target_mulu" 2>/dev/null; then
        echo "$file 文件已成功移动到 $target_mulu 目录下。"
        ((count++))
    else
        echo "移动文件 $file 到 $target_mulu 时出现错误,请检查权限等相关问题。"
    fi
done

if [ $count -eq 0 ]; then
    echo "$source_mulu 目录下没有大于10M的文件可移动。"
else
    echo "$source_mulu 目录下共有 $count 个大于10M的文件已经移动到 $target_mulu 目录下了。"
fi

[root@openEuler-1 scripts]# bash test3.sh
/usr/local 目录下没有大于10M的文件可移动。


http://www.kler.cn/a/500723.html

相关文章:

  • 每天五分钟深度学习:神经网络中的激活函数
  • Jaeger UI使用、采集应用API排除特定路径
  • Zookeeper 集群安装
  • MeCo——给预训练数据增加源信息,就能减少33%的训练量并且提升效果
  • MYSQL-创建数据库 CREATE DATABASE (十一)
  • 蠕虫病毒会给服务器造成哪些危害?
  • vue3后台系统动态路由实现
  • centos 搭建nginx+配置域名+windows访问
  • Vue 开发者的 React 实战指南:性能优化篇
  • 【Ubuntu与Linux操作系统:九、Shell编程】
  • Perl语言的编程范式
  • 简历整理YH
  • Django 社团管理系统的设计与实现
  • SpringBoot项目实战(39)--Beetl网页HTML文件中静态图片及CSS、JS文件的引用和展示
  • 如何在Go语言开发中实现高性能的分布式日志收集
  • 【微服务】面试 2、服务雪崩
  • 【网络】:网络编程套接字
  • 《机器学习》集成学习之随机森林
  • 双因素身份验证技术在NPI区域邮件安全管控上的解决思路
  • Java Web开发基础:HTML的深度解析与应用
  • 基于SSM实现的垃圾分类平台系统功能实现二
  • CSS3 弹性盒子
  • 第三十六章 Spring之假如让你来写MVC——拦截器篇