按找下列要求,写一个脚本
(1)创建目录/tmp/testdir-当前日期时间
(2)在此目录创建100个空文件:file1-file100
(3)显示/etc/passwd文件中位于偶数行的用户的用户名
(4)创建10个用户:user10-user19,密码同用户名
(5)在/tmp创建10个空文件file10-file19
脚本如下:
#!/bin/bash
#set variable date
date=$(date +%Y%m%d)
#create directroy
mkdir /tmp/testdir-${date}
#create 100 empty file
for i in {1..100}; do
touch /tmp/testdir-${date}/file${i}
done
#display enev line in the file /etc/passwd
for i in {1..19};do
let judge_enev=${i}%2
if [ ${judge_enev} -eq 0 ];then
echo "The No.${i} username is $(cat /etc/passwd | head -${i} |tail -1 |cut -d: -f1)"
else
continue
fi
done
#create 10 user and set username is user's password
begin_num=10
for i in {1..10};do
id user${begin_num} &> /dev/null
if [ $? -eq 0 ];then
echo "The user is existed."
else
useradd user${begin_num}
echo "user${begin_num}" | passwd –stdin user${begin_num} &> /dev/null
fi
let begin_num+=1
done
echo "User10-User10 was Created!"
#create 10 empty file
for i in {10..19};do
touch /tmp/file${i}
done
echo "File10-File19 was Created!"
原创文章,作者:Tornado,如若转载,请注明出处:http://www.178linux.com/55404