zoukankan      html  css  js  c++  java
  • shell案例题

    目录:

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

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

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

     1 #!/bin/bash
     2 #
     3 path=/root/scripts
     4 
     5 [ -d "$path" ] || mkdir -p $path
     6 
     7 for n in `seq 10`; do
     8     random=`openssl rand -base64 40 | sed 's@[^a-z]@@g' | cut -c 2-12`
     9     touch $path/${random}_hanshan.html
    10 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循环遍历

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

    (3)、使用mv拼接

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

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

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

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

     1 #!/bin/bash
     2 #
     3 
     4 #定义变量
     5 . /etc/init.d/functions
     6 user="hanshan"
     7 passfile=/tmp/user.log
     8 
     9 #判断文件是否存在,不存在则创建
    10 [ -f $passfile ] || cd `dirname $passfile` && touch $passfile
    11 
    12 #添加用户
    13 add() {
    14 for num in `echo {01..10}`; do
    15     pass="`openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 4-12`"
    16   
    17     id $user$num &>/dev/null    
    18     if [ $? -eq 0 ]; then
    19         echo "$user$num is exist"
    20         continue
    21     fi  
    22 
    23     useradd $user$num &>/dev/null  
    24     echo "$pass" | password --stdin $user$num &>/dev/null 
    25     echo -e "user:$user$num	passwd:$pass">>$passfile
    26     if [ $? -eq 0 ]; then
    27         action "$user$num is ok" /bin/true
    28     else
    29         action "$user$num is fail" /bin/false
    30     fi  
    31 done
    32 
    33 echo =================================
    34 cat $passfile
    35 }
    36 
    37 #删除用户
    38 del() {
    39 for num in `echo {01..10}`; do
    40     id $user$num &>/dev/null
    41     if [ $? -ne 0 ]; then
    42         echo "$user$num is not exist"
    43         continue
    44     fi
    45 
    46     userdel -r $user$num &>/dev/null
    47     if [ $? -eq 0 ]; then
    48         action "$user$num is delete" /bin/true
    49     else
    50         action "$user$num is fail to delete" /bin/false
    51     fi
    52 done
    53 cat /dev/null >$passfile
    54 }
    55 
    56 #选择
    57 read -p "Please input your choice {add|del|quit}: " choice
    58 case $choice in
    59     add)
    60       add ;;
    61     del)
    62       del ;;
    63     quit)
    64       exit ;;
    65     *)
    66     echo "your choice in {add|del|quit}"
    67 esac
    user.sh

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

    #echo "root:123456" | chpasswd 

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

     1 #!/bin/bash
     2 #
     3 
     4 . /etc/init.d/functions
     5 user="hanshan"
     6 passfile=/tmp/user.log
     7 
     8 for num in `seq -w 10`; do
     9     pass=$(echo "hanshan$RANDOM" | md5sum | cut -c 2-10)
    10     useradd $user$num &>/dev/null
    11     echo -e "$user$num:$pass">>$passfile
    12     if [ $? -eq 0 ]; then
    13         action "$user$num  is ok" /bin/true
    14     else
    15         action "$user$num is fail" /bin/false
    16     fi  
    17 done
    18 
    19 echo =========================
    20 chpasswd < $passfile
    21 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、按字母出现频率降序排序!

    你好

  • 相关阅读:
    C++函数模板的显示调用与隐式调用
    git显示颜色配置
    STL容器元素应满足的条件
    vector缩减容量
    PAT (Basic Level) Practise:1036. 跟奥巴马一起编程
    Core Java Volume I — 4.10. Class Design Hints
    Core Java Volume I — 4.7. Packages
    蓝牙(Profile)构成
    Android开发之Java必备基础
    主机控制器接口(HCI)
  • 原文地址:https://www.cnblogs.com/hanshanxiaoheshang/p/9517132.html
Copyright © 2011-2022 走看看