shell案例题
目录:
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 < 密码文件 //给多个用户设置密码,前提是密码文件不能为空
#!/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、按字母出现频率降序排序!
你好
shell案例题的更多相关文章
- R语言、02 案例2-1 Pelican商店、《商务与经济统计》案例题
编程教材 <R语言实战·第2版>Robert I. Kabacoff 课程教材<商务与经济统计·原书第13版> (安德森) P48.案例2-1 Pelican 商店 PS C: ...
- 史上最全前端面试题(含答案)-A篇
HTML+CSS1.对WEB标准以及W3C的理解与认识标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外 链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所 ...
- Linux计划任务 定时任务 Crond 配置详解 crond计划任务调试 sh -x 详解 JAVA脚本环境变量定义
一.Crond 是什么?(概述) crontab 是一款linux系统中的定时任务软件用于实现无人值守或后台定期执行及循环执行任务的脚本程序,在企业中使用的非常广泛. 现在开始学习linux计 ...
- 33、awk命令详解
33.1.命令介绍: awk不仅仅是linux系统中的一个命令,而且是一种编程语言,可以用来处理数据和生成报告. awk的数据可以是一个或多个文件,可以是来自标准输入,也可以通过管道获取标准输入,aw ...
- HTML5面试题-备
万不可投机取巧.只求当时过关,非长久之计也!(感谢大神分享) 面试有几点需要注意: 面试题目: 根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑. 题目类型: 技术视野.项目细节.理论知 ...
- 2014web面试题
面试题目会根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑; 类型: 技术视野.项目细节.理论知识型题,算法题,开放性题,案例题. 进行追问,可以确保问到你开始不懂或者面试官开始不懂为止 ...
- 史上最全前端面试题(含答案)-B篇
面试有几点需要注意面试题目: 根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑.题目类型: 技术视野.项目细节.理论知识型题,算法题,开放性题,案例题.进行追问: 可以确保问到你开始不懂 ...
- JS(JavaScript)的进一步了解4(更新中···)
基类Object的子类有 Function Array Number Boolean String Date Math RegExp 函数 数组 数字 布尔 字符串 日期 算数 正则 都 ...
- 此文记录了我从研二下学期到研三上学期的找工历程,包括百度、腾讯、网易、移动、电信、华为、中兴、IBM八家企业的面试总结和心得--转
感谢电子通讯工程的研究生学长为大家整理了这么全面的求职总结,希望进入通信公司和互联网公司做非技术类岗位的学弟学妹们千万不要错过哦~ ---------------------------原文分割线-- ...
随机推荐
- Python print 中间换行 直接加‘\n’
- python中函数嵌套、函数作为变量以及闭包的原理
嵌套函数: python允许创建嵌套函数.也就是说我们可以在函数里面定义函数,而且现有的作用域和变量生存周期依旧不变. 例子: #encoding=utf-8 def outer(): name ...
- Codeforce 287A - IQ Test (模拟)
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...
- Symfony2学习笔记之事件分配器
----EventDispatcher组件使用 简介: 面向对象编程已经在确保代码的可扩展性方面走过了很长一段路.它是通过创建一些责任明确的类,让它们之间变得更加灵活,开发者可以通过继承这 ...
- ES6知识整理(10)--class的继承
(这是es6的第10篇文章.说真的这样的总结之后虽然直观了许多,但是消耗的时间有点长,或许是知识比较复杂的原因吧) 类的继承 有个A类,B类继承A类,那B类实例就可以使用A类实例的所以属性和方法.不包 ...
- Linux查看机器和硬盘的SN
查看硬件RAID中某块硬盘SN # sas 口: [root@ ~]$ smartctl -a /dev/sda -d megaraid,n *** Serial number: 6RJ974SR * ...
- php中通过Hashids将整数转化为唯一字符串
这个类主要是前台显示的主键ID转化成一串无规律的字符串,比较像 Youtube.Youku.Weibo之类的 id 名,从某种意义上可以防采集 在项目中,暴露给用户真实的项目ID,很有可能被恶意采集, ...
- php 把秒数转换为时长(h:i:s格式)
/** * 把秒数转换为时分秒的格式 * @param Int $times 时间,单位 秒 * @return String */ function secToTime($times){ $resu ...
- java配置xml报cvc-complex-type.2.3: 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”
今天测试es-job的时候,启动的时候报了标题中的错误, 经查,原因有二: 一:从网上粘贴过来字符没有对应上,所以子元素property报错,将文字复制到Notepad++等编辑工具,转为UTF-8编 ...
- nlp homework 03
NLP Homework 03 --冯煜博 题目描述 (盒子和球模型)假设有3个盒子,每个盒子里装有红白两种颜色的球,盒子里的红白球有下表列出,初始状态分布. 解答 1. 给出HMM模型 \(\mu= ...