shell练习1

发布于:2025-02-10 ⋅ 阅读:(51) ⋅ 点赞:(0)

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


[root@openEuler Shell]# vim 2.sh


#!/bin/bash
#########################
#File name:2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2025-01-11 14:13:13
#Description:
#########################


read -p "请输入文件名: "  file

if [ -e $file  ]
then
   cat $file
else
  touch  $file
  echo $(date) >> $file
fi


[root@openEuler Shell]# sh 2.sh


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


[root@openEuler Shell]# vim 3.sh


#!/bin/bash
#########################
#File name:3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2025-01-11 16:55:24
#Description:
#########################


for i in `seq -w 01 20`
do
   random=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1)
   username="user${i}"
   password="user${random}"
   #echo $username
   #echo $password
   useradd $username
   echo "$password" | passwd --stdin $username


done


[root@openEuler Shell]# sh 3.sh


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


[root@openEuler Shell]# vim 4.sh +


#!/bin/bash
#########################
#File name:4.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2025-01-11 17:40:34
#Description:
#########################

#!/bin/bash
read -p "请输路径:" filepath
files=$(find $filepath  -type f -size +10M)
for file in $files
do
    filename=$(basename $file)
    mv $file /tmp/$filename
done