Linux---shell脚本练习

要求: 

1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。
2、写一个 shel1 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。
3、编写个shel 脚本将/usr/local 日录下大于10M的文件转移到/tmp目录下

要求1

代码:

if [ -f /tmp/size.log ]; then
    cat /tmp/size.log
else
    echo $(date) > /tmp/size.log
fi

结果:

要求2

代码:

for ((i = 1; i <= 20; i++)); do
    username="user$(printf %02d $i)"
    password="user$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1)"
    useradd -m -p $(openssl passwd -1 $password) $username
    echo "User $username created with password: $password"
done

 结果:

要求3

代码:

#!/bin/bash
for file in /usr/local/*; do
    if [ -f $file ]; then
        size=$(du -m $file | awk '{print $1}')
        if [ $size -gt 10 ]; then
            mv $file /tmp
            echo "Moved $file to /tmp"
        fi
    fi
done

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值