shell面试题

1.使用for循环在/westos目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串westos

#!/bin/bash

Path=/westos
[ -d "$Path" ] || mkdir -p $Path                 ##判断/westos是否存在,不存在就新建一个

for i in `seq 10`
do
    random=$(openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 3-12)
   touch $Path/${random}_westos.html
done

其中的openssl rand -base64 40 是采用随机的base64加密生成的40长度的密钥

2.新建10个用户westos01-westos10,然后赋予这是个用户随机的密码,密码输出到文档一份

#!/bin/bash

. /etc/init.d/functions

user="westos"
passfile="/tmp/user.log"

for num in `seq -w 10`
do
    pass="`echo $RANDOM | md5sum | cut -c 3-12`"
    useradd $user$num &> /dev/null && {
        echo "$pass" | passwd --stdin $user$num &> /dev/null
        echo -e "user:$user$num\tpasswd:$pass" >> $passfile
    }

if [ $? -eq 0 ];then
    action "$user$num is ok" /bin/true
else
    action "$user$num is failed" /bin/false
fi
done

运行一下

猜你喜欢

转载自blog.csdn.net/weixin_40543283/article/details/85720176