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" ]; thencat "$file_name"
elsedate > "$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.shlog_name="/server/script/userinfo.txt"
: > "$log_name"# 批量创建用户
for i in {1..20}
douser="user$(printf %02d $i)"if id "$user" > /dev/null 2>&1; thenecho "$user用户已存在!!"elsepassword=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 5 | head -n 1)useradd "$user"echo "$password" | passwd --stdin "$user" > /dev/nullecho "$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" ]; thenecho "源目录 $source_mulu 不存在,检查!"exit 1
fi# 检查目标目录是否存在,不存在则创建
if [! -d "$target_mulu" ]; thenmkdir -p "$target_mulu"echo "目标目录 $target_mulu 不存在,已自动创建。"
ficount=0
for file in $(find "$source_mulu" -type f -size +10M 2>/dev/null); doif [ -f "$file" ] && mv "$file" "$target_mulu" 2>/dev/null; thenecho "$file 文件已成功移动到 $target_mulu 目录下。"((count++))elseecho "移动文件 $file 到 $target_mulu 时出现错误,请检查权限等相关问题。"fi
doneif [ $count -eq 0 ]; thenecho "$source_mulu 目录下没有大于10M的文件可移动。"
elseecho "$source_mulu 目录下共有 $count 个大于10M的文件已经移动到 $target_mulu 目录下了。"
fi[root@openEuler-1 scripts]# bash test3.sh
/usr/local 目录下没有大于10M的文件可移动。