1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。
脚本:
###############################################
# Author: aaa
# Version:
# Date: 2025/01/12
# Mail: aaa@westos.org
# Function:
#
################################################
#!/bin/bash
# 定义文件路径
file="/tmp/size.log"
# 检查文件是否存在
if [ -f "$file" ]; then
# 如果文件存在,显示文件内容
cat "$file"
else
# 如果文件不存在,创建文件并写入当前时间
echo "File created on $(date)" > "$file"
echo "File created and timestamp written."
fi
结果:

2、写一个 shell 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。
脚本:
###############################################
# Author: aaa
# Version:
# Date: 2025/01/12
# Mail: aaa@westos.org
# Function:
#
################################################
#!/bin/bash
# 创建20个用户
for i in $(seq -f "%02g" 1 20);
do
# 生成5个随机字符
random_pass=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 5)
# 设置用户名和密码
username="user$i"
password="user$random_pass"
# 创建用户并设置密码
useradd "$username"
echo "$username:$password" | chpasswd
# 输出创建的用户和密码
echo "Created user: $username with password: $password"
done
结果:

3、编写个shell 脚本,将/usr/local 日录下大于10M的文件转移到/tmp目录下
脚本:
#!/bin/bash
# 源目录和目标目录
source_dir="/usr/local"
target_dir="/tmp"
# 查找/usr/local目录下大于10MB的文件,移动到/tmp目录
find "$source_dir" -type f -size +10M | while read file; do
# 获取文件名
filename=$(basename "$file")
# 移动文件到/tmp目录
mv "$file" "$target_dir/$filename"
# 输出移动的信息
echo "Moved $file to $target_dir/$filename"
done
结果:
