shell案例题

时间:2022-03-16 12:29:26

目录:

1、批量生成随机字符文件名案例
2、批量改名特殊案例
3、批量创建特殊要求用户案例

1、批量生成随机字符文件名案例(P359)

(1)、利用openssl命令来实现

 #!/bin/bash
#
path=/root/scripts [ -d "$path" ] || mkdir -p $path for n in `seq 10`; do
random=`openssl rand -base64 40 | sed 's@[^a-z]@@g' | cut -c 2-12`
touch $path/${random}_hanshan.html
done

openssl.sh

(2)、利用RANDOM实现

# echo "hanshan$RANDOM" | md5sum | sed 's/[^a-z]//g' | cut -c 2-12

(3)、通过date获得随机数,纯数字

# date +%s%N | md5sum | cut -c 2-12

(4)、利用/dev/urandom配合cksum生成随机数

# head /dev/urandom | cksum | md5sum | cut -c 2-11

(5)、通过UUID生成随机数

# cat /proc/sys/kernel/random/uuid | md5sum | sed 's/[^a-z0-9]//g' | cut -c 2-12

(6)、利用expect命令附带的mkpasswd生成随机数(P258)

# mkpasswd -l 30 -d 5 -c 15 -C 5 -s 5 | md5sum  | sed 's/[^a-z]//g' | cut -c 2-12

2、批量改名特殊案例(P360)

(1)、利用rename命令改名

# rename hanshan.html xiaoheshang.html  *.html    //把字符hanshan改为xiaoheshang

(2)、使用for循环遍历

 #!/bin/bash
#
filename=xiaoheshang.html
dirname=/root/scripts
cd $dirname || exit 1 for n in `ls *.html`; do //列出所有以.html结尾的文件
name=$(echo ${n} | awk -F '_' '{print $1}')
mv $n ${name}_${filename}
done

mv.sh

(3)、使用mv拼接

 #!/bin/bash
#
path=/root/scripts
cd $path || exit
ls *.html | awk -F '_' '{print "mv "$0" "$1"_hanshan.html"}' | bash

3、批量创建特殊要求用户案例(P360)

数字前加0   #echo {01.10}   #seq -w 10

1、批量添加、删除10个用户

 #!/bin/bash
# #定义变量
. /etc/init.d/functions
user="hanshan"
passfile=/tmp/user.log #判断文件是否存在,不存在则创建
[ -f $passfile ] || cd `dirname $passfile` && touch $passfile #添加用户
add() {
for num in `echo {01..10}`; do
pass="`openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 4-12`" id $user$num &>/dev/null
if [ $? -eq 0 ]; then
echo "$user$num is exist"
continue
fi useradd $user$num &>/dev/null
echo "$pass" | password --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 fail" /bin/false
fi
done echo =================================
cat $passfile
} #删除用户
del() {
for num in `echo {01..10}`; do
id $user$num &>/dev/null
if [ $? -ne 0 ]; then
echo "$user$num is not exist"
continue
fi userdel -r $user$num &>/dev/null
if [ $? -eq 0 ]; then
action "$user$num is delete" /bin/true
else
action "$user$num is fail to delete" /bin/false
fi
done
cat /dev/null >$passfile
} #选择
read -p "Please input your choice {add|del|quit}: " choice
case $choice in
add)
add ;;
del)
del ;;
quit)
exit ;;
*)
echo "your choice in {add|del|quit}"
esac

user.sh

2、使用chpasswd,一个批量更新用户口令的工具

#echo "root:123456" | chpasswd

#chpasswd < 密码文件   //给多个用户设置密码,前提是密码文件不能为空

shell案例题

 #!/bin/bash
# . /etc/init.d/functions
user="hanshan"
passfile=/tmp/user.log for num in `seq -w 10`; do
pass=$(echo "hanshan$RANDOM" | md5sum | cut -c 2-10)
useradd $user$num &>/dev/null
echo -e "$user$num:$pass">>$passfile
if [ $? -eq 0 ]; then
action "$user$num is ok" /bin/true
else
action "$user$num is fail" /bin/false
fi
done echo =========================
chpasswd < $passfile
cat $passfile && >$passfile

chpasswd.sh

4、bash for循环打印下面这句话中字母数不大于6的单词

5、单词及字母去重排序(P373)  //参考:

The months of learning in Old Boy education are the few months that I think the time efficient is the most.

I had also studied at other training institutions before, but I was hard to understand what the tutor said and hard to follow.

It was just too much to learn with no outline.

1、按单词出现频率降序排序!

2、按字母出现频率降序排序!

你好