题目
1、编写一个shell 脚本,检测 /tmp/size.log 文件。如果存在,显示它的内容;不存在则创建一个文件,将创建时间写入。
2、编写一个shell 脚本,实现批量添加 20个用户,用户名为user1-20,密码为user 后面跟随5个随机字符。
3、编写一个shell 脚本,将/usr/local 目录下大于10M的文件,转移到/tmp目录下。
第一题
[root@192 profile.d]# vim jopl1.sh
#!/bin/bash
if [ -d "/tmp" ];
then
echo "/tmp该目录存在"
else
mkdir /tmp
fi
if [ -f "/tmp/size.log" ];
then
echo "size.log该文件存在";
cat /tmp/size.log
else
touch /tmp/size.log;
echo "size.log该文件不存在,已新建一个同名文件";
date > /tmp/size.log;
echo "成功将创建时间添加到该文件中"
fi
#赋予脚本执行权限
[root@192 profile.d]# chmod +x jopl1.sh
脚本内容:

测试结果:
第一次执行脚本

第二次执行脚本

第二题
[root@192 profile.d]# vim jopl2.sh
#!bin/bash
for i in {1..20}
do
pw=`echo $[$RANDOM]|md5sum|cut -c 1-5`
useradd user$i
echo "user$i $pw" >> /user.txt
echo "user$pw" |passwd --stdin user$i
done
[root@192 profile.d]# chmod +x jopl2.sh
脚本内容:

执行结果:

查看文件内容以验证(/user.txt已提前创建好)

第三题
[root@192 profile.d]# vim jopl3.sh
#!bin/bash
read -p "请输入文件源路径:" file path
read -p "请输入保存的文件路径:"save_path
if [ ! -d $file_path ];
then
echo "源路径非目录或不存在!"
exit 2
fi
if [ ! -d $save_path ];
then
echo "保存路径非目录或不存在!"
exit 2
fi
files=$(find $file_path -type f -size +10M)
if [ -z "$files" ];
then
echo "$file_path目录下没有文件大于10M"
else
for file in $files;
do
mv "$file" "$save_path"
if [ $? -eg 0 ];
then
echo "已将 $file 移动到 $save_path"
else
echo "移动 $file 失败"
fi
done
fi
[root@192 profile.d]# chmod +x jopl3.sh
脚本内容:

测试结果:
