1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。
(1)创建.sh脚本文件
[root@localhost scripts]# vim size.sh

(2)编写shell脚本
#!/bin/bash #编译器
##############################################################
File Name: size.sh #脚本名称
Version: V1.0 #脚本版本
Author: Gao_XY #作者名字
Email: Gao_XY@163.com #作者邮箱
Organization:https://blog.csdn.net/ens33?type=blog #作者博客
Created Time : 2025-01-13 16:55:03 #创作时间
Description:
##############################################################
if [ -f /tmp/size.log ]; #使用if判断语句,-f参数:判断/tmp/size.log是否存在
then
echo "this file is exist....." #如果存在,那么输出"this file is exist....."
else #否则
touch /tmp/size.log #创建该文件
echo `date` >> /tmp/size.log #向该文件内输入当前时间
echo "OK" #输出OK
fi #结束if语句

(3)测试
第一次执行脚本时,目录中没有文件则执行创建文件
[root@localhost scripts]# sh size.sh


第二次执行脚本,则该文件已经存在

2、写一个 shel1 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。
(1)创建.sh脚本文件
[root@localhost scripts]# vim add_user.sh

(2)编写脚本
#!/bin/bash
##############################################################
File Name: add_user.sh
Version: V1.0
Author: Gao_XY
Email: Gao_XY@163.com
Organization:https://blog.csdn.net/ens33?type=blog
Created Time : 2024-12-21 15:22:52
Description:
###############################################################
read -p "请输入用户名前缀:" u #输入用户名前缀,该前缀作为变量u
for i in {01..20} #使用for循环,i的范围在1-20之间
do
if ! id -u {u}{i} &>/dev/null #if判断用户名是否为{u}{i},!取反
then
useradd {u}{i} #若不存在,则创建用户
echo 'user'$RANDOM | passwd --stdin {u}{i} &>/dev/null
输出user开头后边加5位随机数,作为{u}{i}用户的密码
else
echo "this ui 存在" #否则输出该用户已存在
fi
done
(3)测试
[root@localhost scripts]# sh add_user.sh
请输入用户名前缀:user


3、编写个shel 脚本将/usr/local 日录下大于10M的文件转移到/tmp目录下。
(1)在创建/usr/local目录下创建大于10M的文件
[root@localhost scripts]# dd if=/dev/zero of=/usr/local/text count=3 bs=10M
(2)创建.sh的脚本文件
[root@localhost scripts]# vim text.sh
(3)编写脚本
#!/bin/bash
##############################################################
File Name: text.sh
Version: V1.0
Author: Gao_XY
Email: Gao_XY@163.com
Organization:https://blog.csdn.net/ens33?type=blog
Created Time : 2025-01-13 19:14:44
Description:
##############################################################
a=`find /usr/local -size +10M` #以文件大小查找大于10Mde文件,定义为变量a
if [ -f $a ]; #判断变量a是否存在
then
cp $a /tmp #若存在复制变量a的内容,移至/tmp文件夹内
echo "this file is OK" #输出"this file is OK"
else #否则
echo "NO" #输出NO
fi #结束
(3)测试
[root@localhost scripts]# sh text.sh

