shell

作者: 秋幻旎苏 | 来源:发表于2018-06-28 08:35 被阅读0次

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

#!/bin/bash
reserve='a-z'
unset -v random
if [ ! -d /opt/test ];then
  mkdir /opt/test
fi
count=0
for i in {1..10}
do
  random=$( cat /dev/urandom| tr -dc $reserve |head -c 10)
  touch /opt/test/${random}_test.html
done

2.将以上面试题1中结果文件名中的test字符串全部改成test2(最好用for循环实现),并且将扩展名html全部改成大写。

#!/bin/bash
cd /opt/test
file=$(find ./ -name "*test*")
for i in $file
do
  echo $i
  newfile=$(echo $i |sed "s/test/test1/g" | sed "s/html/HTML/g")
  echo $newfile
  mv $i $newfile
done

3.批量创建10个系统帐号test01-test10并设置密码(密码为随机数,要求字符和数字等混合)

#!/bin/bash
a=(01 02 03 04 05 06 07 08 09 10)
unset -v password
for i in {0..9}
do
  useradd test${a[$i]}
  password=$( cat /dev/urandom| tr -dc 0-9-a-z-A-Z |head -c 10)
  echo $password |passwd test${a[$i]} --stdin
done
echo test{01..10} | xargs -n1 useradd  | echo $(echo $RANDOM|md5sum|cut -c 1-8) |xargs -n1 passwd --stdin

4.写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?

#!/bin/bash
for i in {2..254}
do
  ping -c 2 10.0.0.$i >/dev/null
  if [ $? -eq 0 ];then
    echo 10.0.0.$i
  fi
done

5.写一个Shell脚本解决DOS攻击生产案例。
请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100(读者根据实际情况设定),即调用防火墙命令封掉对应的IP。防火墙命令为:iptables-I INPUT -s IP地址 -j DROP。

#!/bin/bash
file=/apps/project/nginx/logs/access_2017-09-01.log
#command=$(cat $file |  awk '{++S[$1]} END {for(i in S)print S[i],i}'  |sort -rn| head -n 100)
command=$(cat $file |awk '{print $1}' |sort | uniq -c | sort -n -k 1 -r | head -n 100)
echo $command > access_ip.txt
iplist=$(cat acceess_ip.txt |awk '{if($1>100) print $2}')
for ip in $iplist
do
   iptables -I INPUT -s $ip -j DROP
   echo "$ip is drop!"
done
#!/bin/bash 
command=$(netstat  -ntu |awk '{print $5}' |cut -d ":" -f 1 | sort | uniq -c |sort -rn )
echo $command > access_ip.txt
iplist=$(cat acceess_ip.txt |awk '{if($1>100) print $2}')
for ip in $iplist
do
   iptables -I INPUT -s $ip -j DROP
   echo "$ip is drop!"
done

6.MySQL数据库分库分表备份

#!/bin/bash
user=root
password=123456
socket=/var/lib/mysql.sock
login=$(mysql -u$user -p$password -S $socket)
database=$($login -e "show databases"|egrep -iv "date|_schema|mysql")
for dbname in $database
do
  backup_dir=/apps/backup/
  [ ! -d $backup_dir ] && mkdir -p $backup_dir
  mysqldump -u$user -p$password -S $socket -B $dbname |gzip > $backup_dir/$dbname_${date +%F}.sql.gz
done

http://www.xuliangwei.com/xubusi/252.html
7.利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。
I am oldboy teacher welcome to oldboy trainingclass

#!/bin/bash
sentence=$(cat test.txt)
word=$(echo $sentence |tr " " "\n")
for i in $word
do
  count=$(echo $i |wc -L )
  if [ $count -le 6 ];then
    echo $i
  fi
done

相关文章

  • Shell 学习

    shell 变量 shell 参数传递 shell 数组 shell 运算符 shell echo 命令 prin...

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • Shell 教程

    Shell 变量 Shell 传递参数 Shell 数组 Shell 基本运算符 Shell echo 命令 Sh...

  • shell 第一天

    shell编程初识 1.1 shell编程初识 shell的定义 Shell 是命令解释器 Shell 也是...

  • shell 案例

    Shell编程一 Shell防范ARP攻击 Shell编程二 Shell防范DDos攻击 Shell编程三 ...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • Linux Shell:基础知识和Shell变量

    摘要:Linux,Shell 整理Shell内容要点: Shell基础知识 Shell变量的类型 Shell变量赋...

  • Shell脚本语言一

    一、语法 格式 运行 Shell变量 Shell字符串 Shell数组 Shell注释 Shell传递参数 She...

  • 使用shell脚本

    使用方式 shell 变量 shell 字符串操作 shell 数组 shell 注释 shell 命令行参数 s...

  • vim学习 09——shell命令

    vim学习 09——shell命令 执行 shell 命令 :!shell命令 : 可以执行 shell 命令。 ...

网友评论

    本文标题:shell

    本文链接:https://www.haomeiwen.com/subject/xajrjxtx.html