shell练习3

1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容,不存在则创建一个文件将创建时间写入。

#!/bin/bash
file="/tmp/size.log"
if [ -e "$file" ]
then
	cat "$file"
else
	date +%Y-%m-%d:%H:%M:%S > "$file"
	echo "文件$file已创建,并写入了时间"
fi

运行结果:


2、写一个 shel1 脚本,实现批量添加 20个用户,用户名为user01-20,密码为user 后面跟5个随机字符。

#!/bin/bash
for i in {01..20}
do
	username="user$i"
	passwd="user`cat /dev/random | head -1 | md5sum | head -c 5`"
	useradd $username
	echo "$username:$passwd" | passwd --stdin $username
	echo "$username--$passwd" >> user.txt
	echo "用户 $username 已创建,密码为 $password"
done

运行结果:

3、编写个shel 脚本将/usr/local 日录下大于10M的文件转移到/tmp目录下

#!/bin/bash
src="/user/local"
dst="/tmp"

for file in $(find $src -type f)
do
        file_size=$(stat -c%s "$file")
        if [ $file_size -gt 10485760 ]
        then
                file_name=$(basename "$file")
                mv "$file" "$dst/$file_name"
        fi
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值